blob: 9b0800eddb47dc76782b80fbc4eff283b1dc68e6 [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>
Christopher Fauletef779222018-10-31 08:47:01 +010056#include <proto/htx.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010057#include <proto/log.h>
58#include <proto/pattern.h>
59#include <proto/pipe.h>
60#include <proto/listener.h>
61#include <proto/map.h>
Christopher Fauletef779222018-10-31 08:47:01 +010062#include <proto/proto_http.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010063#include <proto/proxy.h>
64#include <proto/sample.h>
65#include <proto/session.h>
66#include <proto/stream.h>
67#include <proto/server.h>
68#include <proto/raw_sock.h>
69#include <proto/stream_interface.h>
70#include <proto/task.h>
71
72#ifdef USE_OPENSSL
73#include <proto/ssl_sock.h>
74#include <types/ssl_sock.h>
75#endif
76
77
Willy Tarreau0baac8c2016-11-22 16:36:53 +010078/* These are the field names for each INF_* field position. Please pay attention
79 * to always use the exact same name except that the strings for new names must
80 * be lower case or CamelCase while the enum entries must be upper case.
81 */
82const char *info_field_names[INF_TOTAL_FIELDS] = {
83 [INF_NAME] = "Name",
84 [INF_VERSION] = "Version",
85 [INF_RELEASE_DATE] = "Release_date",
Yves Lafon95317282018-02-26 11:10:37 +010086 [INF_NBTHREAD] = "Nbthread",
Willy Tarreau0baac8c2016-11-22 16:36:53 +010087 [INF_NBPROC] = "Nbproc",
88 [INF_PROCESS_NUM] = "Process_num",
89 [INF_PID] = "Pid",
90 [INF_UPTIME] = "Uptime",
91 [INF_UPTIME_SEC] = "Uptime_sec",
92 [INF_MEMMAX_MB] = "Memmax_MB",
93 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
94 [INF_POOL_USED_MB] = "PoolUsed_MB",
95 [INF_POOL_FAILED] = "PoolFailed",
96 [INF_ULIMIT_N] = "Ulimit-n",
97 [INF_MAXSOCK] = "Maxsock",
98 [INF_MAXCONN] = "Maxconn",
99 [INF_HARD_MAXCONN] = "Hard_maxconn",
100 [INF_CURR_CONN] = "CurrConns",
101 [INF_CUM_CONN] = "CumConns",
102 [INF_CUM_REQ] = "CumReq",
103 [INF_MAX_SSL_CONNS] = "MaxSslConns",
104 [INF_CURR_SSL_CONNS] = "CurrSslConns",
105 [INF_CUM_SSL_CONNS] = "CumSslConns",
106 [INF_MAXPIPES] = "Maxpipes",
107 [INF_PIPES_USED] = "PipesUsed",
108 [INF_PIPES_FREE] = "PipesFree",
109 [INF_CONN_RATE] = "ConnRate",
110 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
111 [INF_MAX_CONN_RATE] = "MaxConnRate",
112 [INF_SESS_RATE] = "SessRate",
113 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
114 [INF_MAX_SESS_RATE] = "MaxSessRate",
115 [INF_SSL_RATE] = "SslRate",
116 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
117 [INF_MAX_SSL_RATE] = "MaxSslRate",
118 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
119 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
120 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
121 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
122 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
123 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
124 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
125 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
126 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
127 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
128 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
129 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
130 [INF_TASKS] = "Tasks",
131 [INF_RUN_QUEUE] = "Run_queue",
132 [INF_IDLE_PCT] = "Idle_pct",
133 [INF_NODE] = "node",
134 [INF_DESCRIPTION] = "description",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100135 [INF_STOPPING] = "Stopping",
136 [INF_JOBS] = "Jobs",
William Lallemanda7199262018-11-16 16:57:20 +0100137 [INF_UNSTOPPABLE_JOBS] = "Unstoppable Jobs",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100138 [INF_LISTENERS] = "Listeners",
Willy Tarreau199ad242018-11-05 16:31:22 +0100139 [INF_ACTIVE_PEERS] = "ActivePeers",
Willy Tarreau2d372c22018-11-05 17:12:27 +0100140 [INF_CONNECTED_PEERS] = "ConnectedPeers",
Willy Tarreau13ef7732018-11-12 07:25:28 +0100141 [INF_DROPPED_LOGS] = "DroppedLogs",
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100142 [INF_BUSY_POLLING] = "BusyPolling",
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100143};
144
William Lallemand74c24fb2016-11-21 17:18:36 +0100145const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
146 [ST_F_PXNAME] = "pxname",
147 [ST_F_SVNAME] = "svname",
148 [ST_F_QCUR] = "qcur",
149 [ST_F_QMAX] = "qmax",
150 [ST_F_SCUR] = "scur",
151 [ST_F_SMAX] = "smax",
152 [ST_F_SLIM] = "slim",
153 [ST_F_STOT] = "stot",
154 [ST_F_BIN] = "bin",
155 [ST_F_BOUT] = "bout",
156 [ST_F_DREQ] = "dreq",
157 [ST_F_DRESP] = "dresp",
158 [ST_F_EREQ] = "ereq",
159 [ST_F_ECON] = "econ",
160 [ST_F_ERESP] = "eresp",
161 [ST_F_WRETR] = "wretr",
162 [ST_F_WREDIS] = "wredis",
163 [ST_F_STATUS] = "status",
164 [ST_F_WEIGHT] = "weight",
165 [ST_F_ACT] = "act",
166 [ST_F_BCK] = "bck",
167 [ST_F_CHKFAIL] = "chkfail",
168 [ST_F_CHKDOWN] = "chkdown",
169 [ST_F_LASTCHG] = "lastchg",
170 [ST_F_DOWNTIME] = "downtime",
171 [ST_F_QLIMIT] = "qlimit",
172 [ST_F_PID] = "pid",
173 [ST_F_IID] = "iid",
174 [ST_F_SID] = "sid",
175 [ST_F_THROTTLE] = "throttle",
176 [ST_F_LBTOT] = "lbtot",
177 [ST_F_TRACKED] = "tracked",
178 [ST_F_TYPE] = "type",
179 [ST_F_RATE] = "rate",
180 [ST_F_RATE_LIM] = "rate_lim",
181 [ST_F_RATE_MAX] = "rate_max",
182 [ST_F_CHECK_STATUS] = "check_status",
183 [ST_F_CHECK_CODE] = "check_code",
184 [ST_F_CHECK_DURATION] = "check_duration",
185 [ST_F_HRSP_1XX] = "hrsp_1xx",
186 [ST_F_HRSP_2XX] = "hrsp_2xx",
187 [ST_F_HRSP_3XX] = "hrsp_3xx",
188 [ST_F_HRSP_4XX] = "hrsp_4xx",
189 [ST_F_HRSP_5XX] = "hrsp_5xx",
190 [ST_F_HRSP_OTHER] = "hrsp_other",
191 [ST_F_HANAFAIL] = "hanafail",
192 [ST_F_REQ_RATE] = "req_rate",
193 [ST_F_REQ_RATE_MAX] = "req_rate_max",
194 [ST_F_REQ_TOT] = "req_tot",
195 [ST_F_CLI_ABRT] = "cli_abrt",
196 [ST_F_SRV_ABRT] = "srv_abrt",
197 [ST_F_COMP_IN] = "comp_in",
198 [ST_F_COMP_OUT] = "comp_out",
199 [ST_F_COMP_BYP] = "comp_byp",
200 [ST_F_COMP_RSP] = "comp_rsp",
201 [ST_F_LASTSESS] = "lastsess",
202 [ST_F_LAST_CHK] = "last_chk",
203 [ST_F_LAST_AGT] = "last_agt",
204 [ST_F_QTIME] = "qtime",
205 [ST_F_CTIME] = "ctime",
206 [ST_F_RTIME] = "rtime",
207 [ST_F_TTIME] = "ttime",
208 [ST_F_AGENT_STATUS] = "agent_status",
209 [ST_F_AGENT_CODE] = "agent_code",
210 [ST_F_AGENT_DURATION] = "agent_duration",
211 [ST_F_CHECK_DESC] = "check_desc",
212 [ST_F_AGENT_DESC] = "agent_desc",
213 [ST_F_CHECK_RISE] = "check_rise",
214 [ST_F_CHECK_FALL] = "check_fall",
215 [ST_F_CHECK_HEALTH] = "check_health",
216 [ST_F_AGENT_RISE] = "agent_rise",
217 [ST_F_AGENT_FALL] = "agent_fall",
218 [ST_F_AGENT_HEALTH] = "agent_health",
219 [ST_F_ADDR] = "addr",
220 [ST_F_COOKIE] = "cookie",
221 [ST_F_MODE] = "mode",
222 [ST_F_ALGO] = "algo",
223 [ST_F_CONN_RATE] = "conn_rate",
224 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
225 [ST_F_CONN_TOT] = "conn_tot",
226 [ST_F_INTERCEPTED] = "intercepted",
227 [ST_F_DCON] = "dcon",
228 [ST_F_DSES] = "dses",
Tim Duesterhus3fd19732018-05-27 20:35:08 +0200229 [ST_F_WREW] = "wrew",
William Lallemand74c24fb2016-11-21 17:18:36 +0100230};
231
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100232/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100233static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100234/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100235static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100236
237
Christopher Fauletef779222018-10-31 08:47:01 +0100238static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
239{
240 if (htx) {
241 if (!htx_add_data(htx, ist2(chk->area, chk->data)))
242 return 0;
243 chn->total += chk->data;
244 chk->data = 0;
245 /* notify that some data was read from the SI into the buffer */
246 chn->flags |= CF_READ_PARTIAL;
247 }
248 else {
249 if (ci_putchk(chn, chk) == -1)
250 return 0;
251 }
252 return 1;
253}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100254
William Lallemand74c24fb2016-11-21 17:18:36 +0100255/*
256 * http_stats_io_handler()
257 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
258 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100259 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100260 * -> stats_dump_html_head() // emits the HTML headers
261 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
262 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
263 * -> stats_dump_html_px_hdr()
264 * -> stats_dump_fe_stats()
265 * -> stats_dump_li_stats()
266 * -> stats_dump_sv_stats()
267 * -> stats_dump_be_stats()
268 * -> stats_dump_html_px_end()
269 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100270 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100271 */
272
273
274extern const char *stat_status_codes[];
275
276/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
277 * for clearing it if needed.
278 * NOTE: Some tools happen to rely on the field position instead of its name,
279 * so please only append new fields at the end, never in the middle.
280 */
281static void stats_dump_csv_header()
282{
283 int field;
284
285 chunk_appendf(&trash, "# ");
286 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
287 chunk_appendf(&trash, "%s,", stat_field_names[field]);
288
289 chunk_appendf(&trash, "\n");
290}
291
292
293/* Emits a stats field without any surrounding element and properly encoded to
294 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
295 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200296int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100297{
298 switch (field_format(f, 0)) {
299 case FF_EMPTY: return 1;
300 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
301 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
302 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
303 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
304 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
305 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
306 }
307}
308
309/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
310 * output is supposed to be used on its own line. Returns non-zero on success, 0
311 * if the buffer is full.
312 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200313int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100314{
315 switch (field_format(f, 0)) {
316 case FF_EMPTY: return 1;
317 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
318 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
319 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
320 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
321 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
322 default: return chunk_appendf(out, "%08x:?", f->type);
323 }
324}
325
Simon Horman05ee2132017-01-04 09:37:25 +0100326/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
327 * the recommendation for interoperable integers in section 6 of RFC 7159.
328 */
329#define JSON_INT_MAX ((1ULL << 53) - 1)
330#define JSON_INT_MIN (0 - JSON_INT_MAX)
331
332/* Emits a stats field value and its type in JSON.
333 * Returns non-zero on success, 0 on error.
334 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200335int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100336{
337 int old_len;
338 char buf[20];
339 const char *type, *value = buf, *quote = "";
340
341 switch (field_format(f, 0)) {
342 case FF_EMPTY: return 1;
343 case FF_S32: type = "\"s32\"";
344 snprintf(buf, sizeof(buf), "%d", f->u.s32);
345 break;
346 case FF_U32: type = "\"u32\"";
347 snprintf(buf, sizeof(buf), "%u", f->u.u32);
348 break;
349 case FF_S64: type = "\"s64\"";
350 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
351 return 0;
352 type = "\"s64\"";
353 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
354 break;
355 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
356 return 0;
357 type = "\"u64\"";
358 snprintf(buf, sizeof(buf), "%llu",
359 (unsigned long long) f->u.u64);
360 break;
361 case FF_STR: type = "\"str\"";
362 value = field_str(f, 0);
363 quote = "\"";
364 break;
365 default: snprintf(buf, sizeof(buf), "%u", f->type);
366 type = buf;
367 value = "unknown";
368 quote = "\"";
369 break;
370 }
371
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200372 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100373 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
374 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200375 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100376}
377
William Lallemand74c24fb2016-11-21 17:18:36 +0100378/* Emits an encoding of the field type on 3 characters followed by a delimiter.
379 * Returns non-zero on success, 0 if the buffer is full.
380 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200381int stats_emit_field_tags(struct buffer *out, const struct field *f,
382 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100383{
384 char origin, nature, scope;
385
386 switch (field_origin(f, 0)) {
387 case FO_METRIC: origin = 'M'; break;
388 case FO_STATUS: origin = 'S'; break;
389 case FO_KEY: origin = 'K'; break;
390 case FO_CONFIG: origin = 'C'; break;
391 case FO_PRODUCT: origin = 'P'; break;
392 default: origin = '?'; break;
393 }
394
395 switch (field_nature(f, 0)) {
396 case FN_GAUGE: nature = 'G'; break;
397 case FN_LIMIT: nature = 'L'; break;
398 case FN_MIN: nature = 'm'; break;
399 case FN_MAX: nature = 'M'; break;
400 case FN_RATE: nature = 'R'; break;
401 case FN_COUNTER: nature = 'C'; break;
402 case FN_DURATION: nature = 'D'; break;
403 case FN_AGE: nature = 'A'; break;
404 case FN_TIME: nature = 'T'; break;
405 case FN_NAME: nature = 'N'; break;
406 case FN_OUTPUT: nature = 'O'; break;
407 case FN_AVG: nature = 'a'; break;
408 default: nature = '?'; break;
409 }
410
411 switch (field_scope(f, 0)) {
412 case FS_PROCESS: scope = 'P'; break;
413 case FS_SERVICE: scope = 'S'; break;
414 case FS_SYSTEM: scope = 's'; break;
415 case FS_CLUSTER: scope = 'C'; break;
416 default: scope = '?'; break;
417 }
418
419 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
420}
421
Simon Horman05ee2132017-01-04 09:37:25 +0100422/* Emits an encoding of the field type as JSON.
423 * Returns non-zero on success, 0 if the buffer is full.
424 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200425int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100426{
427 const char *origin, *nature, *scope;
428 int old_len;
429
430 switch (field_origin(f, 0)) {
431 case FO_METRIC: origin = "Metric"; break;
432 case FO_STATUS: origin = "Status"; break;
433 case FO_KEY: origin = "Key"; break;
434 case FO_CONFIG: origin = "Config"; break;
435 case FO_PRODUCT: origin = "Product"; break;
436 default: origin = "Unknown"; break;
437 }
438
439 switch (field_nature(f, 0)) {
440 case FN_GAUGE: nature = "Gauge"; break;
441 case FN_LIMIT: nature = "Limit"; break;
442 case FN_MIN: nature = "Min"; break;
443 case FN_MAX: nature = "Max"; break;
444 case FN_RATE: nature = "Rate"; break;
445 case FN_COUNTER: nature = "Counter"; break;
446 case FN_DURATION: nature = "Duration"; break;
447 case FN_AGE: nature = "Age"; break;
448 case FN_TIME: nature = "Time"; break;
449 case FN_NAME: nature = "Name"; break;
450 case FN_OUTPUT: nature = "Output"; break;
451 case FN_AVG: nature = "Avg"; break;
452 default: nature = "Unknown"; break;
453 }
454
455 switch (field_scope(f, 0)) {
456 case FS_PROCESS: scope = "Process"; break;
457 case FS_SERVICE: scope = "Service"; break;
458 case FS_SYSTEM: scope = "System"; break;
459 case FS_CLUSTER: scope = "Cluster"; break;
460 default: scope = "Unknown"; break;
461 }
462
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200463 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100464 chunk_appendf(out, "\"tags\":{"
465 "\"origin\":\"%s\","
466 "\"nature\":\"%s\","
467 "\"scope\":\"%s\""
468 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200469 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100470}
William Lallemand74c24fb2016-11-21 17:18:36 +0100471
472/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200473static int stats_dump_fields_csv(struct buffer *out,
474 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100475{
476 int field;
477
478 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
479 if (!stats_emit_raw_data_field(out, &stats[field]))
480 return 0;
481 if (!chunk_strcat(out, ","))
482 return 0;
483 }
484 chunk_strcat(out, "\n");
485 return 1;
486}
487
488/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200489static int stats_dump_fields_typed(struct buffer *out,
490 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100491{
492 int field;
493
494 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
495 if (!stats[field].type)
496 continue;
497
498 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
499 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
500 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
501 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
502 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
503 '?',
504 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
505 field, stat_field_names[field], stats[ST_F_PID].u.u32);
506
507 if (!stats_emit_field_tags(out, &stats[field], ':'))
508 return 0;
509 if (!stats_emit_typed_data_field(out, &stats[field]))
510 return 0;
511 if (!chunk_strcat(out, "\n"))
512 return 0;
513 }
514 return 1;
515}
516
Simon Horman05ee2132017-01-04 09:37:25 +0100517/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200518static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100519 const struct field *info)
520{
521 int field;
522 int started = 0;
523
524 if (!chunk_strcat(out, "["))
525 return 0;
526
527 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
528 int old_len;
529
530 if (!field_format(info, field))
531 continue;
532
533 if (started && !chunk_strcat(out, ","))
534 goto err;
535 started = 1;
536
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200537 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100538 chunk_appendf(out,
539 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
540 "\"processNum\":%u,",
541 field, info_field_names[field],
542 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200543 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100544 goto err;
545
546 if (!stats_emit_json_field_tags(out, &info[field]))
547 goto err;
548
549 if (!stats_emit_json_data_field(out, &info[field]))
550 goto err;
551
552 if (!chunk_strcat(out, "}"))
553 goto err;
554 }
555
556 if (!chunk_strcat(out, "]"))
557 goto err;
558 return 1;
559
560err:
561 chunk_reset(out);
562 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
563 return 0;
564}
565
566/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200567static int stats_dump_fields_json(struct buffer *out,
568 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100569 int first_stat)
570{
571 int field;
572 int started = 0;
573
574 if (!first_stat && !chunk_strcat(out, ","))
575 return 0;
576 if (!chunk_strcat(out, "["))
577 return 0;
578
579 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
580 const char *obj_type;
581 int old_len;
582
583 if (!stats[field].type)
584 continue;
585
586 if (started && !chunk_strcat(out, ","))
587 goto err;
588 started = 1;
589
590 switch (stats[ST_F_TYPE].u.u32) {
591 case STATS_TYPE_FE: obj_type = "Frontend"; break;
592 case STATS_TYPE_BE: obj_type = "Backend"; break;
593 case STATS_TYPE_SO: obj_type = "Listener"; break;
594 case STATS_TYPE_SV: obj_type = "Server"; break;
595 default: obj_type = "Unknown"; break;
596 }
597
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200598 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100599 chunk_appendf(out,
600 "{"
601 "\"objType\":\"%s\","
602 "\"proxyId\":%d,"
603 "\"id\":%d,"
604 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
605 "\"processNum\":%u,",
606 obj_type, stats[ST_F_IID].u.u32,
607 stats[ST_F_SID].u.u32, field,
608 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200609 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100610 goto err;
611
612 if (!stats_emit_json_field_tags(out, &stats[field]))
613 goto err;
614
615 if (!stats_emit_json_data_field(out, &stats[field]))
616 goto err;
617
618 if (!chunk_strcat(out, "}"))
619 goto err;
620 }
621
622 if (!chunk_strcat(out, "]"))
623 goto err;
624
625 return 1;
626
627err:
628 chunk_reset(out);
629 if (!first_stat)
630 chunk_strcat(out, ",");
631 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
632 return 0;
633}
634
William Lallemand74c24fb2016-11-21 17:18:36 +0100635/* Dump all fields from <stats> into <out> using the HTML format. A column is
636 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
637 * are provided if ST_SHLGNDS is present in <flags>.
638 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200639static int stats_dump_fields_html(struct buffer *out,
640 const struct field *stats,
641 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100642{
Willy Tarreau83061a82018-07-13 11:56:34 +0200643 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100644
645 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
646 chunk_appendf(out,
647 /* name, queue */
648 "<tr class=\"frontend\">");
649
650 if (flags & ST_SHOWADMIN) {
651 /* Column sub-heading for Enable or Disable server */
652 chunk_appendf(out, "<td></td>");
653 }
654
655 chunk_appendf(out,
656 "<td class=ac>"
657 "<a name=\"%s/Frontend\"></a>"
658 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
659 "<td colspan=3></td>"
660 "",
661 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
662
663 chunk_appendf(out,
664 /* sessions rate : current */
665 "<td><u>%s<div class=tips><table class=det>"
666 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
667 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
668 "",
669 U2H(stats[ST_F_RATE].u.u32),
670 U2H(stats[ST_F_CONN_RATE].u.u32),
671 U2H(stats[ST_F_RATE].u.u32));
672
673 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
674 chunk_appendf(out,
675 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
676 U2H(stats[ST_F_REQ_RATE].u.u32));
677
678 chunk_appendf(out,
679 "</table></div></u></td>"
680 /* sessions rate : max */
681 "<td><u>%s<div class=tips><table class=det>"
682 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
683 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
684 "",
685 U2H(stats[ST_F_RATE_MAX].u.u32),
686 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
687 U2H(stats[ST_F_RATE_MAX].u.u32));
688
689 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
690 chunk_appendf(out,
691 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
692 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
693
694 chunk_appendf(out,
695 "</table></div></u></td>"
696 /* sessions rate : limit */
697 "<td>%s</td>",
698 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
699
700 chunk_appendf(out,
701 /* sessions: current, max, limit, total */
702 "<td>%s</td><td>%s</td><td>%s</td>"
703 "<td><u>%s<div class=tips><table class=det>"
704 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
705 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
706 "",
707 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
708 U2H(stats[ST_F_STOT].u.u64),
709 U2H(stats[ST_F_CONN_TOT].u.u64),
710 U2H(stats[ST_F_STOT].u.u64));
711
712 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
713 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
714 chunk_appendf(out,
715 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
716 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
717 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
718 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
719 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
720 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
721 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
722 "<tr><th>- other responses:</th><td>%s</td></tr>"
723 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200724 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100725 "",
726 U2H(stats[ST_F_REQ_TOT].u.u64),
727 U2H(stats[ST_F_HRSP_1XX].u.u64),
728 U2H(stats[ST_F_HRSP_2XX].u.u64),
729 U2H(stats[ST_F_COMP_RSP].u.u64),
730 stats[ST_F_HRSP_2XX].u.u64 ?
731 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
732 U2H(stats[ST_F_HRSP_3XX].u.u64),
733 U2H(stats[ST_F_HRSP_4XX].u.u64),
734 U2H(stats[ST_F_HRSP_5XX].u.u64),
735 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200736 U2H(stats[ST_F_INTERCEPTED].u.u64),
737 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100738 }
739
740 chunk_appendf(out,
741 "</table></div></u></td>"
742 /* sessions: lbtot, lastsess */
743 "<td></td><td></td>"
744 /* bytes : in */
745 "<td>%s</td>"
746 "",
747 U2H(stats[ST_F_BIN].u.u64));
748
749 chunk_appendf(out,
750 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
751 "<td>%s%s<div class=tips><table class=det>"
752 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
753 "<tr><th>Compression in:</th><td>%s</td></tr>"
754 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
755 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
756 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
757 "</table></div>%s</td>",
758 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
759 U2H(stats[ST_F_BOUT].u.u64),
760 U2H(stats[ST_F_BOUT].u.u64),
761 U2H(stats[ST_F_COMP_IN].u.u64),
762 U2H(stats[ST_F_COMP_OUT].u.u64),
763 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
764 U2H(stats[ST_F_COMP_BYP].u.u64),
765 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
766 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,
767 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
768
769 chunk_appendf(out,
770 /* denied: req, resp */
771 "<td>%s</td><td>%s</td>"
772 /* errors : request, connect, response */
773 "<td>%s</td><td></td><td></td>"
774 /* warnings: retries, redispatches */
775 "<td></td><td></td>"
776 /* server status : reflect frontend status */
777 "<td class=ac>%s</td>"
778 /* rest of server: nothing */
779 "<td class=ac colspan=8></td></tr>"
780 "",
781 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
782 U2H(stats[ST_F_EREQ].u.u64),
783 field_str(stats, ST_F_STATUS));
784 }
785 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
786 chunk_appendf(out, "<tr class=socket>");
787 if (flags & ST_SHOWADMIN) {
788 /* Column sub-heading for Enable or Disable server */
789 chunk_appendf(out, "<td></td>");
790 }
791
792 chunk_appendf(out,
793 /* frontend name, listener name */
794 "<td class=ac><a name=\"%s/+%s\"></a>%s"
795 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
796 "",
797 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
798 (flags & ST_SHLGNDS)?"<u>":"",
799 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
800
801 if (flags & ST_SHLGNDS) {
802 chunk_appendf(out, "<div class=tips>");
803
804 if (isdigit(*field_str(stats, ST_F_ADDR)))
805 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
806 else if (*field_str(stats, ST_F_ADDR) == '[')
807 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
808 else if (*field_str(stats, ST_F_ADDR))
809 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
810
811 /* id */
812 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
813 }
814
815 chunk_appendf(out,
816 /* queue */
817 "%s</td><td colspan=3></td>"
818 /* sessions rate: current, max, limit */
819 "<td colspan=3>&nbsp;</td>"
820 /* sessions: current, max, limit, total, lbtot, lastsess */
821 "<td>%s</td><td>%s</td><td>%s</td>"
822 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
823 /* bytes: in, out */
824 "<td>%s</td><td>%s</td>"
825 "",
826 (flags & ST_SHLGNDS)?"</u>":"",
827 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
828 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
829
830 chunk_appendf(out,
831 /* denied: req, resp */
832 "<td>%s</td><td>%s</td>"
833 /* errors: request, connect, response */
834 "<td>%s</td><td></td><td></td>"
835 /* warnings: retries, redispatches */
836 "<td></td><td></td>"
837 /* server status: reflect listener status */
838 "<td class=ac>%s</td>"
839 /* rest of server: nothing */
840 "<td class=ac colspan=8></td></tr>"
841 "",
842 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
843 U2H(stats[ST_F_EREQ].u.u64),
844 field_str(stats, ST_F_STATUS));
845 }
846 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
847 const char *style;
848
849 /* determine the style to use depending on the server's state,
850 * its health and weight. There isn't a 1-to-1 mapping between
851 * state and styles for the cases where the server is (still)
852 * up. The reason is that we don't want to report nolb and
853 * drain with the same color.
854 */
855
856 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
857 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
858 style = "down";
859 }
860 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
861 style = "going_up";
862 }
863 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
864 style = "going_down";
865 }
866 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
867 style = "nolb";
868 }
869 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
870 style = "no_check";
871 }
872 else if (!stats[ST_F_CHKFAIL].type ||
873 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
874 /* no check or max health = UP */
875 if (stats[ST_F_WEIGHT].u.u32)
876 style = "up";
877 else
878 style = "draining";
879 }
880 else {
881 style = "going_down";
882 }
883
884 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
885 chunk_appendf(out, "<tr class=\"maintain\">");
886 else
887 chunk_appendf(out,
888 "<tr class=\"%s_%s\">",
889 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
890
891
892 if (flags & ST_SHOWADMIN)
893 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000894 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
895 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100896 field_str(stats, ST_F_SVNAME));
897
898 chunk_appendf(out,
899 "<td class=ac><a name=\"%s/%s\"></a>%s"
900 "<a class=lfsb href=\"#%s/%s\">%s</a>"
901 "",
902 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
903 (flags & ST_SHLGNDS) ? "<u>" : "",
904 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
905
906 if (flags & ST_SHLGNDS) {
907 chunk_appendf(out, "<div class=tips>");
908
909 if (isdigit(*field_str(stats, ST_F_ADDR)))
910 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
911 else if (*field_str(stats, ST_F_ADDR) == '[')
912 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
913 else if (*field_str(stats, ST_F_ADDR))
914 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
915
916 /* id */
917 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
918
919 /* cookie */
920 if (stats[ST_F_COOKIE].type) {
921 chunk_appendf(out, ", cookie: '");
922 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
923 chunk_htmlencode(out, &src);
924 chunk_appendf(out, "'");
925 }
926
927 chunk_appendf(out, "</div>");
928 }
929
930 chunk_appendf(out,
931 /* queue : current, max, limit */
932 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
933 /* sessions rate : current, max, limit */
934 "<td>%s</td><td>%s</td><td></td>"
935 "",
936 (flags & ST_SHLGNDS) ? "</u>" : "",
937 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
938 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
939
940 chunk_appendf(out,
941 /* sessions: current, max, limit, total */
942 "<td>%s</td><td>%s</td><td>%s</td>"
943 "<td><u>%s<div class=tips><table class=det>"
944 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
945 "",
946 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
947 U2H(stats[ST_F_STOT].u.u64),
948 U2H(stats[ST_F_STOT].u.u64));
949
950 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
951 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
952 unsigned long long tot;
953
954 tot = stats[ST_F_HRSP_OTHER].u.u64;
955 tot += stats[ST_F_HRSP_1XX].u.u64;
956 tot += stats[ST_F_HRSP_2XX].u.u64;
957 tot += stats[ST_F_HRSP_3XX].u.u64;
958 tot += stats[ST_F_HRSP_4XX].u.u64;
959 tot += stats[ST_F_HRSP_5XX].u.u64;
960
961 chunk_appendf(out,
962 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
963 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
964 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
965 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
966 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
967 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
968 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200969 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100970 "",
971 U2H(tot),
972 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
973 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
974 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
975 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
976 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 +0200977 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
978 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100979 }
980
981 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
982 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
983 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
984 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
985 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
986 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
987
988 chunk_appendf(out,
989 "</table></div></u></td>"
990 /* sessions: lbtot, last */
991 "<td>%s</td><td>%s</td>",
992 U2H(stats[ST_F_LBTOT].u.u64),
993 human_time(stats[ST_F_LASTSESS].u.s32, 1));
994
995 chunk_appendf(out,
996 /* bytes : in, out */
997 "<td>%s</td><td>%s</td>"
998 /* denied: req, resp */
999 "<td></td><td>%s</td>"
1000 /* errors : request, connect */
1001 "<td></td><td>%s</td>"
1002 /* errors : response */
1003 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1004 /* warnings: retries, redispatches */
1005 "<td>%lld</td><td>%lld</td>"
1006 "",
1007 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1008 U2H(stats[ST_F_DRESP].u.u64),
1009 U2H(stats[ST_F_ECON].u.u64),
1010 U2H(stats[ST_F_ERESP].u.u64),
1011 (long long)stats[ST_F_CLI_ABRT].u.u64,
1012 (long long)stats[ST_F_SRV_ABRT].u.u64,
1013 (long long)stats[ST_F_WRETR].u.u64,
1014 (long long)stats[ST_F_WREDIS].u.u64);
1015
1016 /* status, last change */
1017 chunk_appendf(out, "<td class=ac>");
1018
1019 /* FIXME!!!!
1020 * LASTCHG should contain the last change for *this* server and must be computed
1021 * properly above, as was done below, ie: this server if maint, otherwise ref server
1022 * if tracking. Note that ref is either local or remote depending on tracking.
1023 */
1024
1025
1026 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1027 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1028 }
1029 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1030 chunk_strcat(out, "<i>no check</i>");
1031 }
1032 else {
1033 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1034 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1035 if (stats[ST_F_CHECK_HEALTH].u.u32)
1036 chunk_strcat(out, " &uarr;");
1037 }
1038 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1039 chunk_strcat(out, " &darr;");
1040 }
1041
1042 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1043 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1044 chunk_appendf(out,
1045 "</td><td class=ac><u> %s",
1046 field_str(stats, ST_F_AGENT_STATUS));
1047
1048 if (stats[ST_F_AGENT_CODE].type)
1049 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1050
1051 if (stats[ST_F_AGENT_DURATION].type)
1052 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1053
1054 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1055
1056 if (*field_str(stats, ST_F_LAST_AGT)) {
1057 chunk_appendf(out, ": ");
1058 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1059 chunk_htmlencode(out, &src);
1060 }
1061 chunk_appendf(out, "</div></u>");
1062 }
1063 else if (stats[ST_F_CHECK_STATUS].type) {
1064 chunk_appendf(out,
1065 "</td><td class=ac><u> %s",
1066 field_str(stats, ST_F_CHECK_STATUS));
1067
1068 if (stats[ST_F_CHECK_CODE].type)
1069 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1070
1071 if (stats[ST_F_CHECK_DURATION].type)
1072 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1073
1074 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1075
1076 if (*field_str(stats, ST_F_LAST_CHK)) {
1077 chunk_appendf(out, ": ");
1078 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1079 chunk_htmlencode(out, &src);
1080 }
1081 chunk_appendf(out, "</div></u>");
1082 }
1083 else
1084 chunk_appendf(out, "</td><td>");
1085
1086 chunk_appendf(out,
1087 /* weight */
1088 "</td><td class=ac>%d</td>"
1089 /* act, bck */
1090 "<td class=ac>%s</td><td class=ac>%s</td>"
1091 "",
1092 stats[ST_F_WEIGHT].u.u32,
1093 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1094 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1095
1096 /* check failures: unique, fatal, down time */
1097 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1098 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1099 }
1100 else if (stats[ST_F_CHKFAIL].type) {
1101 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1102
1103 if (stats[ST_F_HANAFAIL].type)
1104 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1105
1106 chunk_appendf(out,
1107 "<div class=tips>Failed Health Checks%s</div></u></td>"
1108 "<td>%lld</td><td>%s</td>"
1109 "",
1110 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1111 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1112 }
1113 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1114 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1115 chunk_appendf(out,
1116 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1117 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1118 }
1119 else
1120 chunk_appendf(out, "<td colspan=3></td>");
1121
1122 /* throttle */
1123 if (stats[ST_F_THROTTLE].type)
1124 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1125 else
1126 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1127 }
1128 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1129 chunk_appendf(out, "<tr class=\"backend\">");
1130 if (flags & ST_SHOWADMIN) {
1131 /* Column sub-heading for Enable or Disable server */
1132 chunk_appendf(out, "<td></td>");
1133 }
1134 chunk_appendf(out,
1135 "<td class=ac>"
1136 /* name */
1137 "%s<a name=\"%s/Backend\"></a>"
1138 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1139 "",
1140 (flags & ST_SHLGNDS)?"<u>":"",
1141 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1142
1143 if (flags & ST_SHLGNDS) {
1144 /* balancing */
1145 chunk_appendf(out, "<div class=tips>balancing: %s",
1146 field_str(stats, ST_F_ALGO));
1147
1148 /* cookie */
1149 if (stats[ST_F_COOKIE].type) {
1150 chunk_appendf(out, ", cookie: '");
1151 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1152 chunk_htmlencode(out, &src);
1153 chunk_appendf(out, "'");
1154 }
1155 chunk_appendf(out, "</div>");
1156 }
1157
1158 chunk_appendf(out,
1159 "%s</td>"
1160 /* queue : current, max */
1161 "<td>%s</td><td>%s</td><td></td>"
1162 /* sessions rate : current, max, limit */
1163 "<td>%s</td><td>%s</td><td></td>"
1164 "",
1165 (flags & ST_SHLGNDS)?"</u>":"",
1166 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1167 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1168
1169 chunk_appendf(out,
1170 /* sessions: current, max, limit, total */
1171 "<td>%s</td><td>%s</td><td>%s</td>"
1172 "<td><u>%s<div class=tips><table class=det>"
1173 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1174 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001175 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 +01001176 U2H(stats[ST_F_STOT].u.u64),
1177 U2H(stats[ST_F_STOT].u.u64));
1178
1179 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1180 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1181 chunk_appendf(out,
1182 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1183 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1184 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1185 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1186 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1187 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1188 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1189 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001190 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001191 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1192 "",
1193 U2H(stats[ST_F_REQ_TOT].u.u64),
1194 U2H(stats[ST_F_HRSP_1XX].u.u64),
1195 U2H(stats[ST_F_HRSP_2XX].u.u64),
1196 U2H(stats[ST_F_COMP_RSP].u.u64),
1197 stats[ST_F_HRSP_2XX].u.u64 ?
1198 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1199 U2H(stats[ST_F_HRSP_3XX].u.u64),
1200 U2H(stats[ST_F_HRSP_4XX].u.u64),
1201 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001202 U2H(stats[ST_F_WREW].u.u64),
Willy Tarreauae9bea02016-11-25 14:44:52 +01001203 U2H(stats[ST_F_HRSP_OTHER].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001204 }
1205
1206 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1207 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1208 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1209 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1210 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1211
1212 chunk_appendf(out,
1213 "</table></div></u></td>"
1214 /* sessions: lbtot, last */
1215 "<td>%s</td><td>%s</td>"
1216 /* bytes: in */
1217 "<td>%s</td>"
1218 "",
1219 U2H(stats[ST_F_LBTOT].u.u64),
1220 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1221 U2H(stats[ST_F_BIN].u.u64));
1222
1223 chunk_appendf(out,
1224 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1225 "<td>%s%s<div class=tips><table class=det>"
1226 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1227 "<tr><th>Compression in:</th><td>%s</td></tr>"
1228 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1229 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1230 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1231 "</table></div>%s</td>",
1232 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1233 U2H(stats[ST_F_BOUT].u.u64),
1234 U2H(stats[ST_F_BOUT].u.u64),
1235 U2H(stats[ST_F_COMP_IN].u.u64),
1236 U2H(stats[ST_F_COMP_OUT].u.u64),
1237 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1238 U2H(stats[ST_F_COMP_BYP].u.u64),
1239 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1240 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,
1241 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1242
1243 chunk_appendf(out,
1244 /* denied: req, resp */
1245 "<td>%s</td><td>%s</td>"
1246 /* errors : request, connect */
1247 "<td></td><td>%s</td>"
1248 /* errors : response */
1249 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1250 /* warnings: retries, redispatches */
1251 "<td>%lld</td><td>%lld</td>"
1252 /* backend status: reflect backend status (up/down): we display UP
1253 * if the backend has known working servers or if it has no server at
1254 * all (eg: for stats). Then we display the total weight, number of
1255 * active and backups. */
1256 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1257 "<td class=ac>%d</td><td class=ac>%d</td>"
1258 "",
1259 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1260 U2H(stats[ST_F_ECON].u.u64),
1261 U2H(stats[ST_F_ERESP].u.u64),
1262 (long long)stats[ST_F_CLI_ABRT].u.u64,
1263 (long long)stats[ST_F_SRV_ABRT].u.u64,
1264 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1265 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1266 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1267 stats[ST_F_WEIGHT].u.u32,
1268 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1269
1270 chunk_appendf(out,
1271 /* rest of backend: nothing, down transitions, total downtime, throttle */
1272 "<td class=ac>&nbsp;</td><td>%d</td>"
1273 "<td>%s</td>"
1274 "<td></td>"
1275 "</tr>",
1276 stats[ST_F_CHKDOWN].u.u32,
1277 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1278 }
1279 return 1;
1280}
1281
1282int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1283{
Simon Horman05ee2132017-01-04 09:37:25 +01001284 int ret;
1285
William Lallemand74c24fb2016-11-21 17:18:36 +01001286 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1287 flags |= ST_SHOWADMIN;
1288
1289 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001290 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001291 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001292 ret = stats_dump_fields_typed(&trash, stats);
1293 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1294 ret = stats_dump_fields_json(&trash, stats,
1295 !(appctx->ctx.stats.flags &
1296 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001297 else
Simon Horman05ee2132017-01-04 09:37:25 +01001298 ret = stats_dump_fields_csv(&trash, stats);
1299
1300 if (ret)
1301 appctx->ctx.stats.flags |= STAT_STARTED;
1302
1303 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001304}
1305
1306/* Fill <stats> with the frontend statistics. <stats> is
1307 * preallocated array of length <len>. The length of the array
1308 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1309 * this value, the function returns 0, otherwise, it returns 1.
1310 */
1311int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1312{
1313 if (len < ST_F_TOTAL_FIELDS)
1314 return 0;
1315
1316 memset(stats, 0, sizeof(*stats) * len);
1317
1318 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1319 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1320 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1321 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1322 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1323 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1324 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1325 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1326 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1327 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1328 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1329 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1330 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1331 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1332 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1333 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1334 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1335 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1336 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1337 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1338 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1339 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001340 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001341
1342 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1343 if (px->mode == PR_MODE_HTTP) {
1344 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1345 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1346 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1347 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1348 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1349 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1350 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
1351 }
1352
1353 /* requests : req_rate, req_rate_max, req_tot, */
1354 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1355 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1356 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1357
1358 /* compression: in, out, bypassed, responses */
1359 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1360 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1361 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1362 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1363
1364 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1365 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1366 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1367 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1368
1369 return 1;
1370}
1371
1372/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1373 * the state from stream interface <si>. The caller is responsible for clearing
1374 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1375 */
1376static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1377{
1378 struct appctx *appctx = __objt_appctx(si->end);
1379
1380 if (!(px->cap & PR_CAP_FE))
1381 return 0;
1382
1383 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1384 return 0;
1385
1386 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1387 return 0;
1388
1389 return stats_dump_one_line(stats, 0, px, appctx);
1390}
1391
1392/* Fill <stats> with the listener statistics. <stats> is
1393 * preallocated array of length <len>. The length of the array
1394 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1395 * then this value, the function returns 0, otherwise, it
1396 * returns 1. <flags> can take the value ST_SHLGNDS.
1397 */
1398int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1399 struct field *stats, int len)
1400{
Willy Tarreau83061a82018-07-13 11:56:34 +02001401 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001402
1403 if (len < ST_F_TOTAL_FIELDS)
1404 return 0;
1405
1406 if (!l->counters)
1407 return 0;
1408
1409 chunk_reset(out);
1410 memset(stats, 0, sizeof(*stats) * len);
1411
1412 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1413 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1414 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1415 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1416 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1417 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1418 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1419 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1420 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1421 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1422 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1423 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1424 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1425 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1426 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
1427 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1428 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1429 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1430 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001431 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001432
1433 if (flags & ST_SHLGNDS) {
1434 char str[INET6_ADDRSTRLEN];
1435 int port;
1436
1437 port = get_host_port(&l->addr);
1438 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1439 case AF_INET:
1440 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1441 chunk_appendf(out, "%s:%d", str, port);
1442 break;
1443 case AF_INET6:
1444 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1445 chunk_appendf(out, "[%s]:%d", str, port);
1446 break;
1447 case AF_UNIX:
1448 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1449 break;
1450 case -1:
1451 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1452 chunk_strcat(out, strerror(errno));
1453 break;
1454 default: /* address family not supported */
1455 break;
1456 }
1457 }
1458
1459 return 1;
1460}
1461
1462/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1463 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1464 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1465 * otherwise.
1466 */
1467static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1468{
1469 struct appctx *appctx = __objt_appctx(si->end);
1470
1471 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1472 return 0;
1473
1474 return stats_dump_one_line(stats, flags, px, appctx);
1475}
1476
1477enum srv_stats_state {
1478 SRV_STATS_STATE_DOWN = 0,
1479 SRV_STATS_STATE_DOWN_AGENT,
1480 SRV_STATS_STATE_GOING_UP,
1481 SRV_STATS_STATE_UP_GOING_DOWN,
1482 SRV_STATS_STATE_UP,
1483 SRV_STATS_STATE_NOLB_GOING_DOWN,
1484 SRV_STATS_STATE_NOLB,
1485 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1486 SRV_STATS_STATE_DRAIN,
1487 SRV_STATS_STATE_DRAIN_AGENT,
1488 SRV_STATS_STATE_NO_CHECK,
1489
1490 SRV_STATS_STATE_COUNT, /* Must be last */
1491};
1492
1493static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1494 [SRV_STATS_STATE_DOWN] = "DOWN",
1495 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1496 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1497 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1498 [SRV_STATS_STATE_UP] = "UP",
1499 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1500 [SRV_STATS_STATE_NOLB] = "NOLB",
1501 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1502 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1503 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1504 [SRV_STATS_STATE_NO_CHECK] = "no check"
1505};
1506
1507/* Fill <stats> with the server statistics. <stats> is
1508 * preallocated array of length <len>. The length of the array
1509 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1510 * then this value, the function returns 0, otherwise, it
1511 * returns 1. <flags> can take the value ST_SHLGNDS.
1512 */
1513int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1514 struct field *stats, int len)
1515{
1516 struct server *via, *ref;
1517 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001518 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001519 enum srv_stats_state state;
1520 char *fld_status;
1521
1522 if (len < ST_F_TOTAL_FIELDS)
1523 return 0;
1524
1525 memset(stats, 0, sizeof(*stats) * len);
1526
1527 /* we have "via" which is the tracked server as described in the configuration,
1528 * and "ref" which is the checked server and the end of the chain.
1529 */
1530 via = sv->track ? sv->track : sv;
1531 ref = via;
1532 while (ref->track)
1533 ref = ref->track;
1534
Emeric Brun52a91d32017-08-31 14:41:55 +02001535 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001536 if ((ref->check.state & CHK_ST_ENABLED) &&
1537 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1538 state = SRV_STATS_STATE_UP_GOING_DOWN;
1539 } else {
1540 state = SRV_STATS_STATE_UP;
1541 }
1542
Emeric Brun52a91d32017-08-31 14:41:55 +02001543 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001544 if (ref->agent.state & CHK_ST_ENABLED)
1545 state = SRV_STATS_STATE_DRAIN_AGENT;
1546 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1547 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1548 else
1549 state = SRV_STATS_STATE_DRAIN;
1550 }
1551
1552 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1553 state = SRV_STATS_STATE_NO_CHECK;
1554 }
1555 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001556 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001557 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1558 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1559 state = SRV_STATS_STATE_NOLB;
1560 } else {
1561 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1562 }
1563 }
1564 else { /* stopped */
1565 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1566 state = SRV_STATS_STATE_DOWN_AGENT;
1567 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1568 state = SRV_STATS_STATE_DOWN; /* DOWN */
1569 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1570 state = SRV_STATS_STATE_GOING_UP;
1571 } else {
1572 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1573 }
1574 }
1575
1576 chunk_reset(out);
1577
1578 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1579 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1580 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1581 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1582 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1583 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1584 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1585
1586 if (sv->maxconn)
1587 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1588
1589 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1590 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1591 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1592 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1593 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1594 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1595 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1596 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001597 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001598
1599 /* status */
1600 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001601 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001602 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001603 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001604 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001605 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001606 chunk_appendf(out, "MAINT");
1607 else
1608 chunk_appendf(out,
1609 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001610 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1611 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001612
1613 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1614 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001615 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 +01001616 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1617 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1618
1619 /* check failures: unique, fatal; last change, total downtime */
1620 if (sv->check.state & CHK_ST_ENABLED) {
1621 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1622 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1623 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1624 }
1625
1626 if (sv->maxqueue)
1627 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1628
1629 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1630 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1631 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1632
Emeric Brun52a91d32017-08-31 14:41:55 +02001633 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001634 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1635
1636 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1637
1638 if (sv->track) {
1639 char *fld_track = chunk_newstr(out);
1640
1641 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1642 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1643 }
1644
1645 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1646 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1647 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1648
1649 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1650 const char *fld_chksts;
1651
1652 fld_chksts = chunk_newstr(out);
1653 chunk_strcat(out, "* "); // for check in progress
1654 chunk_strcat(out, get_check_status_info(sv->check.status));
1655 if (!(sv->check.state & CHK_ST_INPROGRESS))
1656 fld_chksts += 2; // skip "* "
1657 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1658
1659 if (sv->check.status >= HCHK_STATUS_L57DATA)
1660 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1661
1662 if (sv->check.status >= HCHK_STATUS_CHECKED)
1663 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1664
1665 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1666 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1667 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1668 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1669 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1670 }
1671
1672 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1673 const char *fld_chksts;
1674
1675 fld_chksts = chunk_newstr(out);
1676 chunk_strcat(out, "* "); // for check in progress
1677 chunk_strcat(out, get_check_status_info(sv->agent.status));
1678 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1679 fld_chksts += 2; // skip "* "
1680 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1681
1682 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1683 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1684
1685 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1686 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1687
1688 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1689 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1690 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1691 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1692 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1693 }
1694
1695 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1696 if (px->mode == PR_MODE_HTTP) {
1697 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1698 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1699 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1700 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1701 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1702 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1703 }
1704
1705 if (ref->observe)
1706 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1707
1708 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1709 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1710 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1711
1712 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1713 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1714 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1715 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1716
1717 if (flags & ST_SHLGNDS) {
1718 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1719 case AF_INET:
1720 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001721 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001722 break;
1723 case AF_INET6:
1724 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001725 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001726 break;
1727 case AF_UNIX:
1728 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1729 break;
1730 case -1:
1731 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1732 chunk_strcat(out, strerror(errno));
1733 break;
1734 default: /* address family not supported */
1735 break;
1736 }
1737
1738 if (sv->cookie)
1739 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1740 }
1741
1742 return 1;
1743}
1744
1745/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1746 * from stream interface <si>, stats flags <flags>, and server state <state>.
1747 * The caller is responsible for clearing the trash if needed. Returns non-zero
1748 * if it emits anything, zero otherwise.
1749 */
1750static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1751{
1752 struct appctx *appctx = __objt_appctx(si->end);
1753
1754 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1755 return 0;
1756
1757 return stats_dump_one_line(stats, flags, px, appctx);
1758}
1759
1760/* Fill <stats> with the backend statistics. <stats> is
1761 * preallocated array of length <len>. The length of the array
1762 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1763 * then this value, the function returns 0, otherwise, it
1764 * returns 1. <flags> can take the value ST_SHLGNDS.
1765 */
1766int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1767{
1768 if (len < ST_F_TOTAL_FIELDS)
1769 return 0;
1770
1771 memset(stats, 0, sizeof(*stats) * len);
1772
1773 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1774 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1775 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1776 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1777 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001778 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001779 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1780 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1781 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1782 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1783 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1784 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1785 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1786 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1787 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1788 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1789 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001790 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001791 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1792 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1793 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1794 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1795 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1796 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1797 if (px->srv)
1798 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1799
1800 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1801 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1802 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1803 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1804 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1805 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1806 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1807
1808 if (flags & ST_SHLGNDS) {
1809 if (px->cookie_name)
1810 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1811 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1812 }
1813
1814 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1815 if (px->mode == PR_MODE_HTTP) {
1816 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1817 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1818 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1819 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1820 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1821 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1822 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001823 }
1824
1825 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1826 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1827
1828 /* compression: in, out, bypassed, responses */
1829 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1830 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1831 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1832 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1833 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1834
1835 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1836 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1837 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1838 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1839
1840 return 1;
1841}
1842
1843/* Dumps a line for backend <px> to the trash for and uses the state from stream
1844 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1845 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1846 */
1847static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1848{
1849 struct appctx *appctx = __objt_appctx(si->end);
1850
1851 if (!(px->cap & PR_CAP_BE))
1852 return 0;
1853
1854 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1855 return 0;
1856
1857 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1858 return 0;
1859
1860 return stats_dump_one_line(stats, flags, px, appctx);
1861}
1862
1863/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1864 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1865 * for clearing the trash if needed.
1866 */
1867static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1868{
1869 struct appctx *appctx = __objt_appctx(si->end);
1870 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1871
1872 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1873 /* A form to enable/disable this proxy servers */
1874
1875 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1876 scope_txt[0] = 0;
1877 if (appctx->ctx.stats.scope_len) {
1878 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02001879 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 +01001880 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1881 }
1882
1883 chunk_appendf(&trash,
1884 "<form method=\"post\">");
1885 }
1886
1887 /* print a new table */
1888 chunk_appendf(&trash,
1889 "<table class=\"tbl\" width=\"100%%\">\n"
1890 "<tr class=\"titre\">"
1891 "<th class=\"pxname\" width=\"10%%\">");
1892
1893 chunk_appendf(&trash,
1894 "<a name=\"%s\"></a>%s"
1895 "<a class=px href=\"#%s\">%s</a>",
1896 px->id,
1897 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1898 px->id, px->id);
1899
1900 if (uri->flags & ST_SHLGNDS) {
1901 /* cap, mode, id */
1902 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1903 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1904 px->uuid);
1905 chunk_appendf(&trash, "</div>");
1906 }
1907
1908 chunk_appendf(&trash,
1909 "%s</th>"
1910 "<th class=\"%s\" width=\"90%%\">%s</th>"
1911 "</tr>\n"
1912 "</table>\n"
1913 "<table class=\"tbl\" width=\"100%%\">\n"
1914 "<tr class=\"titre\">",
1915 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1916 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1917
1918 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1919 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00001920 chunk_appendf(&trash,
1921 "<th rowspan=2 width=1><input type=\"checkbox\" \
1922 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
1923 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
1924 px->id,
1925 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01001926 }
1927
1928 chunk_appendf(&trash,
1929 "<th rowspan=2></th>"
1930 "<th colspan=3>Queue</th>"
1931 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
1932 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
1933 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
1934 "<th colspan=9>Server</th>"
1935 "</tr>\n"
1936 "<tr class=\"titre\">"
1937 "<th>Cur</th><th>Max</th><th>Limit</th>"
1938 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
1939 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
1940 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
1941 "<th>Resp</th><th>Retr</th><th>Redis</th>"
1942 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
1943 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1944 "<th>Thrtle</th>\n"
1945 "</tr>");
1946}
1947
1948/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
1949 * stream interface <si>. The caller is responsible for clearing the trash if needed.
1950 */
1951static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
1952{
1953 struct appctx *appctx = __objt_appctx(si->end);
1954 chunk_appendf(&trash, "</table>");
1955
1956 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1957 /* close the form used to enable/disable this proxy servers */
1958 chunk_appendf(&trash,
1959 "Choose the action to perform on the checked servers : "
1960 "<select name=action>"
1961 "<option value=\"\"></option>"
1962 "<option value=\"ready\">Set state to READY</option>"
1963 "<option value=\"drain\">Set state to DRAIN</option>"
1964 "<option value=\"maint\">Set state to MAINT</option>"
1965 "<option value=\"dhlth\">Health: disable checks</option>"
1966 "<option value=\"ehlth\">Health: enable checks</option>"
1967 "<option value=\"hrunn\">Health: force UP</option>"
1968 "<option value=\"hnolb\">Health: force NOLB</option>"
1969 "<option value=\"hdown\">Health: force DOWN</option>"
1970 "<option value=\"dagent\">Agent: disable checks</option>"
1971 "<option value=\"eagent\">Agent: enable checks</option>"
1972 "<option value=\"arunn\">Agent: force UP</option>"
1973 "<option value=\"adown\">Agent: force DOWN</option>"
1974 "<option value=\"shutdown\">Kill Sessions</option>"
1975 "</select>"
1976 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
1977 "&nbsp;<input type=\"submit\" value=\"Apply\">"
1978 "</form>",
1979 px->uuid);
1980 }
1981
1982 chunk_appendf(&trash, "<p>\n");
1983}
1984
1985/*
1986 * Dumps statistics for a proxy. The output is sent to the stream interface's
1987 * input buffer. Returns 0 if it had to stop dumping data because of lack of
1988 * buffer space, or non-zero if everything completed. This function is used
1989 * both by the CLI and the HTTP entry points, and is able to dump the output
1990 * in HTML or CSV formats. If the later, <uri> must be NULL.
1991 */
Christopher Fauletef779222018-10-31 08:47:01 +01001992int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
1993 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01001994{
1995 struct appctx *appctx = __objt_appctx(si->end);
1996 struct stream *s = si_strm(si);
1997 struct channel *rep = si_ic(si);
1998 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
1999 struct listener *l;
2000 unsigned int flags;
2001
2002 if (uri)
2003 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002004 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002005 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2006 else
2007 flags = ST_SHNODE | ST_SHDESC;
2008
2009 chunk_reset(&trash);
2010
2011 switch (appctx->ctx.stats.px_st) {
2012 case STAT_PX_ST_INIT:
2013 /* we are on a new proxy */
2014 if (uri && uri->scope) {
2015 /* we have a limited scope, we have to check the proxy name */
2016 struct stat_scope *scope;
2017 int len;
2018
2019 len = strlen(px->id);
2020 scope = uri->scope;
2021
2022 while (scope) {
2023 /* match exact proxy name */
2024 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2025 break;
2026
2027 /* match '.' which means 'self' proxy */
2028 if (!strcmp(scope->px_id, ".") && px == s->be)
2029 break;
2030 scope = scope->next;
2031 }
2032
2033 /* proxy name not found : don't dump anything */
2034 if (scope == NULL)
2035 return 1;
2036 }
2037
2038 /* if the user has requested a limited output and the proxy
2039 * name does not match, skip it.
2040 */
2041 if (appctx->ctx.stats.scope_len &&
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002042 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 +01002043 return 1;
2044
2045 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2046 (appctx->ctx.stats.iid != -1) &&
2047 (px->uuid != appctx->ctx.stats.iid))
2048 return 1;
2049
2050 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2051 /* fall through */
2052
2053 case STAT_PX_ST_TH:
2054 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2055 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002056 if (!stats_putchk(rep, htx, &trash))
2057 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002058 }
2059
2060 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2061 /* fall through */
2062
2063 case STAT_PX_ST_FE:
2064 /* print the frontend */
2065 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002066 if (!stats_putchk(rep, htx, &trash))
2067 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002068 }
2069
2070 appctx->ctx.stats.l = px->conf.listeners.n;
2071 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2072 /* fall through */
2073
2074 case STAT_PX_ST_LI:
2075 /* stats.l has been initialized above */
2076 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002077 if (htx) {
2078 if (htx_almost_full(htx))
2079 goto full;
2080 }
2081 else {
2082 if (buffer_almost_full(&rep->buf))
2083 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002084 }
2085
2086 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2087 if (!l->counters)
2088 continue;
2089
2090 if (appctx->ctx.stats.flags & STAT_BOUND) {
2091 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2092 break;
2093
2094 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2095 continue;
2096 }
2097
2098 /* print the frontend */
2099 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002100 if (!stats_putchk(rep, htx, &trash))
2101 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002102 }
2103 }
2104
2105 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2106 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2107 /* fall through */
2108
2109 case STAT_PX_ST_SV:
2110 /* stats.sv has been initialized above */
2111 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002112 if (htx) {
2113 if (htx_almost_full(htx))
2114 goto full;
2115 }
2116 else {
2117 if (buffer_almost_full(&rep->buf))
2118 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002119 }
2120
2121 sv = appctx->ctx.stats.sv;
2122
2123 if (appctx->ctx.stats.flags & STAT_BOUND) {
2124 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2125 break;
2126
2127 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2128 continue;
2129 }
2130
2131 svs = sv;
2132 while (svs->track)
2133 svs = svs->track;
2134
2135 /* do not report servers which are DOWN and not changing state */
2136 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002137 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2138 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002139 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2140 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2141 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2142 continue;
2143 }
2144
2145 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002146 if (!stats_putchk(rep, htx, &trash))
2147 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002148 }
2149 } /* for sv */
2150
2151 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2152 /* fall through */
2153
2154 case STAT_PX_ST_BE:
2155 /* print the backend */
2156 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002157 if (!stats_putchk(rep, htx, &trash))
2158 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002159 }
2160
2161 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2162 /* fall through */
2163
2164 case STAT_PX_ST_END:
2165 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2166 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002167 if (!stats_putchk(rep, htx, &trash))
2168 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002169 }
2170
2171 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2172 /* fall through */
2173
2174 case STAT_PX_ST_FIN:
2175 return 1;
2176
2177 default:
2178 /* unknown state, we should put an abort() here ! */
2179 return 1;
2180 }
Christopher Fauletef779222018-10-31 08:47:01 +01002181
2182 full:
2183 si_rx_room_blk(si);
2184 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002185}
2186
2187/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2188 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2189 */
2190static void stats_dump_html_head(struct uri_auth *uri)
2191{
2192 /* WARNING! This must fit in the first buffer !!! */
2193 chunk_appendf(&trash,
2194 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2195 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2196 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2197 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2198 "<style type=\"text/css\"><!--\n"
2199 "body {"
2200 " font-family: arial, helvetica, sans-serif;"
2201 " font-size: 12px;"
2202 " font-weight: normal;"
2203 " color: black;"
2204 " background: white;"
2205 "}\n"
2206 "th,td {"
2207 " font-size: 10px;"
2208 "}\n"
2209 "h1 {"
2210 " font-size: x-large;"
2211 " margin-bottom: 0.5em;"
2212 "}\n"
2213 "h2 {"
2214 " font-family: helvetica, arial;"
2215 " font-size: x-large;"
2216 " font-weight: bold;"
2217 " font-style: italic;"
2218 " color: #6020a0;"
2219 " margin-top: 0em;"
2220 " margin-bottom: 0em;"
2221 "}\n"
2222 "h3 {"
2223 " font-family: helvetica, arial;"
2224 " font-size: 16px;"
2225 " font-weight: bold;"
2226 " color: #b00040;"
2227 " background: #e8e8d0;"
2228 " margin-top: 0em;"
2229 " margin-bottom: 0em;"
2230 "}\n"
2231 "li {"
2232 " margin-top: 0.25em;"
2233 " margin-right: 2em;"
2234 "}\n"
2235 ".hr {margin-top: 0.25em;"
2236 " border-color: black;"
2237 " border-bottom-style: solid;"
2238 "}\n"
2239 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2240 ".total {background: #20D0D0;color: #ffff80;}\n"
2241 ".frontend {background: #e8e8d0;}\n"
2242 ".socket {background: #d0d0d0;}\n"
2243 ".backend {background: #e8e8d0;}\n"
2244 ".active_down {background: #ff9090;}\n"
2245 ".active_going_up {background: #ffd020;}\n"
2246 ".active_going_down {background: #ffffa0;}\n"
2247 ".active_up {background: #c0ffc0;}\n"
2248 ".active_nolb {background: #20a0ff;}\n"
2249 ".active_draining {background: #20a0FF;}\n"
2250 ".active_no_check {background: #e0e0e0;}\n"
2251 ".backup_down {background: #ff9090;}\n"
2252 ".backup_going_up {background: #ff80ff;}\n"
2253 ".backup_going_down {background: #c060ff;}\n"
2254 ".backup_up {background: #b0d0ff;}\n"
2255 ".backup_nolb {background: #90b0e0;}\n"
2256 ".backup_draining {background: #cc9900;}\n"
2257 ".backup_no_check {background: #e0e0e0;}\n"
2258 ".maintain {background: #c07820;}\n"
2259 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2260 "\n"
2261 "a.px:link {color: #ffff40; text-decoration: none;}"
2262 "a.px:visited {color: #ffff40; text-decoration: none;}"
2263 "a.px:hover {color: #ffffff; text-decoration: none;}"
2264 "a.lfsb:link {color: #000000; text-decoration: none;}"
2265 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2266 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2267 "\n"
2268 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2269 "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"
2270 "table.tbl td.ac { text-align: center;}\n"
2271 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2272 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2273 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2274 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2275 "\n"
2276 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2277 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2278 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2279 "table.det { border-collapse: collapse; border-style: none; }\n"
2280 "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"
2281 "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"
2282 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2283 "div.tips {\n"
2284 " display:block;\n"
2285 " visibility:hidden;\n"
2286 " z-index:2147483647;\n"
2287 " position:absolute;\n"
2288 " padding:2px 4px 3px;\n"
2289 " background:#f0f060; color:#000000;\n"
2290 " border:1px solid #7040c0;\n"
2291 " white-space:nowrap;\n"
2292 " font-style:normal;font-size:11px;font-weight:normal;\n"
2293 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2294 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2295 "}\n"
2296 "u:hover div.tips {visibility:visible;}\n"
2297 "-->\n"
2298 "</style></head>\n",
2299 (uri->flags & ST_SHNODE) ? " on " : "",
2300 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2301 );
2302}
2303
2304/* Dumps the HTML stats information block to the trash for and uses the state from
2305 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2306 * for clearing the trash if needed.
2307 */
2308static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2309{
2310 struct appctx *appctx = __objt_appctx(si->end);
2311 unsigned int up = (now.tv_sec - start_date.tv_sec);
2312 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2313
2314 /* WARNING! this has to fit the first packet too.
2315 * We are around 3.5 kB, add adding entries will
2316 * become tricky if we want to support 4kB buffers !
2317 */
2318 chunk_appendf(&trash,
2319 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2320 PRODUCT_NAME "%s</a></h1>\n"
2321 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2322 "<hr width=\"100%%\" class=\"hr\">\n"
2323 "<h3>&gt; General process information</h3>\n"
2324 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002325 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002326 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2327 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2328 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
2329 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
2330 "Running tasks: %d/%d; idle = %d %%<br>\n"
2331 "</td><td align=\"center\" nowrap>\n"
2332 "<table class=\"lgd\"><tr>\n"
2333 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2334 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2335 "</tr><tr>\n"
2336 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2337 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2338 "</tr><tr>\n"
2339 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2340 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2341 "</tr><tr>\n"
2342 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2343 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2344 "</tr><tr>\n"
2345 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2346 "</tr><tr>\n"
2347 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2348 "</tr></table>\n"
2349 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2350 "</td>"
2351 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2352 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2353 "",
2354 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
2355 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2356 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2357 (uri->flags & ST_SHDESC) ? ": " : "",
2358 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002359 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002360 up / 86400, (up % 86400) / 3600,
2361 (up % 3600) / 60, (up % 60),
2362 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2363 global.rlimit_memmax ? " MB" : "",
2364 global.rlimit_nofile,
2365 global.maxsock, global.maxconn, global.maxpipes,
2366 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002367 tasks_run_queue_cur, nb_tasks_cur, idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002368 );
2369
2370 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002371 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 +01002372 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2373
2374 chunk_appendf(&trash,
2375 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2376 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2377 STAT_SCOPE_TXT_MAXLEN);
2378
2379 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2380 scope_txt[0] = 0;
2381 if (appctx->ctx.stats.scope_len) {
2382 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002383 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 +01002384 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2385 }
2386
2387 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2388 chunk_appendf(&trash,
2389 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2390 uri->uri_prefix,
2391 "",
2392 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2393 scope_txt);
2394 else
2395 chunk_appendf(&trash,
2396 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2397 uri->uri_prefix,
2398 ";up",
2399 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2400 scope_txt);
2401
2402 if (uri->refresh > 0) {
2403 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2404 chunk_appendf(&trash,
2405 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2406 uri->uri_prefix,
2407 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2408 "",
2409 scope_txt);
2410 else
2411 chunk_appendf(&trash,
2412 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2413 uri->uri_prefix,
2414 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2415 ";norefresh",
2416 scope_txt);
2417 }
2418
2419 chunk_appendf(&trash,
2420 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2421 uri->uri_prefix,
2422 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2423 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2424 scope_txt);
2425
2426 chunk_appendf(&trash,
2427 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2428 uri->uri_prefix,
2429 (uri->refresh > 0) ? ";norefresh" : "",
2430 scope_txt);
2431
2432 chunk_appendf(&trash,
2433 "</ul></td>"
2434 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2435 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2436 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2437 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2438 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2439 "</ul>"
2440 "</td>"
2441 "</tr></table>\n"
2442 ""
2443 );
2444
2445 if (appctx->ctx.stats.st_code) {
2446 switch (appctx->ctx.stats.st_code) {
2447 case STAT_STATUS_DONE:
2448 chunk_appendf(&trash,
2449 "<p><div class=active_up>"
2450 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2451 "Action processed successfully."
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_NONE:
2458 chunk_appendf(&trash,
2459 "<p><div class=active_going_down>"
2460 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2461 "Nothing has changed."
2462 "</div>\n", uri->uri_prefix,
2463 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2464 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2465 scope_txt);
2466 break;
2467 case STAT_STATUS_PART:
2468 chunk_appendf(&trash,
2469 "<p><div class=active_going_down>"
2470 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2471 "Action partially processed.<br>"
2472 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2473 "</div>\n", uri->uri_prefix,
2474 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2475 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2476 scope_txt);
2477 break;
2478 case STAT_STATUS_ERRP:
2479 chunk_appendf(&trash,
2480 "<p><div class=active_down>"
2481 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2482 "Action not processed because of invalid parameters."
2483 "<ul>"
2484 "<li>The action is maybe unknown.</li>"
2485 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2486 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2487 "</ul>"
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 case STAT_STATUS_EXCD:
2494 chunk_appendf(&trash,
2495 "<p><div class=active_down>"
2496 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2497 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2498 "You should retry with less servers at a time.</b>"
2499 "</div>\n", uri->uri_prefix,
2500 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2501 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2502 scope_txt);
2503 break;
2504 case STAT_STATUS_DENY:
2505 chunk_appendf(&trash,
2506 "<p><div class=active_down>"
2507 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2508 "<b>Action denied.</b>"
2509 "</div>\n", uri->uri_prefix,
2510 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2511 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2512 scope_txt);
2513 break;
2514 default:
2515 chunk_appendf(&trash,
2516 "<p><div class=active_no_check>"
2517 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2518 "Unexpected result."
2519 "</div>\n", uri->uri_prefix,
2520 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2521 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2522 scope_txt);
2523 }
2524 chunk_appendf(&trash, "<p>\n");
2525 }
2526}
2527
2528/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2529 * for clearing the trash if needed.
2530 */
2531static void stats_dump_html_end()
2532{
2533 chunk_appendf(&trash, "</body></html>\n");
2534}
2535
Simon Horman05ee2132017-01-04 09:37:25 +01002536/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2537 * for clearing it if needed.
2538 */
2539static void stats_dump_json_header()
2540{
2541 chunk_strcat(&trash, "[");
2542}
2543
2544
2545/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2546 * for clearing the trash if needed.
2547 */
2548static void stats_dump_json_end()
2549{
2550 chunk_strcat(&trash, "]");
2551}
2552
William Lallemand74c24fb2016-11-21 17:18:36 +01002553/* This function dumps statistics onto the stream interface's read buffer in
2554 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2555 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2556 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2557 * and the stream must be closed, or -1 in case of any error. This function is
2558 * used by both the CLI and the HTTP handlers.
2559 */
Christopher Fauletef779222018-10-31 08:47:01 +01002560static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2561 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002562{
2563 struct appctx *appctx = __objt_appctx(si->end);
2564 struct channel *rep = si_ic(si);
2565 struct proxy *px;
2566
2567 chunk_reset(&trash);
2568
2569 switch (appctx->st2) {
2570 case STAT_ST_INIT:
2571 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2572 /* fall through */
2573
2574 case STAT_ST_HEAD:
2575 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2576 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002577 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002578 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002579 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2580 stats_dump_csv_header();
2581
Christopher Fauletef779222018-10-31 08:47:01 +01002582 if (!stats_putchk(rep, htx, &trash))
2583 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002584
2585 appctx->st2 = STAT_ST_INFO;
2586 /* fall through */
2587
2588 case STAT_ST_INFO:
2589 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2590 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002591 if (!stats_putchk(rep, htx, &trash))
2592 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002593 }
2594
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002595 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002596 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2597 appctx->st2 = STAT_ST_LIST;
2598 /* fall through */
2599
2600 case STAT_ST_LIST:
2601 /* dump proxies */
2602 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002603 if (htx) {
2604 if (htx_almost_full(htx))
2605 goto full;
2606 }
2607 else {
2608 if (buffer_almost_full(&rep->buf))
2609 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002610 }
2611
2612 px = appctx->ctx.stats.px;
2613 /* skip the disabled proxies, global frontend and non-networked ones */
2614 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002615 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002616 return 0;
2617
2618 appctx->ctx.stats.px = px->next;
2619 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2620 }
2621 /* here, we just have reached the last proxy */
2622
2623 appctx->st2 = STAT_ST_END;
2624 /* fall through */
2625
2626 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002627 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2628 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2629 stats_dump_html_end();
2630 else
2631 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002632 if (!stats_putchk(rep, htx, &trash))
2633 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002634 }
2635
2636 appctx->st2 = STAT_ST_FIN;
2637 /* fall through */
2638
2639 case STAT_ST_FIN:
2640 return 1;
2641
2642 default:
2643 /* unknown state ! */
2644 appctx->st2 = STAT_ST_FIN;
2645 return -1;
2646 }
Christopher Fauletef779222018-10-31 08:47:01 +01002647
2648 full:
2649 si_rx_room_blk(si);
2650 return 0;
2651
William Lallemand74c24fb2016-11-21 17:18:36 +01002652}
2653
2654/* We reached the stats page through a POST request. The appctx is
2655 * expected to have already been allocated by the caller.
2656 * Parse the posted data and enable/disable servers if necessary.
2657 * Returns 1 if request was parsed or zero if it needs more data.
2658 */
2659static int stats_process_http_post(struct stream_interface *si)
2660{
2661 struct stream *s = si_strm(si);
2662 struct appctx *appctx = objt_appctx(si->end);
2663
2664 struct proxy *px = NULL;
2665 struct server *sv = NULL;
2666
2667 char key[LINESIZE];
2668 int action = ST_ADM_ACTION_NONE;
2669 int reprocess = 0;
2670
2671 int total_servers = 0;
2672 int altered_servers = 0;
2673
2674 char *first_param, *cur_param, *next_param, *end_params;
2675 char *st_cur_param = NULL;
2676 char *st_next_param = NULL;
2677
Willy Tarreau83061a82018-07-13 11:56:34 +02002678 struct buffer *temp;
William Lallemand74c24fb2016-11-21 17:18:36 +01002679 int reql;
2680
2681 temp = get_trash_chunk();
2682 if (temp->size < s->txn->req.body_len) {
2683 /* too large request */
2684 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2685 goto out;
2686 }
2687
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002688 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2689 s->txn->req.eoh + 2);
William Lallemand74c24fb2016-11-21 17:18:36 +01002690 if (reql <= 0) {
2691 /* we need more data */
2692 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2693 return 0;
2694 }
2695
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002696 first_param = temp->area;
2697 end_params = temp->area + reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002698 cur_param = next_param = end_params;
2699 *end_params = '\0';
2700
2701 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2702
2703 /*
2704 * Parse the parameters in reverse order to only store the last value.
2705 * From the html form, the backend and the action are at the end.
2706 */
2707 while (cur_param > first_param) {
2708 char *value;
2709 int poffset, plen;
2710
2711 cur_param--;
2712
2713 if ((*cur_param == '&') || (cur_param == first_param)) {
2714 reprocess_servers:
2715 /* Parse the key */
2716 poffset = (cur_param != first_param ? 1 : 0);
2717 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2718 if ((plen > 0) && (plen <= sizeof(key))) {
2719 strncpy(key, cur_param + poffset, plen);
2720 key[plen - 1] = '\0';
2721 } else {
2722 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2723 goto out;
2724 }
2725
2726 /* Parse the value */
2727 value = key;
2728 while (*value != '\0' && *value != '=') {
2729 value++;
2730 }
2731 if (*value == '=') {
2732 /* Ok, a value is found, we can mark the end of the key */
2733 *value++ = '\0';
2734 }
2735 if (url_decode(key) < 0 || url_decode(value) < 0)
2736 break;
2737
2738 /* Now we can check the key to see what to do */
2739 if (!px && (strcmp(key, "b") == 0)) {
2740 if ((px = proxy_be_by_name(value)) == NULL) {
2741 /* the backend name is unknown or ambiguous (duplicate names) */
2742 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2743 goto out;
2744 }
2745 }
2746 else if (!action && (strcmp(key, "action") == 0)) {
2747 if (strcmp(value, "ready") == 0) {
2748 action = ST_ADM_ACTION_READY;
2749 }
2750 else if (strcmp(value, "drain") == 0) {
2751 action = ST_ADM_ACTION_DRAIN;
2752 }
2753 else if (strcmp(value, "maint") == 0) {
2754 action = ST_ADM_ACTION_MAINT;
2755 }
2756 else if (strcmp(value, "shutdown") == 0) {
2757 action = ST_ADM_ACTION_SHUTDOWN;
2758 }
2759 else if (strcmp(value, "dhlth") == 0) {
2760 action = ST_ADM_ACTION_DHLTH;
2761 }
2762 else if (strcmp(value, "ehlth") == 0) {
2763 action = ST_ADM_ACTION_EHLTH;
2764 }
2765 else if (strcmp(value, "hrunn") == 0) {
2766 action = ST_ADM_ACTION_HRUNN;
2767 }
2768 else if (strcmp(value, "hnolb") == 0) {
2769 action = ST_ADM_ACTION_HNOLB;
2770 }
2771 else if (strcmp(value, "hdown") == 0) {
2772 action = ST_ADM_ACTION_HDOWN;
2773 }
2774 else if (strcmp(value, "dagent") == 0) {
2775 action = ST_ADM_ACTION_DAGENT;
2776 }
2777 else if (strcmp(value, "eagent") == 0) {
2778 action = ST_ADM_ACTION_EAGENT;
2779 }
2780 else if (strcmp(value, "arunn") == 0) {
2781 action = ST_ADM_ACTION_ARUNN;
2782 }
2783 else if (strcmp(value, "adown") == 0) {
2784 action = ST_ADM_ACTION_ADOWN;
2785 }
2786 /* else these are the old supported methods */
2787 else if (strcmp(value, "disable") == 0) {
2788 action = ST_ADM_ACTION_DISABLE;
2789 }
2790 else if (strcmp(value, "enable") == 0) {
2791 action = ST_ADM_ACTION_ENABLE;
2792 }
2793 else if (strcmp(value, "stop") == 0) {
2794 action = ST_ADM_ACTION_STOP;
2795 }
2796 else if (strcmp(value, "start") == 0) {
2797 action = ST_ADM_ACTION_START;
2798 }
2799 else {
2800 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2801 goto out;
2802 }
2803 }
2804 else if (strcmp(key, "s") == 0) {
2805 if (!(px && action)) {
2806 /*
2807 * Indicates that we'll need to reprocess the parameters
2808 * as soon as backend and action are known
2809 */
2810 if (!reprocess) {
2811 st_cur_param = cur_param;
2812 st_next_param = next_param;
2813 }
2814 reprocess = 1;
2815 }
2816 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002817 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002818 switch (action) {
2819 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002820 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002821 altered_servers++;
2822 total_servers++;
2823 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2824 }
2825 break;
2826 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002827 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002828 altered_servers++;
2829 total_servers++;
2830 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2831 }
2832 break;
2833 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002834 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002835 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2836 altered_servers++;
2837 total_servers++;
2838 }
2839 break;
2840 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002841 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002842 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2843 altered_servers++;
2844 total_servers++;
2845 }
2846 break;
2847 case ST_ADM_ACTION_DHLTH:
2848 if (sv->check.state & CHK_ST_CONFIGURED) {
2849 sv->check.state &= ~CHK_ST_ENABLED;
2850 altered_servers++;
2851 total_servers++;
2852 }
2853 break;
2854 case ST_ADM_ACTION_EHLTH:
2855 if (sv->check.state & CHK_ST_CONFIGURED) {
2856 sv->check.state |= CHK_ST_ENABLED;
2857 altered_servers++;
2858 total_servers++;
2859 }
2860 break;
2861 case ST_ADM_ACTION_HRUNN:
2862 if (!(sv->track)) {
2863 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002864 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002865 altered_servers++;
2866 total_servers++;
2867 }
2868 break;
2869 case ST_ADM_ACTION_HNOLB:
2870 if (!(sv->track)) {
2871 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002872 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002873 altered_servers++;
2874 total_servers++;
2875 }
2876 break;
2877 case ST_ADM_ACTION_HDOWN:
2878 if (!(sv->track)) {
2879 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002880 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002881 altered_servers++;
2882 total_servers++;
2883 }
2884 break;
2885 case ST_ADM_ACTION_DAGENT:
2886 if (sv->agent.state & CHK_ST_CONFIGURED) {
2887 sv->agent.state &= ~CHK_ST_ENABLED;
2888 altered_servers++;
2889 total_servers++;
2890 }
2891 break;
2892 case ST_ADM_ACTION_EAGENT:
2893 if (sv->agent.state & CHK_ST_CONFIGURED) {
2894 sv->agent.state |= CHK_ST_ENABLED;
2895 altered_servers++;
2896 total_servers++;
2897 }
2898 break;
2899 case ST_ADM_ACTION_ARUNN:
2900 if (sv->agent.state & CHK_ST_ENABLED) {
2901 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002902 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002903 altered_servers++;
2904 total_servers++;
2905 }
2906 break;
2907 case ST_ADM_ACTION_ADOWN:
2908 if (sv->agent.state & CHK_ST_ENABLED) {
2909 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002910 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002911 altered_servers++;
2912 total_servers++;
2913 }
2914 break;
2915 case ST_ADM_ACTION_READY:
2916 srv_adm_set_ready(sv);
2917 altered_servers++;
2918 total_servers++;
2919 break;
2920 case ST_ADM_ACTION_DRAIN:
2921 srv_adm_set_drain(sv);
2922 altered_servers++;
2923 total_servers++;
2924 break;
2925 case ST_ADM_ACTION_MAINT:
2926 srv_adm_set_maint(sv);
2927 altered_servers++;
2928 total_servers++;
2929 break;
2930 case ST_ADM_ACTION_SHUTDOWN:
2931 if (px->state != PR_STSTOPPED) {
2932 struct stream *sess, *sess_bck;
2933
2934 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2935 if (sess->srv_conn == sv)
2936 stream_shutdown(sess, SF_ERR_KILLED);
2937
2938 altered_servers++;
2939 total_servers++;
2940 }
2941 break;
2942 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002943 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002944 } else {
2945 /* the server name is unknown or ambiguous (duplicate names) */
2946 total_servers++;
2947 }
2948 }
2949 if (reprocess && px && action) {
2950 /* Now, we know the backend and the action chosen by the user.
2951 * We can safely restart from the first server parameter
2952 * to reprocess them
2953 */
2954 cur_param = st_cur_param;
2955 next_param = st_next_param;
2956 reprocess = 0;
2957 goto reprocess_servers;
2958 }
2959
2960 next_param = cur_param;
2961 }
2962 }
2963
2964 if (total_servers == 0) {
2965 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2966 }
2967 else if (altered_servers == 0) {
2968 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2969 }
2970 else if (altered_servers == total_servers) {
2971 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
2972 }
2973 else {
2974 appctx->ctx.stats.st_code = STAT_STATUS_PART;
2975 }
2976 out:
2977 return 1;
Christopher Fauletef779222018-10-31 08:47:01 +01002978}
2979
2980
2981static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
2982{
2983 struct stream *s = si_strm(si);
2984 struct uri_auth *uri = s->be->uri_auth;
2985 struct appctx *appctx = __objt_appctx(si->end);
2986 union h1_sl sl;
2987
2988 sl.st.status = 200;
2989 sl.st.v = ist("HTTP/1.1");
2990 sl.st.c = ist("200");
2991 sl.st.r = ist("OK");
2992
2993 if (!htx_add_resline(htx, sl))
2994 goto full;
2995
2996 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
2997 !htx_add_header(htx, ist("Connection"), ist("close")))
2998 goto full;
2999 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3000 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3001 goto full;
3002 }
3003 else {
3004 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3005 goto full;
3006 }
3007
3008 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3009 const char *refresh = U2A(uri->refresh);
3010 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3011 goto full;
3012 }
3013
3014 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3015 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3016 goto full;
3017 }
3018
3019 if (!htx_add_endof(htx, HTX_BLK_EOH))
3020 goto full;
3021
3022 return 1;
3023
3024 full:
3025 htx_reset(htx);
3026 si_rx_room_blk(si);
3027 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003028}
3029
Christopher Fauletef779222018-10-31 08:47:01 +01003030
3031static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx)
3032{
3033 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3034 struct stream *s = si_strm(si);
3035 struct uri_auth *uri = s->be->uri_auth;
3036 struct appctx *appctx = __objt_appctx(si->end);
3037 union h1_sl sl;
3038
3039 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3040 scope_txt[0] = 0;
3041 if (appctx->ctx.stats.scope_len) {
3042 strcpy(scope_txt, STAT_SCOPE_PATTERN);
3043 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), co_head(si_oc(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3044 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3045 }
3046
3047 /* We don't want to land on the posted stats page because a refresh will
3048 * repost the data. We don't want this to happen on accident so we redirect
3049 * the browse to the stats page with a GET.
3050 */
3051 chunk_printf(&trash, "%s;st=%s%s%s%s",
3052 uri->uri_prefix,
3053 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3054 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3055 stat_status_codes[appctx->ctx.stats.st_code]) ?
3056 stat_status_codes[appctx->ctx.stats.st_code] :
3057 stat_status_codes[STAT_STATUS_UNKN],
3058 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3059 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3060 scope_txt);
3061
3062 sl.st.status = 303;
3063 sl.st.v = ist("HTTP/1.1");
3064 sl.st.c = ist("303");
3065 sl.st.r = ist("See Other");
3066 if (!htx_add_resline(htx, sl))
3067 goto full;
3068
3069 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
3070 !htx_add_header(htx, ist("Connection"), ist("close")) ||
3071 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3072 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3073 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3074 goto full;
3075
3076 if (!htx_add_endof(htx, HTX_BLK_EOH))
3077 goto full;
3078
3079 return 1;
3080
3081full:
3082 htx_reset(htx);
3083 si_rx_room_blk(si);
3084 return 0;
3085}
William Lallemand74c24fb2016-11-21 17:18:36 +01003086
3087static int stats_send_http_headers(struct stream_interface *si)
3088{
3089 struct stream *s = si_strm(si);
3090 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003091 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003092
3093 chunk_printf(&trash,
3094 "HTTP/1.1 200 OK\r\n"
3095 "Cache-Control: no-cache\r\n"
3096 "Connection: close\r\n"
3097 "Content-Type: %s\r\n",
3098 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
3099
3100 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
3101 chunk_appendf(&trash, "Refresh: %d\r\n",
3102 uri->refresh);
3103
3104 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
3105
3106 if (appctx->ctx.stats.flags & STAT_CHUNKED)
3107 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
3108 else
3109 chunk_appendf(&trash, "\r\n");
3110
Willy Tarreau06d80a92017-10-19 14:32:15 +02003111 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003112 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003113 return 0;
3114 }
3115
3116 return 1;
3117}
3118
3119static int stats_send_http_redirect(struct stream_interface *si)
3120{
3121 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3122 struct stream *s = si_strm(si);
3123 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003124 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003125
3126 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3127 scope_txt[0] = 0;
3128 if (appctx->ctx.stats.scope_len) {
3129 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02003130 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 +01003131 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3132 }
3133
3134 /* We don't want to land on the posted stats page because a refresh will
3135 * repost the data. We don't want this to happen on accident so we redirect
3136 * the browse to the stats page with a GET.
3137 */
3138 chunk_printf(&trash,
3139 "HTTP/1.1 303 See Other\r\n"
3140 "Cache-Control: no-cache\r\n"
3141 "Content-Type: text/plain\r\n"
3142 "Connection: close\r\n"
3143 "Location: %s;st=%s%s%s%s\r\n"
3144 "Content-length: 0\r\n"
3145 "\r\n",
3146 uri->uri_prefix,
3147 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3148 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3149 stat_status_codes[appctx->ctx.stats.st_code]) ?
3150 stat_status_codes[appctx->ctx.stats.st_code] :
3151 stat_status_codes[STAT_STATUS_UNKN],
3152 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3153 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3154 scope_txt);
3155
Willy Tarreau06d80a92017-10-19 14:32:15 +02003156 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003157 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003158 return 0;
3159 }
3160
3161 return 1;
3162}
3163
Simon Horman05ee2132017-01-04 09:37:25 +01003164
William Lallemand74c24fb2016-11-21 17:18:36 +01003165/* This I/O handler runs as an applet embedded in a stream interface. It is
3166 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3167 * appctx->st0 contains the operation in progress (dump, done). The handler
3168 * automatically unregisters itself once transfer is complete.
3169 */
Christopher Fauletef779222018-10-31 08:47:01 +01003170static void htx_stats_io_handler(struct appctx *appctx)
3171{
3172 struct stream_interface *si = appctx->owner;
3173 struct stream *s = si_strm(si);
3174 struct channel *req = si_oc(si);
3175 struct channel *res = si_ic(si);
3176 struct htx *req_htx, *res_htx;
3177
3178 res_htx = htx_from_buf(&res->buf);
3179
3180 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3181 goto out;
3182
3183 /* Check if the input buffer is avalaible. */
3184 if (!b_size(&res->buf)) {
3185 si_rx_room_blk(si);
3186 goto out;
3187 }
3188
3189 /* check that the output is not closed */
3190 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3191 appctx->st0 = STAT_HTTP_DONE;
3192
3193 /* all states are processed in sequence */
3194 if (appctx->st0 == STAT_HTTP_HEAD) {
3195 if (stats_send_htx_headers(si, res_htx)) {
3196 if (s->txn->meth == HTTP_METH_HEAD)
3197 appctx->st0 = STAT_HTTP_DONE;
3198 else
3199 appctx->st0 = STAT_HTTP_DUMP;
3200 }
3201 }
3202
3203 if (appctx->st0 == STAT_HTTP_DUMP) {
3204 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3205 appctx->st0 = STAT_HTTP_DONE;
3206 }
3207
3208 if (appctx->st0 == STAT_HTTP_POST) {
3209 if (stats_process_http_post(si))
3210 appctx->st0 = STAT_HTTP_LAST;
3211 else if (si_oc(si)->flags & CF_SHUTR)
3212 appctx->st0 = STAT_HTTP_DONE;
3213 }
3214
3215 if (appctx->st0 == STAT_HTTP_LAST) {
3216 if (stats_send_htx_redirect(si, res_htx))
3217 appctx->st0 = STAT_HTTP_DONE;
3218 }
3219
3220 if (appctx->st0 == STAT_HTTP_DONE) {
3221 struct htx_blk *blk;
3222
3223 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3224 // FIXME: if trailers failed, do not add EOD twice
3225 if (!htx_add_endof(res_htx, HTX_BLK_EOD)) {
3226 si_rx_room_blk(si);
3227 goto out;
3228 }
3229 if (!htx_add_trailer(res_htx, ist("\r\n"))) {
3230 si_rx_room_blk(si);
3231 goto out;
3232 }
3233 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
3234 }
3235 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3236 si_rx_room_blk(si);
3237 goto out;
3238 }
3239
3240 /* eat the whole request */
3241 req_htx = htx_from_buf(&req->buf);
3242 blk = htx_get_head_blk(req_htx);
3243 while (blk) {
3244 enum htx_blk_type type = htx_get_blk_type(blk);
3245
3246 blk = htx_remove_blk(req_htx, blk);
3247 if (type == HTX_BLK_EOM)
3248 break;
3249 }
Christopher Faulet9b95d312018-11-19 21:58:10 +01003250 co_set_data(req, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01003251 if (htx_is_empty(req_htx)) {
3252 htx_reset(req_htx);
3253 b_set_data(&req->buf, 0);
3254 }
3255 res->flags |= CF_READ_NULL;
3256 si_shutr(si);
3257 }
3258
3259 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3260 si_shutw(si);
3261
3262 if (appctx->st0 == STAT_HTTP_DONE) {
3263 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3264 si_shutr(si);
3265 res->flags |= CF_READ_NULL;
3266 }
3267 }
3268 out:
3269 /* we have left the request in the buffer for the case where we
3270 * process a POST, and this automatically re-enables activity on
3271 * read. It's better to indicate that we want to stop reading when
3272 * we're sending, so that we know there's at most one direction
3273 * deciding to wake the applet up. It saves it from looping when
3274 * emitting large blocks into small TCP windows.
3275 */
3276 if (htx_is_empty(res_htx)) {
3277 htx_reset(res_htx);
3278 b_set_data(&res->buf, 0);
3279 }
3280 else {
3281 b_set_data(&res->buf, b_size(&res->buf));
3282 si_stop_get(si);
3283 }
3284}
3285
3286
3287/* This I/O handler runs as an applet embedded in a stream interface. It is
3288 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3289 * appctx->st0 contains the operation in progress (dump, done). The handler
3290 * automatically unregisters itself once transfer is complete.
3291 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003292static void http_stats_io_handler(struct appctx *appctx)
3293{
3294 struct stream_interface *si = appctx->owner;
3295 struct stream *s = si_strm(si);
3296 struct channel *req = si_oc(si);
3297 struct channel *res = si_ic(si);
3298
Christopher Fauletef779222018-10-31 08:47:01 +01003299 if (IS_HTX_STRM(s))
3300 return htx_stats_io_handler(appctx);
3301
William Lallemand74c24fb2016-11-21 17:18:36 +01003302 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3303 goto out;
3304
Joseph Herlant29023ec2018-11-15 13:39:46 -08003305 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003306 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003307 /* already subscribed, we'll be called later once the buffer is
3308 * available.
3309 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003310 goto out;
3311 }
3312
William Lallemand74c24fb2016-11-21 17:18:36 +01003313 /* check that the output is not closed */
3314 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3315 appctx->st0 = STAT_HTTP_DONE;
3316
3317 /* all states are processed in sequence */
3318 if (appctx->st0 == STAT_HTTP_HEAD) {
3319 if (stats_send_http_headers(si)) {
3320 if (s->txn->meth == HTTP_METH_HEAD)
3321 appctx->st0 = STAT_HTTP_DONE;
3322 else
3323 appctx->st0 = STAT_HTTP_DUMP;
3324 }
3325 }
3326
3327 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003328 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003329 unsigned int data_len;
3330 unsigned int last_len;
3331 unsigned int last_fwd = 0;
3332
3333 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3334 /* One difficulty we're facing is that we must prevent
3335 * the input data from being automatically forwarded to
3336 * the output area. For this, we temporarily disable
3337 * forwarding on the channel.
3338 */
3339 last_fwd = si_ic(si)->to_forward;
3340 si_ic(si)->to_forward = 0;
3341 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003342 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003343 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003344 si_ic(si)->to_forward = last_fwd;
3345 goto out;
3346 }
3347 }
3348
Willy Tarreau97f538b2018-06-15 19:41:31 +02003349 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003350 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003351 appctx->st0 = STAT_HTTP_DONE;
3352
Willy Tarreau97f538b2018-06-15 19:41:31 +02003353 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003354
3355 /* Now we must either adjust or remove the chunk size. This is
3356 * not easy because the chunk size might wrap at the end of the
3357 * buffer, so we pretend we have nothing in the buffer, we write
3358 * the size, then restore the buffer's contents. Note that we can
3359 * only do that because no forwarding is scheduled on the stats
3360 * applet.
3361 */
3362 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3363 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003364 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003365
3366 if (last_len != data_len) {
3367 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003368 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003369 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003370
3371 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003372 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003373 }
3374 /* now re-enable forwarding */
3375 channel_forward(si_ic(si), last_fwd);
3376 }
3377 }
3378
3379 if (appctx->st0 == STAT_HTTP_POST) {
3380 if (stats_process_http_post(si))
3381 appctx->st0 = STAT_HTTP_LAST;
3382 else if (si_oc(si)->flags & CF_SHUTR)
3383 appctx->st0 = STAT_HTTP_DONE;
3384 }
3385
3386 if (appctx->st0 == STAT_HTTP_LAST) {
3387 if (stats_send_http_redirect(si))
3388 appctx->st0 = STAT_HTTP_DONE;
3389 }
3390
3391 if (appctx->st0 == STAT_HTTP_DONE) {
3392 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3393 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003394 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003395 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003396 goto out;
3397 }
3398 }
3399 /* eat the whole request */
Willy Tarreau97f538b2018-06-15 19:41:31 +02003400 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003401 res->flags |= CF_READ_NULL;
3402 si_shutr(si);
3403 }
3404
3405 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3406 si_shutw(si);
3407
3408 if (appctx->st0 == STAT_HTTP_DONE) {
3409 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3410 si_shutr(si);
3411 res->flags |= CF_READ_NULL;
3412 }
3413 }
3414 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003415 /* we have left the request in the buffer for the case where we
3416 * process a POST, and this automatically re-enables activity on
3417 * read. It's better to indicate that we want to stop reading when
3418 * we're sending, so that we know there's at most one direction
3419 * deciding to wake the applet up. It saves it from looping when
3420 * emitting large blocks into small TCP windows.
3421 */
3422 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003423 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003424}
3425
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003426/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003427static int stats_dump_info_fields(struct buffer *out,
3428 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003429{
3430 int field;
3431
3432 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3433 if (!field_format(info, field))
3434 continue;
3435
3436 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3437 return 0;
3438 if (!stats_emit_raw_data_field(out, &info[field]))
3439 return 0;
3440 if (!chunk_strcat(out, "\n"))
3441 return 0;
3442 }
3443 return 1;
3444}
3445
3446/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003447static int stats_dump_typed_info_fields(struct buffer *out,
3448 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003449{
3450 int field;
3451
3452 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3453 if (!field_format(info, field))
3454 continue;
3455
3456 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3457 return 0;
3458 if (!stats_emit_field_tags(out, &info[field], ':'))
3459 return 0;
3460 if (!stats_emit_typed_data_field(out, &info[field]))
3461 return 0;
3462 if (!chunk_strcat(out, "\n"))
3463 return 0;
3464 }
3465 return 1;
3466}
3467
3468/* Fill <info> with HAProxy global info. <info> is preallocated
3469 * array of length <len>. The length of the aray must be
3470 * INF_TOTAL_FIELDS. If this length is less then this value, the
3471 * function returns 0, otherwise, it returns 1.
3472 */
3473int stats_fill_info(struct field *info, int len)
3474{
3475 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003476 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003477
3478#ifdef USE_OPENSSL
3479 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3480 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3481 int ssl_reuse = 0;
3482
3483 if (ssl_key_rate < ssl_sess_rate) {
3484 /* count the ssl reuse ratio and avoid overflows in both directions */
3485 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3486 }
3487#endif
3488
3489 if (len < INF_TOTAL_FIELDS)
3490 return 0;
3491
3492 chunk_reset(out);
3493 memset(info, 0, sizeof(*info) * len);
3494
3495 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3496 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3497 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3498
Yves Lafon95317282018-02-26 11:10:37 +01003499 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003500 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3501 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3502 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3503
3504 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3505 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3506
3507 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3508 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3509 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3510 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3511 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3512 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3513 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3514 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3515 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3516 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3517 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3518 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3519#ifdef USE_OPENSSL
3520 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3521 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3522 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3523#endif
3524 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3525 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3526 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3527 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3528 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3529 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3530 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3531 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3532 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3533
3534#ifdef USE_OPENSSL
3535 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3536 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3537 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3538 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3539 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3540 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3541 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3542 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3543 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3544 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3545#endif
3546 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3547 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3548 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3549#ifdef USE_ZLIB
3550 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3551 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3552#endif
3553 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003554 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003555 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3556 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3557 if (global.desc)
3558 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003559 info[INF_STOPPING] = mkf_u32(0, stopping);
3560 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003561 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003562 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003563 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003564 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003565 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003566 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003567
3568 return 1;
3569}
3570
3571/* This function dumps information onto the stream interface's read buffer.
3572 * It returns 0 as long as it does not complete, non-zero upon completion.
3573 * No state is used.
3574 */
3575static int stats_dump_info_to_buffer(struct stream_interface *si)
3576{
3577 struct appctx *appctx = __objt_appctx(si->end);
3578
3579 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3580 return 0;
3581
3582 chunk_reset(&trash);
3583
3584 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3585 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003586 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3587 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003588 else
3589 stats_dump_info_fields(&trash, info);
3590
Willy Tarreau06d80a92017-10-19 14:32:15 +02003591 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003592 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003593 return 0;
3594 }
3595
3596 return 1;
3597}
3598
Simon Horman6f6bb382017-01-04 09:37:26 +01003599/* This function dumps the schema onto the stream interface's read buffer.
3600 * It returns 0 as long as it does not complete, non-zero upon completion.
3601 * No state is used.
3602 *
3603 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3604 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3605 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003606static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003607{
3608
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003609 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003610
3611 chunk_strcat(out,
3612 "{"
3613 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3614 "\"oneOf\":["
3615 "{"
3616 "\"title\":\"Info\","
3617 "\"type\":\"array\","
3618 "\"items\":{"
3619 "\"properties\":{"
3620 "\"title\":\"InfoItem\","
3621 "\"type\":\"object\","
3622 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3623 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3624 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3625 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3626 "},"
3627 "\"required\":[\"field\",\"processNum\",\"tags\","
3628 "\"value\"]"
3629 "}"
3630 "},"
3631 "{"
3632 "\"title\":\"Stat\","
3633 "\"type\":\"array\","
3634 "\"items\":{"
3635 "\"title\":\"InfoItem\","
3636 "\"type\":\"object\","
3637 "\"properties\":{"
3638 "\"objType\":{"
3639 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3640 "\"Server\",\"Unknown\"]"
3641 "},"
3642 "\"proxyId\":{"
3643 "\"type\":\"integer\","
3644 "\"minimum\":0"
3645 "},"
3646 "\"id\":{"
3647 "\"type\":\"integer\","
3648 "\"minimum\":0"
3649 "},"
3650 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3651 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3652 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3653 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3654 "},"
3655 "\"required\":[\"objType\",\"proxyId\",\"id\","
3656 "\"field\",\"processNum\",\"tags\","
3657 "\"value\"]"
3658 "}"
3659 "},"
3660 "{"
3661 "\"title\":\"Error\","
3662 "\"type\":\"object\","
3663 "\"properties\":{"
3664 "\"errorStr\":{"
3665 "\"type\":\"string\""
3666 "},"
3667 "\"required\":[\"errorStr\"]"
3668 "}"
3669 "}"
3670 "],"
3671 "\"definitions\":{"
3672 "\"field\":{"
3673 "\"type\":\"object\","
3674 "\"pos\":{"
3675 "\"type\":\"integer\","
3676 "\"minimum\":0"
3677 "},"
3678 "\"name\":{"
3679 "\"type\":\"string\""
3680 "},"
3681 "\"required\":[\"pos\",\"name\"]"
3682 "},"
3683 "\"processNum\":{"
3684 "\"type\":\"integer\","
3685 "\"minimum\":1"
3686 "},"
3687 "\"tags\":{"
3688 "\"type\":\"object\","
3689 "\"origin\":{"
3690 "\"type\":\"string\","
3691 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3692 "\"Config\",\"Product\",\"Unknown\"]"
3693 "},"
3694 "\"nature\":{"
3695 "\"type\":\"string\","
3696 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3697 "\"Rate\",\"Counter\",\"Duration\","
3698 "\"Age\",\"Time\",\"Name\",\"Output\","
3699 "\"Avg\", \"Unknown\"]"
3700 "},"
3701 "\"scope\":{"
3702 "\"type\":\"string\","
3703 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3704 "\"System\",\"Unknown\"]"
3705 "},"
3706 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3707 "},"
3708 "\"typedValue\":{"
3709 "\"type\":\"object\","
3710 "\"oneOf\":["
3711 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3712 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3713 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3714 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3715 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3716 "],"
3717 "\"definitions\":{"
3718 "\"s32Value\":{"
3719 "\"properties\":{"
3720 "\"type\":{"
3721 "\"type\":\"string\","
3722 "\"enum\":[\"s32\"]"
3723 "},"
3724 "\"value\":{"
3725 "\"type\":\"integer\","
3726 "\"minimum\":-2147483648,"
3727 "\"maximum\":2147483647"
3728 "}"
3729 "},"
3730 "\"required\":[\"type\",\"value\"]"
3731 "},"
3732 "\"s64Value\":{"
3733 "\"properties\":{"
3734 "\"type\":{"
3735 "\"type\":\"string\","
3736 "\"enum\":[\"s64\"]"
3737 "},"
3738 "\"value\":{"
3739 "\"type\":\"integer\","
3740 "\"minimum\":-9007199254740991,"
3741 "\"maximum\":9007199254740991"
3742 "}"
3743 "},"
3744 "\"required\":[\"type\",\"value\"]"
3745 "},"
3746 "\"u32Value\":{"
3747 "\"properties\":{"
3748 "\"type\":{"
3749 "\"type\":\"string\","
3750 "\"enum\":[\"u32\"]"
3751 "},"
3752 "\"value\":{"
3753 "\"type\":\"integer\","
3754 "\"minimum\":0,"
3755 "\"maximum\":4294967295"
3756 "}"
3757 "},"
3758 "\"required\":[\"type\",\"value\"]"
3759 "},"
3760 "\"u64Value\":{"
3761 "\"properties\":{"
3762 "\"type\":{"
3763 "\"type\":\"string\","
3764 "\"enum\":[\"u64\"]"
3765 "},"
3766 "\"value\":{"
3767 "\"type\":\"integer\","
3768 "\"minimum\":0,"
3769 "\"maximum\":9007199254740991"
3770 "}"
3771 "},"
3772 "\"required\":[\"type\",\"value\"]"
3773 "},"
3774 "\"strValue\":{"
3775 "\"properties\":{"
3776 "\"type\":{"
3777 "\"type\":\"string\","
3778 "\"enum\":[\"str\"]"
3779 "},"
3780 "\"value\":{\"type\":\"string\"}"
3781 "},"
3782 "\"required\":[\"type\",\"value\"]"
3783 "},"
3784 "\"unknownValue\":{"
3785 "\"properties\":{"
3786 "\"type\":{"
3787 "\"type\":\"integer\","
3788 "\"minimum\":0"
3789 "},"
3790 "\"value\":{"
3791 "\"type\":\"string\","
3792 "\"enum\":[\"unknown\"]"
3793 "}"
3794 "},"
3795 "\"required\":[\"type\",\"value\"]"
3796 "}"
3797 "}"
3798 "}"
3799 "}"
3800 "}");
3801
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003802 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003803 chunk_reset(out);
3804 chunk_appendf(out,
3805 "{\"errorStr\":\"output buffer too short\"}");
3806 }
3807}
3808
3809/* This function dumps the schema onto the stream interface's read buffer.
3810 * It returns 0 as long as it does not complete, non-zero upon completion.
3811 * No state is used.
3812 */
3813static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3814{
3815 chunk_reset(&trash);
3816
3817 stats_dump_json_schema(&trash);
3818
Willy Tarreau06d80a92017-10-19 14:32:15 +02003819 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003820 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003821 return 0;
3822 }
3823
3824 return 1;
3825}
3826
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003827static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003828{
3829 struct proxy *px;
3830 struct server *sv;
3831 struct listener *li;
3832 int clrall = 0;
3833
3834 if (strcmp(args[2], "all") == 0)
3835 clrall = 1;
3836
3837 /* check permissions */
3838 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3839 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3840 return 1;
3841
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003842 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003843 if (clrall) {
3844 memset(&px->be_counters, 0, sizeof(px->be_counters));
3845 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3846 }
3847 else {
3848 px->be_counters.conn_max = 0;
3849 px->be_counters.p.http.rps_max = 0;
3850 px->be_counters.sps_max = 0;
3851 px->be_counters.cps_max = 0;
3852 px->be_counters.nbpend_max = 0;
3853
3854 px->fe_counters.conn_max = 0;
3855 px->fe_counters.p.http.rps_max = 0;
3856 px->fe_counters.sps_max = 0;
3857 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003858 }
3859
3860 for (sv = px->srv; sv; sv = sv->next)
3861 if (clrall)
3862 memset(&sv->counters, 0, sizeof(sv->counters));
3863 else {
3864 sv->counters.cur_sess_max = 0;
3865 sv->counters.nbpend_max = 0;
3866 sv->counters.sps_max = 0;
3867 }
3868
3869 list_for_each_entry(li, &px->conf.listeners, by_fe)
3870 if (li->counters) {
3871 if (clrall)
3872 memset(li->counters, 0, sizeof(*li->counters));
3873 else
3874 li->counters->conn_max = 0;
3875 }
3876 }
3877
3878 global.cps_max = 0;
3879 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003880 global.ssl_max = 0;
3881 global.ssl_fe_keys_max = 0;
3882 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003883
3884 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003885 return 1;
3886}
3887
3888
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003889static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003890{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003891 appctx->ctx.stats.scope_str = 0;
3892 appctx->ctx.stats.scope_len = 0;
3893 appctx->ctx.stats.flags = 0;
3894
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003895 if (strcmp(args[2], "typed") == 0)
3896 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003897 else if (strcmp(args[2], "json") == 0)
3898 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003899 return 0;
3900}
3901
3902
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003903static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003904{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003905 appctx->ctx.stats.scope_str = 0;
3906 appctx->ctx.stats.scope_len = 0;
3907 appctx->ctx.stats.flags = 0;
3908
Willy Tarreau2b812e22016-11-22 16:18:05 +01003909 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003910 struct proxy *px;
3911
3912 px = proxy_find_by_name(args[2], 0, 0);
3913 if (px)
3914 appctx->ctx.stats.iid = px->uuid;
3915 else
3916 appctx->ctx.stats.iid = atoi(args[2]);
3917
3918 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003919 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003920 appctx->ctx.cli.msg = "No such proxy.\n";
3921 appctx->st0 = CLI_ST_PRINT;
3922 return 1;
3923 }
3924
Willy Tarreau2b812e22016-11-22 16:18:05 +01003925 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003926 appctx->ctx.stats.type = atoi(args[3]);
3927 appctx->ctx.stats.sid = atoi(args[4]);
3928 if (strcmp(args[5], "typed") == 0)
3929 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003930 else if (strcmp(args[5], "json") == 0)
3931 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003932 }
3933 else if (strcmp(args[2], "typed") == 0)
3934 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003935 else if (strcmp(args[2], "json") == 0)
3936 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003937
Willy Tarreau2b812e22016-11-22 16:18:05 +01003938 return 0;
3939}
3940
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003941static int cli_io_handler_dump_info(struct appctx *appctx)
3942{
3943 return stats_dump_info_to_buffer(appctx->owner);
3944}
3945
Willy Tarreau2b812e22016-11-22 16:18:05 +01003946/* This I/O handler runs as an applet embedded in a stream interface. It is
3947 * used to send raw stats over a socket.
3948 */
3949static int cli_io_handler_dump_stat(struct appctx *appctx)
3950{
Christopher Fauletef779222018-10-31 08:47:01 +01003951 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003952}
3953
Simon Horman6f6bb382017-01-04 09:37:26 +01003954static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3955{
3956 return stats_dump_json_schema_to_buffer(appctx->owner);
3957}
3958
Willy Tarreau2b812e22016-11-22 16:18:05 +01003959/* register cli keywords */
3960static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003961 { { "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 +01003962 { { "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 +01003963 { { "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 +01003964 { { "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 +01003965 {{},}
3966}};
3967
William Lallemand74c24fb2016-11-21 17:18:36 +01003968struct applet http_stats_applet = {
3969 .obj_type = OBJ_TYPE_APPLET,
3970 .name = "<STATS>", /* used for logging */
3971 .fct = http_stats_io_handler,
3972 .release = NULL,
3973};
3974
Willy Tarreau2b812e22016-11-22 16:18:05 +01003975__attribute__((constructor))
3976static void __stat_init(void)
3977{
3978 cli_register_kw(&cli_kws);
3979}
3980
William Lallemand74c24fb2016-11-21 17:18:36 +01003981/*
3982 * Local variables:
3983 * c-indent-level: 8
3984 * c-basic-offset: 8
3985 * End:
3986 */