blob: 57968860fc6033af5b5bfa1435a7e60e56b0c6ae [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>
Willy Tarreau0108d902018-11-25 19:14:37 +010032#include <common/initcall.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
36#include <common/ticks.h>
37#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
40#include <common/base64.h>
41
42#include <types/applet.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010043#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010044#include <types/global.h>
45#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010046#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010047
48#include <proto/backend.h>
49#include <proto/channel.h>
50#include <proto/checks.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010051#include <proto/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010052#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010053#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010054#include <proto/fd.h>
55#include <proto/freq_ctr.h>
56#include <proto/frontend.h>
Christopher Fauletef779222018-10-31 08:47:01 +010057#include <proto/htx.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010058#include <proto/log.h>
59#include <proto/pattern.h>
60#include <proto/pipe.h>
61#include <proto/listener.h>
62#include <proto/map.h>
Christopher Fauletef779222018-10-31 08:47:01 +010063#include <proto/proto_http.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010064#include <proto/proxy.h>
65#include <proto/sample.h>
66#include <proto/session.h>
67#include <proto/stream.h>
68#include <proto/server.h>
69#include <proto/raw_sock.h>
70#include <proto/stream_interface.h>
71#include <proto/task.h>
72
73#ifdef USE_OPENSSL
74#include <proto/ssl_sock.h>
75#include <types/ssl_sock.h>
76#endif
77
78
Willy Tarreau0baac8c2016-11-22 16:36:53 +010079/* These are the field names for each INF_* field position. Please pay attention
80 * to always use the exact same name except that the strings for new names must
81 * be lower case or CamelCase while the enum entries must be upper case.
82 */
83const char *info_field_names[INF_TOTAL_FIELDS] = {
84 [INF_NAME] = "Name",
85 [INF_VERSION] = "Version",
86 [INF_RELEASE_DATE] = "Release_date",
Yves Lafon95317282018-02-26 11:10:37 +010087 [INF_NBTHREAD] = "Nbthread",
Willy Tarreau0baac8c2016-11-22 16:36:53 +010088 [INF_NBPROC] = "Nbproc",
89 [INF_PROCESS_NUM] = "Process_num",
90 [INF_PID] = "Pid",
91 [INF_UPTIME] = "Uptime",
92 [INF_UPTIME_SEC] = "Uptime_sec",
93 [INF_MEMMAX_MB] = "Memmax_MB",
94 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
95 [INF_POOL_USED_MB] = "PoolUsed_MB",
96 [INF_POOL_FAILED] = "PoolFailed",
97 [INF_ULIMIT_N] = "Ulimit-n",
98 [INF_MAXSOCK] = "Maxsock",
99 [INF_MAXCONN] = "Maxconn",
100 [INF_HARD_MAXCONN] = "Hard_maxconn",
101 [INF_CURR_CONN] = "CurrConns",
102 [INF_CUM_CONN] = "CumConns",
103 [INF_CUM_REQ] = "CumReq",
104 [INF_MAX_SSL_CONNS] = "MaxSslConns",
105 [INF_CURR_SSL_CONNS] = "CurrSslConns",
106 [INF_CUM_SSL_CONNS] = "CumSslConns",
107 [INF_MAXPIPES] = "Maxpipes",
108 [INF_PIPES_USED] = "PipesUsed",
109 [INF_PIPES_FREE] = "PipesFree",
110 [INF_CONN_RATE] = "ConnRate",
111 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
112 [INF_MAX_CONN_RATE] = "MaxConnRate",
113 [INF_SESS_RATE] = "SessRate",
114 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
115 [INF_MAX_SESS_RATE] = "MaxSessRate",
116 [INF_SSL_RATE] = "SslRate",
117 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
118 [INF_MAX_SSL_RATE] = "MaxSslRate",
119 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
120 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
121 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
122 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
123 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
124 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
125 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
126 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
127 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
128 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
129 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
130 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
131 [INF_TASKS] = "Tasks",
132 [INF_RUN_QUEUE] = "Run_queue",
133 [INF_IDLE_PCT] = "Idle_pct",
134 [INF_NODE] = "node",
135 [INF_DESCRIPTION] = "description",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100136 [INF_STOPPING] = "Stopping",
137 [INF_JOBS] = "Jobs",
William Lallemanda7199262018-11-16 16:57:20 +0100138 [INF_UNSTOPPABLE_JOBS] = "Unstoppable Jobs",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100139 [INF_LISTENERS] = "Listeners",
Willy Tarreau199ad242018-11-05 16:31:22 +0100140 [INF_ACTIVE_PEERS] = "ActivePeers",
Willy Tarreau2d372c22018-11-05 17:12:27 +0100141 [INF_CONNECTED_PEERS] = "ConnectedPeers",
Willy Tarreau13ef7732018-11-12 07:25:28 +0100142 [INF_DROPPED_LOGS] = "DroppedLogs",
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100143 [INF_BUSY_POLLING] = "BusyPolling",
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100144};
145
William Lallemand74c24fb2016-11-21 17:18:36 +0100146const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
147 [ST_F_PXNAME] = "pxname",
148 [ST_F_SVNAME] = "svname",
149 [ST_F_QCUR] = "qcur",
150 [ST_F_QMAX] = "qmax",
151 [ST_F_SCUR] = "scur",
152 [ST_F_SMAX] = "smax",
153 [ST_F_SLIM] = "slim",
154 [ST_F_STOT] = "stot",
155 [ST_F_BIN] = "bin",
156 [ST_F_BOUT] = "bout",
157 [ST_F_DREQ] = "dreq",
158 [ST_F_DRESP] = "dresp",
159 [ST_F_EREQ] = "ereq",
160 [ST_F_ECON] = "econ",
161 [ST_F_ERESP] = "eresp",
162 [ST_F_WRETR] = "wretr",
163 [ST_F_WREDIS] = "wredis",
164 [ST_F_STATUS] = "status",
165 [ST_F_WEIGHT] = "weight",
166 [ST_F_ACT] = "act",
167 [ST_F_BCK] = "bck",
168 [ST_F_CHKFAIL] = "chkfail",
169 [ST_F_CHKDOWN] = "chkdown",
170 [ST_F_LASTCHG] = "lastchg",
171 [ST_F_DOWNTIME] = "downtime",
172 [ST_F_QLIMIT] = "qlimit",
173 [ST_F_PID] = "pid",
174 [ST_F_IID] = "iid",
175 [ST_F_SID] = "sid",
176 [ST_F_THROTTLE] = "throttle",
177 [ST_F_LBTOT] = "lbtot",
178 [ST_F_TRACKED] = "tracked",
179 [ST_F_TYPE] = "type",
180 [ST_F_RATE] = "rate",
181 [ST_F_RATE_LIM] = "rate_lim",
182 [ST_F_RATE_MAX] = "rate_max",
183 [ST_F_CHECK_STATUS] = "check_status",
184 [ST_F_CHECK_CODE] = "check_code",
185 [ST_F_CHECK_DURATION] = "check_duration",
186 [ST_F_HRSP_1XX] = "hrsp_1xx",
187 [ST_F_HRSP_2XX] = "hrsp_2xx",
188 [ST_F_HRSP_3XX] = "hrsp_3xx",
189 [ST_F_HRSP_4XX] = "hrsp_4xx",
190 [ST_F_HRSP_5XX] = "hrsp_5xx",
191 [ST_F_HRSP_OTHER] = "hrsp_other",
192 [ST_F_HANAFAIL] = "hanafail",
193 [ST_F_REQ_RATE] = "req_rate",
194 [ST_F_REQ_RATE_MAX] = "req_rate_max",
195 [ST_F_REQ_TOT] = "req_tot",
196 [ST_F_CLI_ABRT] = "cli_abrt",
197 [ST_F_SRV_ABRT] = "srv_abrt",
198 [ST_F_COMP_IN] = "comp_in",
199 [ST_F_COMP_OUT] = "comp_out",
200 [ST_F_COMP_BYP] = "comp_byp",
201 [ST_F_COMP_RSP] = "comp_rsp",
202 [ST_F_LASTSESS] = "lastsess",
203 [ST_F_LAST_CHK] = "last_chk",
204 [ST_F_LAST_AGT] = "last_agt",
205 [ST_F_QTIME] = "qtime",
206 [ST_F_CTIME] = "ctime",
207 [ST_F_RTIME] = "rtime",
208 [ST_F_TTIME] = "ttime",
209 [ST_F_AGENT_STATUS] = "agent_status",
210 [ST_F_AGENT_CODE] = "agent_code",
211 [ST_F_AGENT_DURATION] = "agent_duration",
212 [ST_F_CHECK_DESC] = "check_desc",
213 [ST_F_AGENT_DESC] = "agent_desc",
214 [ST_F_CHECK_RISE] = "check_rise",
215 [ST_F_CHECK_FALL] = "check_fall",
216 [ST_F_CHECK_HEALTH] = "check_health",
217 [ST_F_AGENT_RISE] = "agent_rise",
218 [ST_F_AGENT_FALL] = "agent_fall",
219 [ST_F_AGENT_HEALTH] = "agent_health",
220 [ST_F_ADDR] = "addr",
221 [ST_F_COOKIE] = "cookie",
222 [ST_F_MODE] = "mode",
223 [ST_F_ALGO] = "algo",
224 [ST_F_CONN_RATE] = "conn_rate",
225 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
226 [ST_F_CONN_TOT] = "conn_tot",
227 [ST_F_INTERCEPTED] = "intercepted",
228 [ST_F_DCON] = "dcon",
229 [ST_F_DSES] = "dses",
Tim Duesterhus3fd19732018-05-27 20:35:08 +0200230 [ST_F_WREW] = "wrew",
William Lallemand74c24fb2016-11-21 17:18:36 +0100231};
232
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100233/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100234static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100235/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100236static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100237
238
Christopher Fauletef779222018-10-31 08:47:01 +0100239static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
240{
241 if (htx) {
242 if (!htx_add_data(htx, ist2(chk->area, chk->data)))
243 return 0;
244 chn->total += chk->data;
245 chk->data = 0;
246 /* notify that some data was read from the SI into the buffer */
247 chn->flags |= CF_READ_PARTIAL;
248 }
249 else {
250 if (ci_putchk(chn, chk) == -1)
251 return 0;
252 }
253 return 1;
254}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100255
William Lallemand74c24fb2016-11-21 17:18:36 +0100256/*
257 * http_stats_io_handler()
258 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
259 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100260 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100261 * -> stats_dump_html_head() // emits the HTML headers
262 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
263 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
264 * -> stats_dump_html_px_hdr()
265 * -> stats_dump_fe_stats()
266 * -> stats_dump_li_stats()
267 * -> stats_dump_sv_stats()
268 * -> stats_dump_be_stats()
269 * -> stats_dump_html_px_end()
270 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100271 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100272 */
273
274
275extern const char *stat_status_codes[];
276
277/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
278 * for clearing it if needed.
279 * NOTE: Some tools happen to rely on the field position instead of its name,
280 * so please only append new fields at the end, never in the middle.
281 */
282static void stats_dump_csv_header()
283{
284 int field;
285
286 chunk_appendf(&trash, "# ");
287 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
288 chunk_appendf(&trash, "%s,", stat_field_names[field]);
289
290 chunk_appendf(&trash, "\n");
291}
292
293
294/* Emits a stats field without any surrounding element and properly encoded to
295 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
296 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200297int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100298{
299 switch (field_format(f, 0)) {
300 case FF_EMPTY: return 1;
301 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
302 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
303 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
304 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
305 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
306 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
307 }
308}
309
310/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
311 * output is supposed to be used on its own line. Returns non-zero on success, 0
312 * if the buffer is full.
313 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200314int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100315{
316 switch (field_format(f, 0)) {
317 case FF_EMPTY: return 1;
318 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
319 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
320 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
321 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
322 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
323 default: return chunk_appendf(out, "%08x:?", f->type);
324 }
325}
326
Simon Horman05ee2132017-01-04 09:37:25 +0100327/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
328 * the recommendation for interoperable integers in section 6 of RFC 7159.
329 */
330#define JSON_INT_MAX ((1ULL << 53) - 1)
331#define JSON_INT_MIN (0 - JSON_INT_MAX)
332
333/* Emits a stats field value and its type in JSON.
334 * Returns non-zero on success, 0 on error.
335 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200336int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100337{
338 int old_len;
339 char buf[20];
340 const char *type, *value = buf, *quote = "";
341
342 switch (field_format(f, 0)) {
343 case FF_EMPTY: return 1;
344 case FF_S32: type = "\"s32\"";
345 snprintf(buf, sizeof(buf), "%d", f->u.s32);
346 break;
347 case FF_U32: type = "\"u32\"";
348 snprintf(buf, sizeof(buf), "%u", f->u.u32);
349 break;
350 case FF_S64: type = "\"s64\"";
351 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
352 return 0;
353 type = "\"s64\"";
354 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
355 break;
356 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
357 return 0;
358 type = "\"u64\"";
359 snprintf(buf, sizeof(buf), "%llu",
360 (unsigned long long) f->u.u64);
361 break;
362 case FF_STR: type = "\"str\"";
363 value = field_str(f, 0);
364 quote = "\"";
365 break;
366 default: snprintf(buf, sizeof(buf), "%u", f->type);
367 type = buf;
368 value = "unknown";
369 quote = "\"";
370 break;
371 }
372
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200373 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100374 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
375 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200376 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100377}
378
William Lallemand74c24fb2016-11-21 17:18:36 +0100379/* Emits an encoding of the field type on 3 characters followed by a delimiter.
380 * Returns non-zero on success, 0 if the buffer is full.
381 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200382int stats_emit_field_tags(struct buffer *out, const struct field *f,
383 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100384{
385 char origin, nature, scope;
386
387 switch (field_origin(f, 0)) {
388 case FO_METRIC: origin = 'M'; break;
389 case FO_STATUS: origin = 'S'; break;
390 case FO_KEY: origin = 'K'; break;
391 case FO_CONFIG: origin = 'C'; break;
392 case FO_PRODUCT: origin = 'P'; break;
393 default: origin = '?'; break;
394 }
395
396 switch (field_nature(f, 0)) {
397 case FN_GAUGE: nature = 'G'; break;
398 case FN_LIMIT: nature = 'L'; break;
399 case FN_MIN: nature = 'm'; break;
400 case FN_MAX: nature = 'M'; break;
401 case FN_RATE: nature = 'R'; break;
402 case FN_COUNTER: nature = 'C'; break;
403 case FN_DURATION: nature = 'D'; break;
404 case FN_AGE: nature = 'A'; break;
405 case FN_TIME: nature = 'T'; break;
406 case FN_NAME: nature = 'N'; break;
407 case FN_OUTPUT: nature = 'O'; break;
408 case FN_AVG: nature = 'a'; break;
409 default: nature = '?'; break;
410 }
411
412 switch (field_scope(f, 0)) {
413 case FS_PROCESS: scope = 'P'; break;
414 case FS_SERVICE: scope = 'S'; break;
415 case FS_SYSTEM: scope = 's'; break;
416 case FS_CLUSTER: scope = 'C'; break;
417 default: scope = '?'; break;
418 }
419
420 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
421}
422
Simon Horman05ee2132017-01-04 09:37:25 +0100423/* Emits an encoding of the field type as JSON.
424 * Returns non-zero on success, 0 if the buffer is full.
425 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200426int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100427{
428 const char *origin, *nature, *scope;
429 int old_len;
430
431 switch (field_origin(f, 0)) {
432 case FO_METRIC: origin = "Metric"; break;
433 case FO_STATUS: origin = "Status"; break;
434 case FO_KEY: origin = "Key"; break;
435 case FO_CONFIG: origin = "Config"; break;
436 case FO_PRODUCT: origin = "Product"; break;
437 default: origin = "Unknown"; break;
438 }
439
440 switch (field_nature(f, 0)) {
441 case FN_GAUGE: nature = "Gauge"; break;
442 case FN_LIMIT: nature = "Limit"; break;
443 case FN_MIN: nature = "Min"; break;
444 case FN_MAX: nature = "Max"; break;
445 case FN_RATE: nature = "Rate"; break;
446 case FN_COUNTER: nature = "Counter"; break;
447 case FN_DURATION: nature = "Duration"; break;
448 case FN_AGE: nature = "Age"; break;
449 case FN_TIME: nature = "Time"; break;
450 case FN_NAME: nature = "Name"; break;
451 case FN_OUTPUT: nature = "Output"; break;
452 case FN_AVG: nature = "Avg"; break;
453 default: nature = "Unknown"; break;
454 }
455
456 switch (field_scope(f, 0)) {
457 case FS_PROCESS: scope = "Process"; break;
458 case FS_SERVICE: scope = "Service"; break;
459 case FS_SYSTEM: scope = "System"; break;
460 case FS_CLUSTER: scope = "Cluster"; break;
461 default: scope = "Unknown"; break;
462 }
463
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200464 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100465 chunk_appendf(out, "\"tags\":{"
466 "\"origin\":\"%s\","
467 "\"nature\":\"%s\","
468 "\"scope\":\"%s\""
469 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200470 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100471}
William Lallemand74c24fb2016-11-21 17:18:36 +0100472
473/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200474static int stats_dump_fields_csv(struct buffer *out,
475 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100476{
477 int field;
478
479 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
480 if (!stats_emit_raw_data_field(out, &stats[field]))
481 return 0;
482 if (!chunk_strcat(out, ","))
483 return 0;
484 }
485 chunk_strcat(out, "\n");
486 return 1;
487}
488
489/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200490static int stats_dump_fields_typed(struct buffer *out,
491 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100492{
493 int field;
494
495 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
496 if (!stats[field].type)
497 continue;
498
499 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
500 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
501 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
502 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
503 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
504 '?',
505 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
506 field, stat_field_names[field], stats[ST_F_PID].u.u32);
507
508 if (!stats_emit_field_tags(out, &stats[field], ':'))
509 return 0;
510 if (!stats_emit_typed_data_field(out, &stats[field]))
511 return 0;
512 if (!chunk_strcat(out, "\n"))
513 return 0;
514 }
515 return 1;
516}
517
Simon Horman05ee2132017-01-04 09:37:25 +0100518/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200519static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100520 const struct field *info)
521{
522 int field;
523 int started = 0;
524
525 if (!chunk_strcat(out, "["))
526 return 0;
527
528 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
529 int old_len;
530
531 if (!field_format(info, field))
532 continue;
533
534 if (started && !chunk_strcat(out, ","))
535 goto err;
536 started = 1;
537
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200538 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100539 chunk_appendf(out,
540 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
541 "\"processNum\":%u,",
542 field, info_field_names[field],
543 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200544 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100545 goto err;
546
547 if (!stats_emit_json_field_tags(out, &info[field]))
548 goto err;
549
550 if (!stats_emit_json_data_field(out, &info[field]))
551 goto err;
552
553 if (!chunk_strcat(out, "}"))
554 goto err;
555 }
556
557 if (!chunk_strcat(out, "]"))
558 goto err;
559 return 1;
560
561err:
562 chunk_reset(out);
563 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
564 return 0;
565}
566
567/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200568static int stats_dump_fields_json(struct buffer *out,
569 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100570 int first_stat)
571{
572 int field;
573 int started = 0;
574
575 if (!first_stat && !chunk_strcat(out, ","))
576 return 0;
577 if (!chunk_strcat(out, "["))
578 return 0;
579
580 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
581 const char *obj_type;
582 int old_len;
583
584 if (!stats[field].type)
585 continue;
586
587 if (started && !chunk_strcat(out, ","))
588 goto err;
589 started = 1;
590
591 switch (stats[ST_F_TYPE].u.u32) {
592 case STATS_TYPE_FE: obj_type = "Frontend"; break;
593 case STATS_TYPE_BE: obj_type = "Backend"; break;
594 case STATS_TYPE_SO: obj_type = "Listener"; break;
595 case STATS_TYPE_SV: obj_type = "Server"; break;
596 default: obj_type = "Unknown"; break;
597 }
598
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200599 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100600 chunk_appendf(out,
601 "{"
602 "\"objType\":\"%s\","
603 "\"proxyId\":%d,"
604 "\"id\":%d,"
605 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
606 "\"processNum\":%u,",
607 obj_type, stats[ST_F_IID].u.u32,
608 stats[ST_F_SID].u.u32, field,
609 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200610 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100611 goto err;
612
613 if (!stats_emit_json_field_tags(out, &stats[field]))
614 goto err;
615
616 if (!stats_emit_json_data_field(out, &stats[field]))
617 goto err;
618
619 if (!chunk_strcat(out, "}"))
620 goto err;
621 }
622
623 if (!chunk_strcat(out, "]"))
624 goto err;
625
626 return 1;
627
628err:
629 chunk_reset(out);
630 if (!first_stat)
631 chunk_strcat(out, ",");
632 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
633 return 0;
634}
635
William Lallemand74c24fb2016-11-21 17:18:36 +0100636/* Dump all fields from <stats> into <out> using the HTML format. A column is
637 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
638 * are provided if ST_SHLGNDS is present in <flags>.
639 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200640static int stats_dump_fields_html(struct buffer *out,
641 const struct field *stats,
642 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100643{
Willy Tarreau83061a82018-07-13 11:56:34 +0200644 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100645
646 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
647 chunk_appendf(out,
648 /* name, queue */
649 "<tr class=\"frontend\">");
650
651 if (flags & ST_SHOWADMIN) {
652 /* Column sub-heading for Enable or Disable server */
653 chunk_appendf(out, "<td></td>");
654 }
655
656 chunk_appendf(out,
657 "<td class=ac>"
658 "<a name=\"%s/Frontend\"></a>"
659 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
660 "<td colspan=3></td>"
661 "",
662 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
663
664 chunk_appendf(out,
665 /* sessions rate : current */
666 "<td><u>%s<div class=tips><table class=det>"
667 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
668 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
669 "",
670 U2H(stats[ST_F_RATE].u.u32),
671 U2H(stats[ST_F_CONN_RATE].u.u32),
672 U2H(stats[ST_F_RATE].u.u32));
673
674 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
675 chunk_appendf(out,
676 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
677 U2H(stats[ST_F_REQ_RATE].u.u32));
678
679 chunk_appendf(out,
680 "</table></div></u></td>"
681 /* sessions rate : max */
682 "<td><u>%s<div class=tips><table class=det>"
683 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
684 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
685 "",
686 U2H(stats[ST_F_RATE_MAX].u.u32),
687 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
688 U2H(stats[ST_F_RATE_MAX].u.u32));
689
690 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
691 chunk_appendf(out,
692 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
693 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
694
695 chunk_appendf(out,
696 "</table></div></u></td>"
697 /* sessions rate : limit */
698 "<td>%s</td>",
699 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
700
701 chunk_appendf(out,
702 /* sessions: current, max, limit, total */
703 "<td>%s</td><td>%s</td><td>%s</td>"
704 "<td><u>%s<div class=tips><table class=det>"
705 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
706 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
707 "",
708 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
709 U2H(stats[ST_F_STOT].u.u64),
710 U2H(stats[ST_F_CONN_TOT].u.u64),
711 U2H(stats[ST_F_STOT].u.u64));
712
713 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
714 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
715 chunk_appendf(out,
716 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
717 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
718 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
719 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
720 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
721 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
722 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
723 "<tr><th>- other responses:</th><td>%s</td></tr>"
724 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200725 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100726 "",
727 U2H(stats[ST_F_REQ_TOT].u.u64),
728 U2H(stats[ST_F_HRSP_1XX].u.u64),
729 U2H(stats[ST_F_HRSP_2XX].u.u64),
730 U2H(stats[ST_F_COMP_RSP].u.u64),
731 stats[ST_F_HRSP_2XX].u.u64 ?
732 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
733 U2H(stats[ST_F_HRSP_3XX].u.u64),
734 U2H(stats[ST_F_HRSP_4XX].u.u64),
735 U2H(stats[ST_F_HRSP_5XX].u.u64),
736 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200737 U2H(stats[ST_F_INTERCEPTED].u.u64),
738 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100739 }
740
741 chunk_appendf(out,
742 "</table></div></u></td>"
743 /* sessions: lbtot, lastsess */
744 "<td></td><td></td>"
745 /* bytes : in */
746 "<td>%s</td>"
747 "",
748 U2H(stats[ST_F_BIN].u.u64));
749
750 chunk_appendf(out,
751 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
752 "<td>%s%s<div class=tips><table class=det>"
753 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
754 "<tr><th>Compression in:</th><td>%s</td></tr>"
755 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
756 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
757 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
758 "</table></div>%s</td>",
759 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
760 U2H(stats[ST_F_BOUT].u.u64),
761 U2H(stats[ST_F_BOUT].u.u64),
762 U2H(stats[ST_F_COMP_IN].u.u64),
763 U2H(stats[ST_F_COMP_OUT].u.u64),
764 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
765 U2H(stats[ST_F_COMP_BYP].u.u64),
766 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
767 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,
768 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
769
770 chunk_appendf(out,
771 /* denied: req, resp */
772 "<td>%s</td><td>%s</td>"
773 /* errors : request, connect, response */
774 "<td>%s</td><td></td><td></td>"
775 /* warnings: retries, redispatches */
776 "<td></td><td></td>"
777 /* server status : reflect frontend status */
778 "<td class=ac>%s</td>"
779 /* rest of server: nothing */
780 "<td class=ac colspan=8></td></tr>"
781 "",
782 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
783 U2H(stats[ST_F_EREQ].u.u64),
784 field_str(stats, ST_F_STATUS));
785 }
786 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
787 chunk_appendf(out, "<tr class=socket>");
788 if (flags & ST_SHOWADMIN) {
789 /* Column sub-heading for Enable or Disable server */
790 chunk_appendf(out, "<td></td>");
791 }
792
793 chunk_appendf(out,
794 /* frontend name, listener name */
795 "<td class=ac><a name=\"%s/+%s\"></a>%s"
796 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
797 "",
798 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
799 (flags & ST_SHLGNDS)?"<u>":"",
800 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
801
802 if (flags & ST_SHLGNDS) {
803 chunk_appendf(out, "<div class=tips>");
804
805 if (isdigit(*field_str(stats, ST_F_ADDR)))
806 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
807 else if (*field_str(stats, ST_F_ADDR) == '[')
808 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
809 else if (*field_str(stats, ST_F_ADDR))
810 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
811
812 /* id */
813 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
814 }
815
816 chunk_appendf(out,
817 /* queue */
818 "%s</td><td colspan=3></td>"
819 /* sessions rate: current, max, limit */
820 "<td colspan=3>&nbsp;</td>"
821 /* sessions: current, max, limit, total, lbtot, lastsess */
822 "<td>%s</td><td>%s</td><td>%s</td>"
823 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
824 /* bytes: in, out */
825 "<td>%s</td><td>%s</td>"
826 "",
827 (flags & ST_SHLGNDS)?"</u>":"",
828 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
829 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
830
831 chunk_appendf(out,
832 /* denied: req, resp */
833 "<td>%s</td><td>%s</td>"
834 /* errors: request, connect, response */
835 "<td>%s</td><td></td><td></td>"
836 /* warnings: retries, redispatches */
837 "<td></td><td></td>"
838 /* server status: reflect listener status */
839 "<td class=ac>%s</td>"
840 /* rest of server: nothing */
841 "<td class=ac colspan=8></td></tr>"
842 "",
843 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
844 U2H(stats[ST_F_EREQ].u.u64),
845 field_str(stats, ST_F_STATUS));
846 }
847 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
848 const char *style;
849
850 /* determine the style to use depending on the server's state,
851 * its health and weight. There isn't a 1-to-1 mapping between
852 * state and styles for the cases where the server is (still)
853 * up. The reason is that we don't want to report nolb and
854 * drain with the same color.
855 */
856
857 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
858 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
859 style = "down";
860 }
861 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
862 style = "going_up";
863 }
864 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
865 style = "going_down";
866 }
867 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
868 style = "nolb";
869 }
870 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
871 style = "no_check";
872 }
873 else if (!stats[ST_F_CHKFAIL].type ||
874 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
875 /* no check or max health = UP */
876 if (stats[ST_F_WEIGHT].u.u32)
877 style = "up";
878 else
879 style = "draining";
880 }
881 else {
882 style = "going_down";
883 }
884
885 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
886 chunk_appendf(out, "<tr class=\"maintain\">");
887 else
888 chunk_appendf(out,
889 "<tr class=\"%s_%s\">",
890 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
891
892
893 if (flags & ST_SHOWADMIN)
894 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000895 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
896 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100897 field_str(stats, ST_F_SVNAME));
898
899 chunk_appendf(out,
900 "<td class=ac><a name=\"%s/%s\"></a>%s"
901 "<a class=lfsb href=\"#%s/%s\">%s</a>"
902 "",
903 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
904 (flags & ST_SHLGNDS) ? "<u>" : "",
905 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
906
907 if (flags & ST_SHLGNDS) {
908 chunk_appendf(out, "<div class=tips>");
909
910 if (isdigit(*field_str(stats, ST_F_ADDR)))
911 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
912 else if (*field_str(stats, ST_F_ADDR) == '[')
913 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
914 else if (*field_str(stats, ST_F_ADDR))
915 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
916
917 /* id */
918 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
919
920 /* cookie */
921 if (stats[ST_F_COOKIE].type) {
922 chunk_appendf(out, ", cookie: '");
923 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
924 chunk_htmlencode(out, &src);
925 chunk_appendf(out, "'");
926 }
927
928 chunk_appendf(out, "</div>");
929 }
930
931 chunk_appendf(out,
932 /* queue : current, max, limit */
933 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
934 /* sessions rate : current, max, limit */
935 "<td>%s</td><td>%s</td><td></td>"
936 "",
937 (flags & ST_SHLGNDS) ? "</u>" : "",
938 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
939 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
940
941 chunk_appendf(out,
942 /* sessions: current, max, limit, total */
943 "<td>%s</td><td>%s</td><td>%s</td>"
944 "<td><u>%s<div class=tips><table class=det>"
945 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
946 "",
947 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
948 U2H(stats[ST_F_STOT].u.u64),
949 U2H(stats[ST_F_STOT].u.u64));
950
951 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
952 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
953 unsigned long long tot;
954
955 tot = stats[ST_F_HRSP_OTHER].u.u64;
956 tot += stats[ST_F_HRSP_1XX].u.u64;
957 tot += stats[ST_F_HRSP_2XX].u.u64;
958 tot += stats[ST_F_HRSP_3XX].u.u64;
959 tot += stats[ST_F_HRSP_4XX].u.u64;
960 tot += stats[ST_F_HRSP_5XX].u.u64;
961
962 chunk_appendf(out,
963 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
964 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
965 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
966 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
967 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
968 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
969 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200970 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100971 "",
972 U2H(tot),
973 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
974 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
975 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
976 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
977 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 +0200978 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
979 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100980 }
981
982 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
983 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
984 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
985 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
986 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
987 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
988
989 chunk_appendf(out,
990 "</table></div></u></td>"
991 /* sessions: lbtot, last */
992 "<td>%s</td><td>%s</td>",
993 U2H(stats[ST_F_LBTOT].u.u64),
994 human_time(stats[ST_F_LASTSESS].u.s32, 1));
995
996 chunk_appendf(out,
997 /* bytes : in, out */
998 "<td>%s</td><td>%s</td>"
999 /* denied: req, resp */
1000 "<td></td><td>%s</td>"
1001 /* errors : request, connect */
1002 "<td></td><td>%s</td>"
1003 /* errors : response */
1004 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1005 /* warnings: retries, redispatches */
1006 "<td>%lld</td><td>%lld</td>"
1007 "",
1008 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1009 U2H(stats[ST_F_DRESP].u.u64),
1010 U2H(stats[ST_F_ECON].u.u64),
1011 U2H(stats[ST_F_ERESP].u.u64),
1012 (long long)stats[ST_F_CLI_ABRT].u.u64,
1013 (long long)stats[ST_F_SRV_ABRT].u.u64,
1014 (long long)stats[ST_F_WRETR].u.u64,
1015 (long long)stats[ST_F_WREDIS].u.u64);
1016
1017 /* status, last change */
1018 chunk_appendf(out, "<td class=ac>");
1019
1020 /* FIXME!!!!
1021 * LASTCHG should contain the last change for *this* server and must be computed
1022 * properly above, as was done below, ie: this server if maint, otherwise ref server
1023 * if tracking. Note that ref is either local or remote depending on tracking.
1024 */
1025
1026
1027 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1028 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1029 }
1030 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1031 chunk_strcat(out, "<i>no check</i>");
1032 }
1033 else {
1034 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1035 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1036 if (stats[ST_F_CHECK_HEALTH].u.u32)
1037 chunk_strcat(out, " &uarr;");
1038 }
1039 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1040 chunk_strcat(out, " &darr;");
1041 }
1042
1043 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1044 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1045 chunk_appendf(out,
1046 "</td><td class=ac><u> %s",
1047 field_str(stats, ST_F_AGENT_STATUS));
1048
1049 if (stats[ST_F_AGENT_CODE].type)
1050 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1051
1052 if (stats[ST_F_AGENT_DURATION].type)
1053 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1054
1055 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1056
1057 if (*field_str(stats, ST_F_LAST_AGT)) {
1058 chunk_appendf(out, ": ");
1059 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1060 chunk_htmlencode(out, &src);
1061 }
1062 chunk_appendf(out, "</div></u>");
1063 }
1064 else if (stats[ST_F_CHECK_STATUS].type) {
1065 chunk_appendf(out,
1066 "</td><td class=ac><u> %s",
1067 field_str(stats, ST_F_CHECK_STATUS));
1068
1069 if (stats[ST_F_CHECK_CODE].type)
1070 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1071
1072 if (stats[ST_F_CHECK_DURATION].type)
1073 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1074
1075 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1076
1077 if (*field_str(stats, ST_F_LAST_CHK)) {
1078 chunk_appendf(out, ": ");
1079 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1080 chunk_htmlencode(out, &src);
1081 }
1082 chunk_appendf(out, "</div></u>");
1083 }
1084 else
1085 chunk_appendf(out, "</td><td>");
1086
1087 chunk_appendf(out,
1088 /* weight */
1089 "</td><td class=ac>%d</td>"
1090 /* act, bck */
1091 "<td class=ac>%s</td><td class=ac>%s</td>"
1092 "",
1093 stats[ST_F_WEIGHT].u.u32,
1094 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1095 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1096
1097 /* check failures: unique, fatal, down time */
1098 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1099 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1100 }
1101 else if (stats[ST_F_CHKFAIL].type) {
1102 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1103
1104 if (stats[ST_F_HANAFAIL].type)
1105 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1106
1107 chunk_appendf(out,
1108 "<div class=tips>Failed Health Checks%s</div></u></td>"
1109 "<td>%lld</td><td>%s</td>"
1110 "",
1111 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1112 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1113 }
1114 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1115 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1116 chunk_appendf(out,
1117 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1118 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1119 }
1120 else
1121 chunk_appendf(out, "<td colspan=3></td>");
1122
1123 /* throttle */
1124 if (stats[ST_F_THROTTLE].type)
1125 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1126 else
1127 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1128 }
1129 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1130 chunk_appendf(out, "<tr class=\"backend\">");
1131 if (flags & ST_SHOWADMIN) {
1132 /* Column sub-heading for Enable or Disable server */
1133 chunk_appendf(out, "<td></td>");
1134 }
1135 chunk_appendf(out,
1136 "<td class=ac>"
1137 /* name */
1138 "%s<a name=\"%s/Backend\"></a>"
1139 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1140 "",
1141 (flags & ST_SHLGNDS)?"<u>":"",
1142 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1143
1144 if (flags & ST_SHLGNDS) {
1145 /* balancing */
1146 chunk_appendf(out, "<div class=tips>balancing: %s",
1147 field_str(stats, ST_F_ALGO));
1148
1149 /* cookie */
1150 if (stats[ST_F_COOKIE].type) {
1151 chunk_appendf(out, ", cookie: '");
1152 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1153 chunk_htmlencode(out, &src);
1154 chunk_appendf(out, "'");
1155 }
1156 chunk_appendf(out, "</div>");
1157 }
1158
1159 chunk_appendf(out,
1160 "%s</td>"
1161 /* queue : current, max */
1162 "<td>%s</td><td>%s</td><td></td>"
1163 /* sessions rate : current, max, limit */
1164 "<td>%s</td><td>%s</td><td></td>"
1165 "",
1166 (flags & ST_SHLGNDS)?"</u>":"",
1167 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1168 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1169
1170 chunk_appendf(out,
1171 /* sessions: current, max, limit, total */
1172 "<td>%s</td><td>%s</td><td>%s</td>"
1173 "<td><u>%s<div class=tips><table class=det>"
1174 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1175 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001176 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 +01001177 U2H(stats[ST_F_STOT].u.u64),
1178 U2H(stats[ST_F_STOT].u.u64));
1179
1180 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1181 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1182 chunk_appendf(out,
1183 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1184 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1185 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1186 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1187 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1188 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1189 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1190 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001191 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001192 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1193 "",
1194 U2H(stats[ST_F_REQ_TOT].u.u64),
1195 U2H(stats[ST_F_HRSP_1XX].u.u64),
1196 U2H(stats[ST_F_HRSP_2XX].u.u64),
1197 U2H(stats[ST_F_COMP_RSP].u.u64),
1198 stats[ST_F_HRSP_2XX].u.u64 ?
1199 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1200 U2H(stats[ST_F_HRSP_3XX].u.u64),
1201 U2H(stats[ST_F_HRSP_4XX].u.u64),
1202 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001203 U2H(stats[ST_F_WREW].u.u64),
Willy Tarreauae9bea02016-11-25 14:44:52 +01001204 U2H(stats[ST_F_HRSP_OTHER].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001205 }
1206
1207 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1208 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1209 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1210 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1211 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1212
1213 chunk_appendf(out,
1214 "</table></div></u></td>"
1215 /* sessions: lbtot, last */
1216 "<td>%s</td><td>%s</td>"
1217 /* bytes: in */
1218 "<td>%s</td>"
1219 "",
1220 U2H(stats[ST_F_LBTOT].u.u64),
1221 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1222 U2H(stats[ST_F_BIN].u.u64));
1223
1224 chunk_appendf(out,
1225 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1226 "<td>%s%s<div class=tips><table class=det>"
1227 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1228 "<tr><th>Compression in:</th><td>%s</td></tr>"
1229 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1230 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1231 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1232 "</table></div>%s</td>",
1233 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1234 U2H(stats[ST_F_BOUT].u.u64),
1235 U2H(stats[ST_F_BOUT].u.u64),
1236 U2H(stats[ST_F_COMP_IN].u.u64),
1237 U2H(stats[ST_F_COMP_OUT].u.u64),
1238 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1239 U2H(stats[ST_F_COMP_BYP].u.u64),
1240 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1241 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,
1242 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1243
1244 chunk_appendf(out,
1245 /* denied: req, resp */
1246 "<td>%s</td><td>%s</td>"
1247 /* errors : request, connect */
1248 "<td></td><td>%s</td>"
1249 /* errors : response */
1250 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1251 /* warnings: retries, redispatches */
1252 "<td>%lld</td><td>%lld</td>"
1253 /* backend status: reflect backend status (up/down): we display UP
1254 * if the backend has known working servers or if it has no server at
1255 * all (eg: for stats). Then we display the total weight, number of
1256 * active and backups. */
1257 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1258 "<td class=ac>%d</td><td class=ac>%d</td>"
1259 "",
1260 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1261 U2H(stats[ST_F_ECON].u.u64),
1262 U2H(stats[ST_F_ERESP].u.u64),
1263 (long long)stats[ST_F_CLI_ABRT].u.u64,
1264 (long long)stats[ST_F_SRV_ABRT].u.u64,
1265 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1266 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1267 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1268 stats[ST_F_WEIGHT].u.u32,
1269 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1270
1271 chunk_appendf(out,
1272 /* rest of backend: nothing, down transitions, total downtime, throttle */
1273 "<td class=ac>&nbsp;</td><td>%d</td>"
1274 "<td>%s</td>"
1275 "<td></td>"
1276 "</tr>",
1277 stats[ST_F_CHKDOWN].u.u32,
1278 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1279 }
1280 return 1;
1281}
1282
1283int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1284{
Simon Horman05ee2132017-01-04 09:37:25 +01001285 int ret;
1286
William Lallemand74c24fb2016-11-21 17:18:36 +01001287 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1288 flags |= ST_SHOWADMIN;
1289
1290 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001291 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001292 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001293 ret = stats_dump_fields_typed(&trash, stats);
1294 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1295 ret = stats_dump_fields_json(&trash, stats,
1296 !(appctx->ctx.stats.flags &
1297 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001298 else
Simon Horman05ee2132017-01-04 09:37:25 +01001299 ret = stats_dump_fields_csv(&trash, stats);
1300
1301 if (ret)
1302 appctx->ctx.stats.flags |= STAT_STARTED;
1303
1304 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001305}
1306
1307/* Fill <stats> with the frontend statistics. <stats> is
1308 * preallocated array of length <len>. The length of the array
1309 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1310 * this value, the function returns 0, otherwise, it returns 1.
1311 */
1312int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1313{
1314 if (len < ST_F_TOTAL_FIELDS)
1315 return 0;
1316
1317 memset(stats, 0, sizeof(*stats) * len);
1318
1319 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1320 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1321 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1322 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1323 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1324 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1325 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1326 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1327 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1328 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1329 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1330 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1331 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1332 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1333 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1334 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1335 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1336 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1337 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1338 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1339 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1340 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001341 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001342
1343 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1344 if (px->mode == PR_MODE_HTTP) {
1345 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1346 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1347 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1348 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1349 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1350 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1351 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
1352 }
1353
1354 /* requests : req_rate, req_rate_max, req_tot, */
1355 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1356 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1357 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1358
1359 /* compression: in, out, bypassed, responses */
1360 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1361 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1362 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1363 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1364
1365 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1366 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1367 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1368 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1369
1370 return 1;
1371}
1372
1373/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1374 * the state from stream interface <si>. The caller is responsible for clearing
1375 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1376 */
1377static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1378{
1379 struct appctx *appctx = __objt_appctx(si->end);
1380
1381 if (!(px->cap & PR_CAP_FE))
1382 return 0;
1383
1384 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1385 return 0;
1386
1387 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1388 return 0;
1389
1390 return stats_dump_one_line(stats, 0, px, appctx);
1391}
1392
1393/* Fill <stats> with the listener statistics. <stats> is
1394 * preallocated array of length <len>. The length of the array
1395 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1396 * then this value, the function returns 0, otherwise, it
1397 * returns 1. <flags> can take the value ST_SHLGNDS.
1398 */
1399int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1400 struct field *stats, int len)
1401{
Willy Tarreau83061a82018-07-13 11:56:34 +02001402 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001403
1404 if (len < ST_F_TOTAL_FIELDS)
1405 return 0;
1406
1407 if (!l->counters)
1408 return 0;
1409
1410 chunk_reset(out);
1411 memset(stats, 0, sizeof(*stats) * len);
1412
1413 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1414 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1415 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1416 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1417 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1418 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1419 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1420 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1421 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1422 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1423 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1424 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1425 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1426 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1427 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
1428 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1429 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1430 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1431 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001432 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001433
1434 if (flags & ST_SHLGNDS) {
1435 char str[INET6_ADDRSTRLEN];
1436 int port;
1437
1438 port = get_host_port(&l->addr);
1439 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1440 case AF_INET:
1441 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1442 chunk_appendf(out, "%s:%d", str, port);
1443 break;
1444 case AF_INET6:
1445 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1446 chunk_appendf(out, "[%s]:%d", str, port);
1447 break;
1448 case AF_UNIX:
1449 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1450 break;
1451 case -1:
1452 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1453 chunk_strcat(out, strerror(errno));
1454 break;
1455 default: /* address family not supported */
1456 break;
1457 }
1458 }
1459
1460 return 1;
1461}
1462
1463/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1464 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1465 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1466 * otherwise.
1467 */
1468static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1469{
1470 struct appctx *appctx = __objt_appctx(si->end);
1471
1472 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1473 return 0;
1474
1475 return stats_dump_one_line(stats, flags, px, appctx);
1476}
1477
1478enum srv_stats_state {
1479 SRV_STATS_STATE_DOWN = 0,
1480 SRV_STATS_STATE_DOWN_AGENT,
1481 SRV_STATS_STATE_GOING_UP,
1482 SRV_STATS_STATE_UP_GOING_DOWN,
1483 SRV_STATS_STATE_UP,
1484 SRV_STATS_STATE_NOLB_GOING_DOWN,
1485 SRV_STATS_STATE_NOLB,
1486 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1487 SRV_STATS_STATE_DRAIN,
1488 SRV_STATS_STATE_DRAIN_AGENT,
1489 SRV_STATS_STATE_NO_CHECK,
1490
1491 SRV_STATS_STATE_COUNT, /* Must be last */
1492};
1493
1494static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1495 [SRV_STATS_STATE_DOWN] = "DOWN",
1496 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1497 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1498 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1499 [SRV_STATS_STATE_UP] = "UP",
1500 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1501 [SRV_STATS_STATE_NOLB] = "NOLB",
1502 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1503 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1504 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1505 [SRV_STATS_STATE_NO_CHECK] = "no check"
1506};
1507
1508/* Fill <stats> with the server statistics. <stats> is
1509 * preallocated array of length <len>. The length of the array
1510 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1511 * then this value, the function returns 0, otherwise, it
1512 * returns 1. <flags> can take the value ST_SHLGNDS.
1513 */
1514int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1515 struct field *stats, int len)
1516{
1517 struct server *via, *ref;
1518 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001519 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001520 enum srv_stats_state state;
1521 char *fld_status;
1522
1523 if (len < ST_F_TOTAL_FIELDS)
1524 return 0;
1525
1526 memset(stats, 0, sizeof(*stats) * len);
1527
1528 /* we have "via" which is the tracked server as described in the configuration,
1529 * and "ref" which is the checked server and the end of the chain.
1530 */
1531 via = sv->track ? sv->track : sv;
1532 ref = via;
1533 while (ref->track)
1534 ref = ref->track;
1535
Emeric Brun52a91d32017-08-31 14:41:55 +02001536 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001537 if ((ref->check.state & CHK_ST_ENABLED) &&
1538 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1539 state = SRV_STATS_STATE_UP_GOING_DOWN;
1540 } else {
1541 state = SRV_STATS_STATE_UP;
1542 }
1543
Emeric Brun52a91d32017-08-31 14:41:55 +02001544 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001545 if (ref->agent.state & CHK_ST_ENABLED)
1546 state = SRV_STATS_STATE_DRAIN_AGENT;
1547 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1548 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1549 else
1550 state = SRV_STATS_STATE_DRAIN;
1551 }
1552
1553 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1554 state = SRV_STATS_STATE_NO_CHECK;
1555 }
1556 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001557 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001558 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1559 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1560 state = SRV_STATS_STATE_NOLB;
1561 } else {
1562 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1563 }
1564 }
1565 else { /* stopped */
1566 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1567 state = SRV_STATS_STATE_DOWN_AGENT;
1568 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1569 state = SRV_STATS_STATE_DOWN; /* DOWN */
1570 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1571 state = SRV_STATS_STATE_GOING_UP;
1572 } else {
1573 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1574 }
1575 }
1576
1577 chunk_reset(out);
1578
1579 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1580 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1581 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1582 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1583 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1584 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1585 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1586
1587 if (sv->maxconn)
1588 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1589
1590 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1591 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1592 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1593 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1594 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1595 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1596 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1597 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001598 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001599
1600 /* status */
1601 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001602 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001603 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001604 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001605 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001606 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001607 chunk_appendf(out, "MAINT");
1608 else
1609 chunk_appendf(out,
1610 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001611 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1612 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001613
1614 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1615 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001616 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 +01001617 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1618 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1619
1620 /* check failures: unique, fatal; last change, total downtime */
1621 if (sv->check.state & CHK_ST_ENABLED) {
1622 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1623 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1624 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1625 }
1626
1627 if (sv->maxqueue)
1628 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1629
1630 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1631 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1632 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1633
Emeric Brun52a91d32017-08-31 14:41:55 +02001634 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001635 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1636
1637 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1638
1639 if (sv->track) {
1640 char *fld_track = chunk_newstr(out);
1641
1642 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1643 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1644 }
1645
1646 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1647 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1648 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1649
1650 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1651 const char *fld_chksts;
1652
1653 fld_chksts = chunk_newstr(out);
1654 chunk_strcat(out, "* "); // for check in progress
1655 chunk_strcat(out, get_check_status_info(sv->check.status));
1656 if (!(sv->check.state & CHK_ST_INPROGRESS))
1657 fld_chksts += 2; // skip "* "
1658 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1659
1660 if (sv->check.status >= HCHK_STATUS_L57DATA)
1661 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1662
1663 if (sv->check.status >= HCHK_STATUS_CHECKED)
1664 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1665
1666 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1667 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1668 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1669 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1670 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1671 }
1672
1673 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1674 const char *fld_chksts;
1675
1676 fld_chksts = chunk_newstr(out);
1677 chunk_strcat(out, "* "); // for check in progress
1678 chunk_strcat(out, get_check_status_info(sv->agent.status));
1679 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1680 fld_chksts += 2; // skip "* "
1681 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1682
1683 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1684 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1685
1686 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1687 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1688
1689 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1690 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1691 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1692 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1693 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1694 }
1695
1696 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1697 if (px->mode == PR_MODE_HTTP) {
1698 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1699 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1700 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1701 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1702 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1703 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1704 }
1705
1706 if (ref->observe)
1707 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1708
1709 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1710 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1711 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1712
1713 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1714 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1715 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1716 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1717
1718 if (flags & ST_SHLGNDS) {
1719 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1720 case AF_INET:
1721 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001722 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001723 break;
1724 case AF_INET6:
1725 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001726 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001727 break;
1728 case AF_UNIX:
1729 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1730 break;
1731 case -1:
1732 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1733 chunk_strcat(out, strerror(errno));
1734 break;
1735 default: /* address family not supported */
1736 break;
1737 }
1738
1739 if (sv->cookie)
1740 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1741 }
1742
1743 return 1;
1744}
1745
1746/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1747 * from stream interface <si>, stats flags <flags>, and server state <state>.
1748 * The caller is responsible for clearing the trash if needed. Returns non-zero
1749 * if it emits anything, zero otherwise.
1750 */
1751static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1752{
1753 struct appctx *appctx = __objt_appctx(si->end);
1754
1755 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1756 return 0;
1757
1758 return stats_dump_one_line(stats, flags, px, appctx);
1759}
1760
1761/* Fill <stats> with the backend statistics. <stats> is
1762 * preallocated array of length <len>. The length of the array
1763 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1764 * then this value, the function returns 0, otherwise, it
1765 * returns 1. <flags> can take the value ST_SHLGNDS.
1766 */
1767int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1768{
1769 if (len < ST_F_TOTAL_FIELDS)
1770 return 0;
1771
1772 memset(stats, 0, sizeof(*stats) * len);
1773
1774 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1775 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1776 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1777 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1778 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001779 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001780 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1781 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1782 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1783 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1784 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1785 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1786 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1787 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1788 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1789 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1790 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001791 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001792 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1793 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1794 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1795 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1796 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1797 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1798 if (px->srv)
1799 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1800
1801 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1802 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1803 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1804 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1805 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1806 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1807 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1808
1809 if (flags & ST_SHLGNDS) {
1810 if (px->cookie_name)
1811 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1812 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1813 }
1814
1815 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1816 if (px->mode == PR_MODE_HTTP) {
1817 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1818 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1819 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1820 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1821 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1822 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1823 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001824 }
1825
1826 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1827 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1828
1829 /* compression: in, out, bypassed, responses */
1830 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1831 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1832 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1833 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1834 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1835
1836 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1837 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1838 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1839 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1840
1841 return 1;
1842}
1843
1844/* Dumps a line for backend <px> to the trash for and uses the state from stream
1845 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1846 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1847 */
1848static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1849{
1850 struct appctx *appctx = __objt_appctx(si->end);
1851
1852 if (!(px->cap & PR_CAP_BE))
1853 return 0;
1854
1855 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1856 return 0;
1857
1858 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1859 return 0;
1860
1861 return stats_dump_one_line(stats, flags, px, appctx);
1862}
1863
1864/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1865 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1866 * for clearing the trash if needed.
1867 */
1868static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1869{
1870 struct appctx *appctx = __objt_appctx(si->end);
1871 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1872
1873 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1874 /* A form to enable/disable this proxy servers */
1875
1876 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1877 scope_txt[0] = 0;
1878 if (appctx->ctx.stats.scope_len) {
1879 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02001880 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 +01001881 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1882 }
1883
1884 chunk_appendf(&trash,
1885 "<form method=\"post\">");
1886 }
1887
1888 /* print a new table */
1889 chunk_appendf(&trash,
1890 "<table class=\"tbl\" width=\"100%%\">\n"
1891 "<tr class=\"titre\">"
1892 "<th class=\"pxname\" width=\"10%%\">");
1893
1894 chunk_appendf(&trash,
1895 "<a name=\"%s\"></a>%s"
1896 "<a class=px href=\"#%s\">%s</a>",
1897 px->id,
1898 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1899 px->id, px->id);
1900
1901 if (uri->flags & ST_SHLGNDS) {
1902 /* cap, mode, id */
1903 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1904 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1905 px->uuid);
1906 chunk_appendf(&trash, "</div>");
1907 }
1908
1909 chunk_appendf(&trash,
1910 "%s</th>"
1911 "<th class=\"%s\" width=\"90%%\">%s</th>"
1912 "</tr>\n"
1913 "</table>\n"
1914 "<table class=\"tbl\" width=\"100%%\">\n"
1915 "<tr class=\"titre\">",
1916 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1917 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1918
1919 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1920 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00001921 chunk_appendf(&trash,
1922 "<th rowspan=2 width=1><input type=\"checkbox\" \
1923 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
1924 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
1925 px->id,
1926 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01001927 }
1928
1929 chunk_appendf(&trash,
1930 "<th rowspan=2></th>"
1931 "<th colspan=3>Queue</th>"
1932 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
1933 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
1934 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
1935 "<th colspan=9>Server</th>"
1936 "</tr>\n"
1937 "<tr class=\"titre\">"
1938 "<th>Cur</th><th>Max</th><th>Limit</th>"
1939 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
1940 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
1941 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
1942 "<th>Resp</th><th>Retr</th><th>Redis</th>"
1943 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
1944 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1945 "<th>Thrtle</th>\n"
1946 "</tr>");
1947}
1948
1949/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
1950 * stream interface <si>. The caller is responsible for clearing the trash if needed.
1951 */
1952static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
1953{
1954 struct appctx *appctx = __objt_appctx(si->end);
1955 chunk_appendf(&trash, "</table>");
1956
1957 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1958 /* close the form used to enable/disable this proxy servers */
1959 chunk_appendf(&trash,
1960 "Choose the action to perform on the checked servers : "
1961 "<select name=action>"
1962 "<option value=\"\"></option>"
1963 "<option value=\"ready\">Set state to READY</option>"
1964 "<option value=\"drain\">Set state to DRAIN</option>"
1965 "<option value=\"maint\">Set state to MAINT</option>"
1966 "<option value=\"dhlth\">Health: disable checks</option>"
1967 "<option value=\"ehlth\">Health: enable checks</option>"
1968 "<option value=\"hrunn\">Health: force UP</option>"
1969 "<option value=\"hnolb\">Health: force NOLB</option>"
1970 "<option value=\"hdown\">Health: force DOWN</option>"
1971 "<option value=\"dagent\">Agent: disable checks</option>"
1972 "<option value=\"eagent\">Agent: enable checks</option>"
1973 "<option value=\"arunn\">Agent: force UP</option>"
1974 "<option value=\"adown\">Agent: force DOWN</option>"
1975 "<option value=\"shutdown\">Kill Sessions</option>"
1976 "</select>"
1977 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
1978 "&nbsp;<input type=\"submit\" value=\"Apply\">"
1979 "</form>",
1980 px->uuid);
1981 }
1982
1983 chunk_appendf(&trash, "<p>\n");
1984}
1985
1986/*
1987 * Dumps statistics for a proxy. The output is sent to the stream interface's
1988 * input buffer. Returns 0 if it had to stop dumping data because of lack of
1989 * buffer space, or non-zero if everything completed. This function is used
1990 * both by the CLI and the HTTP entry points, and is able to dump the output
1991 * in HTML or CSV formats. If the later, <uri> must be NULL.
1992 */
Christopher Fauletef779222018-10-31 08:47:01 +01001993int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
1994 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01001995{
1996 struct appctx *appctx = __objt_appctx(si->end);
1997 struct stream *s = si_strm(si);
1998 struct channel *rep = si_ic(si);
1999 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2000 struct listener *l;
2001 unsigned int flags;
2002
2003 if (uri)
2004 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002005 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002006 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2007 else
2008 flags = ST_SHNODE | ST_SHDESC;
2009
2010 chunk_reset(&trash);
2011
2012 switch (appctx->ctx.stats.px_st) {
2013 case STAT_PX_ST_INIT:
2014 /* we are on a new proxy */
2015 if (uri && uri->scope) {
2016 /* we have a limited scope, we have to check the proxy name */
2017 struct stat_scope *scope;
2018 int len;
2019
2020 len = strlen(px->id);
2021 scope = uri->scope;
2022
2023 while (scope) {
2024 /* match exact proxy name */
2025 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2026 break;
2027
2028 /* match '.' which means 'self' proxy */
2029 if (!strcmp(scope->px_id, ".") && px == s->be)
2030 break;
2031 scope = scope->next;
2032 }
2033
2034 /* proxy name not found : don't dump anything */
2035 if (scope == NULL)
2036 return 1;
2037 }
2038
2039 /* if the user has requested a limited output and the proxy
2040 * name does not match, skip it.
2041 */
2042 if (appctx->ctx.stats.scope_len &&
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002043 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 +01002044 return 1;
2045
2046 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2047 (appctx->ctx.stats.iid != -1) &&
2048 (px->uuid != appctx->ctx.stats.iid))
2049 return 1;
2050
2051 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2052 /* fall through */
2053
2054 case STAT_PX_ST_TH:
2055 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2056 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002057 if (!stats_putchk(rep, htx, &trash))
2058 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002059 }
2060
2061 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2062 /* fall through */
2063
2064 case STAT_PX_ST_FE:
2065 /* print the frontend */
2066 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002067 if (!stats_putchk(rep, htx, &trash))
2068 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002069 }
2070
2071 appctx->ctx.stats.l = px->conf.listeners.n;
2072 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2073 /* fall through */
2074
2075 case STAT_PX_ST_LI:
2076 /* stats.l has been initialized above */
2077 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002078 if (htx) {
2079 if (htx_almost_full(htx))
2080 goto full;
2081 }
2082 else {
2083 if (buffer_almost_full(&rep->buf))
2084 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002085 }
2086
2087 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2088 if (!l->counters)
2089 continue;
2090
2091 if (appctx->ctx.stats.flags & STAT_BOUND) {
2092 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2093 break;
2094
2095 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2096 continue;
2097 }
2098
2099 /* print the frontend */
2100 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002101 if (!stats_putchk(rep, htx, &trash))
2102 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002103 }
2104 }
2105
2106 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2107 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2108 /* fall through */
2109
2110 case STAT_PX_ST_SV:
2111 /* stats.sv has been initialized above */
2112 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002113 if (htx) {
2114 if (htx_almost_full(htx))
2115 goto full;
2116 }
2117 else {
2118 if (buffer_almost_full(&rep->buf))
2119 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002120 }
2121
2122 sv = appctx->ctx.stats.sv;
2123
2124 if (appctx->ctx.stats.flags & STAT_BOUND) {
2125 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2126 break;
2127
2128 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2129 continue;
2130 }
2131
2132 svs = sv;
2133 while (svs->track)
2134 svs = svs->track;
2135
2136 /* do not report servers which are DOWN and not changing state */
2137 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002138 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2139 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002140 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2141 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2142 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2143 continue;
2144 }
2145
2146 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002147 if (!stats_putchk(rep, htx, &trash))
2148 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002149 }
2150 } /* for sv */
2151
2152 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2153 /* fall through */
2154
2155 case STAT_PX_ST_BE:
2156 /* print the backend */
2157 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002158 if (!stats_putchk(rep, htx, &trash))
2159 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002160 }
2161
2162 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2163 /* fall through */
2164
2165 case STAT_PX_ST_END:
2166 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2167 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002168 if (!stats_putchk(rep, htx, &trash))
2169 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002170 }
2171
2172 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2173 /* fall through */
2174
2175 case STAT_PX_ST_FIN:
2176 return 1;
2177
2178 default:
2179 /* unknown state, we should put an abort() here ! */
2180 return 1;
2181 }
Christopher Fauletef779222018-10-31 08:47:01 +01002182
2183 full:
2184 si_rx_room_blk(si);
2185 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002186}
2187
2188/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2189 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2190 */
2191static void stats_dump_html_head(struct uri_auth *uri)
2192{
2193 /* WARNING! This must fit in the first buffer !!! */
2194 chunk_appendf(&trash,
2195 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2196 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2197 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2198 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2199 "<style type=\"text/css\"><!--\n"
2200 "body {"
2201 " font-family: arial, helvetica, sans-serif;"
2202 " font-size: 12px;"
2203 " font-weight: normal;"
2204 " color: black;"
2205 " background: white;"
2206 "}\n"
2207 "th,td {"
2208 " font-size: 10px;"
2209 "}\n"
2210 "h1 {"
2211 " font-size: x-large;"
2212 " margin-bottom: 0.5em;"
2213 "}\n"
2214 "h2 {"
2215 " font-family: helvetica, arial;"
2216 " font-size: x-large;"
2217 " font-weight: bold;"
2218 " font-style: italic;"
2219 " color: #6020a0;"
2220 " margin-top: 0em;"
2221 " margin-bottom: 0em;"
2222 "}\n"
2223 "h3 {"
2224 " font-family: helvetica, arial;"
2225 " font-size: 16px;"
2226 " font-weight: bold;"
2227 " color: #b00040;"
2228 " background: #e8e8d0;"
2229 " margin-top: 0em;"
2230 " margin-bottom: 0em;"
2231 "}\n"
2232 "li {"
2233 " margin-top: 0.25em;"
2234 " margin-right: 2em;"
2235 "}\n"
2236 ".hr {margin-top: 0.25em;"
2237 " border-color: black;"
2238 " border-bottom-style: solid;"
2239 "}\n"
2240 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2241 ".total {background: #20D0D0;color: #ffff80;}\n"
2242 ".frontend {background: #e8e8d0;}\n"
2243 ".socket {background: #d0d0d0;}\n"
2244 ".backend {background: #e8e8d0;}\n"
2245 ".active_down {background: #ff9090;}\n"
2246 ".active_going_up {background: #ffd020;}\n"
2247 ".active_going_down {background: #ffffa0;}\n"
2248 ".active_up {background: #c0ffc0;}\n"
2249 ".active_nolb {background: #20a0ff;}\n"
2250 ".active_draining {background: #20a0FF;}\n"
2251 ".active_no_check {background: #e0e0e0;}\n"
2252 ".backup_down {background: #ff9090;}\n"
2253 ".backup_going_up {background: #ff80ff;}\n"
2254 ".backup_going_down {background: #c060ff;}\n"
2255 ".backup_up {background: #b0d0ff;}\n"
2256 ".backup_nolb {background: #90b0e0;}\n"
2257 ".backup_draining {background: #cc9900;}\n"
2258 ".backup_no_check {background: #e0e0e0;}\n"
2259 ".maintain {background: #c07820;}\n"
2260 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2261 "\n"
2262 "a.px:link {color: #ffff40; text-decoration: none;}"
2263 "a.px:visited {color: #ffff40; text-decoration: none;}"
2264 "a.px:hover {color: #ffffff; text-decoration: none;}"
2265 "a.lfsb:link {color: #000000; text-decoration: none;}"
2266 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2267 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2268 "\n"
2269 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2270 "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"
2271 "table.tbl td.ac { text-align: center;}\n"
2272 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2273 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2274 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2275 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2276 "\n"
2277 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2278 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2279 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2280 "table.det { border-collapse: collapse; border-style: none; }\n"
2281 "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"
2282 "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"
2283 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2284 "div.tips {\n"
2285 " display:block;\n"
2286 " visibility:hidden;\n"
2287 " z-index:2147483647;\n"
2288 " position:absolute;\n"
2289 " padding:2px 4px 3px;\n"
2290 " background:#f0f060; color:#000000;\n"
2291 " border:1px solid #7040c0;\n"
2292 " white-space:nowrap;\n"
2293 " font-style:normal;font-size:11px;font-weight:normal;\n"
2294 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2295 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2296 "}\n"
2297 "u:hover div.tips {visibility:visible;}\n"
2298 "-->\n"
2299 "</style></head>\n",
2300 (uri->flags & ST_SHNODE) ? " on " : "",
2301 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2302 );
2303}
2304
2305/* Dumps the HTML stats information block to the trash for and uses the state from
2306 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2307 * for clearing the trash if needed.
2308 */
2309static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2310{
2311 struct appctx *appctx = __objt_appctx(si->end);
2312 unsigned int up = (now.tv_sec - start_date.tv_sec);
2313 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2314
2315 /* WARNING! this has to fit the first packet too.
2316 * We are around 3.5 kB, add adding entries will
2317 * become tricky if we want to support 4kB buffers !
2318 */
2319 chunk_appendf(&trash,
2320 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2321 PRODUCT_NAME "%s</a></h1>\n"
2322 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2323 "<hr width=\"100%%\" class=\"hr\">\n"
2324 "<h3>&gt; General process information</h3>\n"
2325 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002326 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002327 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2328 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2329 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
2330 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
2331 "Running tasks: %d/%d; idle = %d %%<br>\n"
2332 "</td><td align=\"center\" nowrap>\n"
2333 "<table class=\"lgd\"><tr>\n"
2334 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2335 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2336 "</tr><tr>\n"
2337 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2338 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2339 "</tr><tr>\n"
2340 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2341 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2342 "</tr><tr>\n"
2343 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2344 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2345 "</tr><tr>\n"
2346 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2347 "</tr><tr>\n"
2348 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2349 "</tr></table>\n"
2350 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2351 "</td>"
2352 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2353 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2354 "",
2355 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
2356 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2357 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2358 (uri->flags & ST_SHDESC) ? ": " : "",
2359 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002360 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002361 up / 86400, (up % 86400) / 3600,
2362 (up % 3600) / 60, (up % 60),
2363 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2364 global.rlimit_memmax ? " MB" : "",
2365 global.rlimit_nofile,
2366 global.maxsock, global.maxconn, global.maxpipes,
2367 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002368 tasks_run_queue_cur, nb_tasks_cur, idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002369 );
2370
2371 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002372 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 +01002373 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2374
2375 chunk_appendf(&trash,
2376 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2377 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2378 STAT_SCOPE_TXT_MAXLEN);
2379
2380 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2381 scope_txt[0] = 0;
2382 if (appctx->ctx.stats.scope_len) {
2383 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002384 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 +01002385 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2386 }
2387
2388 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2389 chunk_appendf(&trash,
2390 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2391 uri->uri_prefix,
2392 "",
2393 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2394 scope_txt);
2395 else
2396 chunk_appendf(&trash,
2397 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2398 uri->uri_prefix,
2399 ";up",
2400 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2401 scope_txt);
2402
2403 if (uri->refresh > 0) {
2404 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2405 chunk_appendf(&trash,
2406 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2407 uri->uri_prefix,
2408 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2409 "",
2410 scope_txt);
2411 else
2412 chunk_appendf(&trash,
2413 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2414 uri->uri_prefix,
2415 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2416 ";norefresh",
2417 scope_txt);
2418 }
2419
2420 chunk_appendf(&trash,
2421 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2422 uri->uri_prefix,
2423 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2424 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2425 scope_txt);
2426
2427 chunk_appendf(&trash,
2428 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2429 uri->uri_prefix,
2430 (uri->refresh > 0) ? ";norefresh" : "",
2431 scope_txt);
2432
2433 chunk_appendf(&trash,
2434 "</ul></td>"
2435 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2436 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2437 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2438 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2439 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2440 "</ul>"
2441 "</td>"
2442 "</tr></table>\n"
2443 ""
2444 );
2445
2446 if (appctx->ctx.stats.st_code) {
2447 switch (appctx->ctx.stats.st_code) {
2448 case STAT_STATUS_DONE:
2449 chunk_appendf(&trash,
2450 "<p><div class=active_up>"
2451 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2452 "Action processed successfully."
2453 "</div>\n", uri->uri_prefix,
2454 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2455 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2456 scope_txt);
2457 break;
2458 case STAT_STATUS_NONE:
2459 chunk_appendf(&trash,
2460 "<p><div class=active_going_down>"
2461 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2462 "Nothing has changed."
2463 "</div>\n", uri->uri_prefix,
2464 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2465 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2466 scope_txt);
2467 break;
2468 case STAT_STATUS_PART:
2469 chunk_appendf(&trash,
2470 "<p><div class=active_going_down>"
2471 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2472 "Action partially processed.<br>"
2473 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2474 "</div>\n", uri->uri_prefix,
2475 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2476 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2477 scope_txt);
2478 break;
2479 case STAT_STATUS_ERRP:
2480 chunk_appendf(&trash,
2481 "<p><div class=active_down>"
2482 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2483 "Action not processed because of invalid parameters."
2484 "<ul>"
2485 "<li>The action is maybe unknown.</li>"
2486 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2487 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2488 "</ul>"
2489 "</div>\n", uri->uri_prefix,
2490 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2491 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2492 scope_txt);
2493 break;
2494 case STAT_STATUS_EXCD:
2495 chunk_appendf(&trash,
2496 "<p><div class=active_down>"
2497 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2498 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2499 "You should retry with less servers at a time.</b>"
2500 "</div>\n", uri->uri_prefix,
2501 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2502 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2503 scope_txt);
2504 break;
2505 case STAT_STATUS_DENY:
2506 chunk_appendf(&trash,
2507 "<p><div class=active_down>"
2508 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2509 "<b>Action denied.</b>"
2510 "</div>\n", uri->uri_prefix,
2511 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2512 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2513 scope_txt);
2514 break;
2515 default:
2516 chunk_appendf(&trash,
2517 "<p><div class=active_no_check>"
2518 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2519 "Unexpected result."
2520 "</div>\n", uri->uri_prefix,
2521 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2522 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2523 scope_txt);
2524 }
2525 chunk_appendf(&trash, "<p>\n");
2526 }
2527}
2528
2529/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2530 * for clearing the trash if needed.
2531 */
2532static void stats_dump_html_end()
2533{
2534 chunk_appendf(&trash, "</body></html>\n");
2535}
2536
Simon Horman05ee2132017-01-04 09:37:25 +01002537/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2538 * for clearing it if needed.
2539 */
2540static void stats_dump_json_header()
2541{
2542 chunk_strcat(&trash, "[");
2543}
2544
2545
2546/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2547 * for clearing the trash if needed.
2548 */
2549static void stats_dump_json_end()
2550{
2551 chunk_strcat(&trash, "]");
2552}
2553
William Lallemand74c24fb2016-11-21 17:18:36 +01002554/* This function dumps statistics onto the stream interface's read buffer in
2555 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2556 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2557 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2558 * and the stream must be closed, or -1 in case of any error. This function is
2559 * used by both the CLI and the HTTP handlers.
2560 */
Christopher Fauletef779222018-10-31 08:47:01 +01002561static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2562 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002563{
2564 struct appctx *appctx = __objt_appctx(si->end);
2565 struct channel *rep = si_ic(si);
2566 struct proxy *px;
2567
2568 chunk_reset(&trash);
2569
2570 switch (appctx->st2) {
2571 case STAT_ST_INIT:
2572 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2573 /* fall through */
2574
2575 case STAT_ST_HEAD:
2576 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2577 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002578 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002579 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002580 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2581 stats_dump_csv_header();
2582
Christopher Fauletef779222018-10-31 08:47:01 +01002583 if (!stats_putchk(rep, htx, &trash))
2584 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002585
2586 appctx->st2 = STAT_ST_INFO;
2587 /* fall through */
2588
2589 case STAT_ST_INFO:
2590 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2591 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002592 if (!stats_putchk(rep, htx, &trash))
2593 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002594 }
2595
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002596 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002597 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2598 appctx->st2 = STAT_ST_LIST;
2599 /* fall through */
2600
2601 case STAT_ST_LIST:
2602 /* dump proxies */
2603 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002604 if (htx) {
2605 if (htx_almost_full(htx))
2606 goto full;
2607 }
2608 else {
2609 if (buffer_almost_full(&rep->buf))
2610 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002611 }
2612
2613 px = appctx->ctx.stats.px;
2614 /* skip the disabled proxies, global frontend and non-networked ones */
2615 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002616 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002617 return 0;
2618
2619 appctx->ctx.stats.px = px->next;
2620 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2621 }
2622 /* here, we just have reached the last proxy */
2623
2624 appctx->st2 = STAT_ST_END;
2625 /* fall through */
2626
2627 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002628 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2629 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2630 stats_dump_html_end();
2631 else
2632 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002633 if (!stats_putchk(rep, htx, &trash))
2634 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002635 }
2636
2637 appctx->st2 = STAT_ST_FIN;
2638 /* fall through */
2639
2640 case STAT_ST_FIN:
2641 return 1;
2642
2643 default:
2644 /* unknown state ! */
2645 appctx->st2 = STAT_ST_FIN;
2646 return -1;
2647 }
Christopher Fauletef779222018-10-31 08:47:01 +01002648
2649 full:
2650 si_rx_room_blk(si);
2651 return 0;
2652
William Lallemand74c24fb2016-11-21 17:18:36 +01002653}
2654
2655/* We reached the stats page through a POST request. The appctx is
2656 * expected to have already been allocated by the caller.
2657 * Parse the posted data and enable/disable servers if necessary.
2658 * Returns 1 if request was parsed or zero if it needs more data.
2659 */
2660static int stats_process_http_post(struct stream_interface *si)
2661{
2662 struct stream *s = si_strm(si);
2663 struct appctx *appctx = objt_appctx(si->end);
2664
2665 struct proxy *px = NULL;
2666 struct server *sv = NULL;
2667
2668 char key[LINESIZE];
2669 int action = ST_ADM_ACTION_NONE;
2670 int reprocess = 0;
2671
2672 int total_servers = 0;
2673 int altered_servers = 0;
2674
2675 char *first_param, *cur_param, *next_param, *end_params;
2676 char *st_cur_param = NULL;
2677 char *st_next_param = NULL;
2678
Willy Tarreau83061a82018-07-13 11:56:34 +02002679 struct buffer *temp;
William Lallemand74c24fb2016-11-21 17:18:36 +01002680 int reql;
2681
2682 temp = get_trash_chunk();
2683 if (temp->size < s->txn->req.body_len) {
2684 /* too large request */
2685 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2686 goto out;
2687 }
2688
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002689 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2690 s->txn->req.eoh + 2);
William Lallemand74c24fb2016-11-21 17:18:36 +01002691 if (reql <= 0) {
2692 /* we need more data */
2693 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2694 return 0;
2695 }
2696
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002697 first_param = temp->area;
2698 end_params = temp->area + reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002699 cur_param = next_param = end_params;
2700 *end_params = '\0';
2701
2702 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2703
2704 /*
2705 * Parse the parameters in reverse order to only store the last value.
2706 * From the html form, the backend and the action are at the end.
2707 */
2708 while (cur_param > first_param) {
2709 char *value;
2710 int poffset, plen;
2711
2712 cur_param--;
2713
2714 if ((*cur_param == '&') || (cur_param == first_param)) {
2715 reprocess_servers:
2716 /* Parse the key */
2717 poffset = (cur_param != first_param ? 1 : 0);
2718 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2719 if ((plen > 0) && (plen <= sizeof(key))) {
2720 strncpy(key, cur_param + poffset, plen);
2721 key[plen - 1] = '\0';
2722 } else {
2723 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2724 goto out;
2725 }
2726
2727 /* Parse the value */
2728 value = key;
2729 while (*value != '\0' && *value != '=') {
2730 value++;
2731 }
2732 if (*value == '=') {
2733 /* Ok, a value is found, we can mark the end of the key */
2734 *value++ = '\0';
2735 }
2736 if (url_decode(key) < 0 || url_decode(value) < 0)
2737 break;
2738
2739 /* Now we can check the key to see what to do */
2740 if (!px && (strcmp(key, "b") == 0)) {
2741 if ((px = proxy_be_by_name(value)) == NULL) {
2742 /* the backend name is unknown or ambiguous (duplicate names) */
2743 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2744 goto out;
2745 }
2746 }
2747 else if (!action && (strcmp(key, "action") == 0)) {
2748 if (strcmp(value, "ready") == 0) {
2749 action = ST_ADM_ACTION_READY;
2750 }
2751 else if (strcmp(value, "drain") == 0) {
2752 action = ST_ADM_ACTION_DRAIN;
2753 }
2754 else if (strcmp(value, "maint") == 0) {
2755 action = ST_ADM_ACTION_MAINT;
2756 }
2757 else if (strcmp(value, "shutdown") == 0) {
2758 action = ST_ADM_ACTION_SHUTDOWN;
2759 }
2760 else if (strcmp(value, "dhlth") == 0) {
2761 action = ST_ADM_ACTION_DHLTH;
2762 }
2763 else if (strcmp(value, "ehlth") == 0) {
2764 action = ST_ADM_ACTION_EHLTH;
2765 }
2766 else if (strcmp(value, "hrunn") == 0) {
2767 action = ST_ADM_ACTION_HRUNN;
2768 }
2769 else if (strcmp(value, "hnolb") == 0) {
2770 action = ST_ADM_ACTION_HNOLB;
2771 }
2772 else if (strcmp(value, "hdown") == 0) {
2773 action = ST_ADM_ACTION_HDOWN;
2774 }
2775 else if (strcmp(value, "dagent") == 0) {
2776 action = ST_ADM_ACTION_DAGENT;
2777 }
2778 else if (strcmp(value, "eagent") == 0) {
2779 action = ST_ADM_ACTION_EAGENT;
2780 }
2781 else if (strcmp(value, "arunn") == 0) {
2782 action = ST_ADM_ACTION_ARUNN;
2783 }
2784 else if (strcmp(value, "adown") == 0) {
2785 action = ST_ADM_ACTION_ADOWN;
2786 }
2787 /* else these are the old supported methods */
2788 else if (strcmp(value, "disable") == 0) {
2789 action = ST_ADM_ACTION_DISABLE;
2790 }
2791 else if (strcmp(value, "enable") == 0) {
2792 action = ST_ADM_ACTION_ENABLE;
2793 }
2794 else if (strcmp(value, "stop") == 0) {
2795 action = ST_ADM_ACTION_STOP;
2796 }
2797 else if (strcmp(value, "start") == 0) {
2798 action = ST_ADM_ACTION_START;
2799 }
2800 else {
2801 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2802 goto out;
2803 }
2804 }
2805 else if (strcmp(key, "s") == 0) {
2806 if (!(px && action)) {
2807 /*
2808 * Indicates that we'll need to reprocess the parameters
2809 * as soon as backend and action are known
2810 */
2811 if (!reprocess) {
2812 st_cur_param = cur_param;
2813 st_next_param = next_param;
2814 }
2815 reprocess = 1;
2816 }
2817 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002818 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002819 switch (action) {
2820 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002821 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002822 altered_servers++;
2823 total_servers++;
2824 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2825 }
2826 break;
2827 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002828 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002829 altered_servers++;
2830 total_servers++;
2831 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2832 }
2833 break;
2834 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002835 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002836 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2837 altered_servers++;
2838 total_servers++;
2839 }
2840 break;
2841 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002842 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002843 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2844 altered_servers++;
2845 total_servers++;
2846 }
2847 break;
2848 case ST_ADM_ACTION_DHLTH:
2849 if (sv->check.state & CHK_ST_CONFIGURED) {
2850 sv->check.state &= ~CHK_ST_ENABLED;
2851 altered_servers++;
2852 total_servers++;
2853 }
2854 break;
2855 case ST_ADM_ACTION_EHLTH:
2856 if (sv->check.state & CHK_ST_CONFIGURED) {
2857 sv->check.state |= CHK_ST_ENABLED;
2858 altered_servers++;
2859 total_servers++;
2860 }
2861 break;
2862 case ST_ADM_ACTION_HRUNN:
2863 if (!(sv->track)) {
2864 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002865 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002866 altered_servers++;
2867 total_servers++;
2868 }
2869 break;
2870 case ST_ADM_ACTION_HNOLB:
2871 if (!(sv->track)) {
2872 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002873 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002874 altered_servers++;
2875 total_servers++;
2876 }
2877 break;
2878 case ST_ADM_ACTION_HDOWN:
2879 if (!(sv->track)) {
2880 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002881 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002882 altered_servers++;
2883 total_servers++;
2884 }
2885 break;
2886 case ST_ADM_ACTION_DAGENT:
2887 if (sv->agent.state & CHK_ST_CONFIGURED) {
2888 sv->agent.state &= ~CHK_ST_ENABLED;
2889 altered_servers++;
2890 total_servers++;
2891 }
2892 break;
2893 case ST_ADM_ACTION_EAGENT:
2894 if (sv->agent.state & CHK_ST_CONFIGURED) {
2895 sv->agent.state |= CHK_ST_ENABLED;
2896 altered_servers++;
2897 total_servers++;
2898 }
2899 break;
2900 case ST_ADM_ACTION_ARUNN:
2901 if (sv->agent.state & CHK_ST_ENABLED) {
2902 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002903 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002904 altered_servers++;
2905 total_servers++;
2906 }
2907 break;
2908 case ST_ADM_ACTION_ADOWN:
2909 if (sv->agent.state & CHK_ST_ENABLED) {
2910 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002911 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002912 altered_servers++;
2913 total_servers++;
2914 }
2915 break;
2916 case ST_ADM_ACTION_READY:
2917 srv_adm_set_ready(sv);
2918 altered_servers++;
2919 total_servers++;
2920 break;
2921 case ST_ADM_ACTION_DRAIN:
2922 srv_adm_set_drain(sv);
2923 altered_servers++;
2924 total_servers++;
2925 break;
2926 case ST_ADM_ACTION_MAINT:
2927 srv_adm_set_maint(sv);
2928 altered_servers++;
2929 total_servers++;
2930 break;
2931 case ST_ADM_ACTION_SHUTDOWN:
2932 if (px->state != PR_STSTOPPED) {
2933 struct stream *sess, *sess_bck;
2934
2935 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2936 if (sess->srv_conn == sv)
2937 stream_shutdown(sess, SF_ERR_KILLED);
2938
2939 altered_servers++;
2940 total_servers++;
2941 }
2942 break;
2943 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002944 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002945 } else {
2946 /* the server name is unknown or ambiguous (duplicate names) */
2947 total_servers++;
2948 }
2949 }
2950 if (reprocess && px && action) {
2951 /* Now, we know the backend and the action chosen by the user.
2952 * We can safely restart from the first server parameter
2953 * to reprocess them
2954 */
2955 cur_param = st_cur_param;
2956 next_param = st_next_param;
2957 reprocess = 0;
2958 goto reprocess_servers;
2959 }
2960
2961 next_param = cur_param;
2962 }
2963 }
2964
2965 if (total_servers == 0) {
2966 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2967 }
2968 else if (altered_servers == 0) {
2969 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2970 }
2971 else if (altered_servers == total_servers) {
2972 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
2973 }
2974 else {
2975 appctx->ctx.stats.st_code = STAT_STATUS_PART;
2976 }
2977 out:
2978 return 1;
Christopher Fauletef779222018-10-31 08:47:01 +01002979}
2980
2981
2982static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
2983{
2984 struct stream *s = si_strm(si);
2985 struct uri_auth *uri = s->be->uri_auth;
2986 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002987 struct htx_sl *sl;
2988 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01002989
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002990 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_ENC|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
2991 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
2992 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01002993 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002994 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01002995
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);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003037 struct htx_sl *sl;
3038 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003039
3040 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3041 scope_txt[0] = 0;
3042 if (appctx->ctx.stats.scope_len) {
3043 strcpy(scope_txt, STAT_SCOPE_PATTERN);
3044 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), co_head(si_oc(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3045 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3046 }
3047
3048 /* We don't want to land on the posted stats page because a refresh will
3049 * repost the data. We don't want this to happen on accident so we redirect
3050 * the browse to the stats page with a GET.
3051 */
3052 chunk_printf(&trash, "%s;st=%s%s%s%s",
3053 uri->uri_prefix,
3054 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3055 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3056 stat_status_codes[appctx->ctx.stats.st_code]) ?
3057 stat_status_codes[appctx->ctx.stats.st_code] :
3058 stat_status_codes[STAT_STATUS_UNKN],
3059 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3060 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3061 scope_txt);
3062
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003063 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3064 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3065 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003066 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003067 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003068
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
Christopher Fauletb1b08212018-11-21 13:51:07 +01003223 /* Don't add EOD and TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003224 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3225 si_rx_room_blk(si);
3226 goto out;
3227 }
3228
3229 /* eat the whole request */
3230 req_htx = htx_from_buf(&req->buf);
3231 blk = htx_get_head_blk(req_htx);
3232 while (blk) {
3233 enum htx_blk_type type = htx_get_blk_type(blk);
3234
3235 blk = htx_remove_blk(req_htx, blk);
3236 if (type == HTX_BLK_EOM)
3237 break;
3238 }
Christopher Faulet9b95d312018-11-19 21:58:10 +01003239 co_set_data(req, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01003240 if (htx_is_empty(req_htx)) {
3241 htx_reset(req_htx);
3242 b_set_data(&req->buf, 0);
3243 }
3244 res->flags |= CF_READ_NULL;
3245 si_shutr(si);
3246 }
3247
3248 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3249 si_shutw(si);
3250
3251 if (appctx->st0 == STAT_HTTP_DONE) {
3252 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3253 si_shutr(si);
3254 res->flags |= CF_READ_NULL;
3255 }
3256 }
3257 out:
3258 /* we have left the request in the buffer for the case where we
3259 * process a POST, and this automatically re-enables activity on
3260 * read. It's better to indicate that we want to stop reading when
3261 * we're sending, so that we know there's at most one direction
3262 * deciding to wake the applet up. It saves it from looping when
3263 * emitting large blocks into small TCP windows.
3264 */
3265 if (htx_is_empty(res_htx)) {
3266 htx_reset(res_htx);
3267 b_set_data(&res->buf, 0);
3268 }
3269 else {
3270 b_set_data(&res->buf, b_size(&res->buf));
3271 si_stop_get(si);
3272 }
3273}
3274
3275
3276/* This I/O handler runs as an applet embedded in a stream interface. It is
3277 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3278 * appctx->st0 contains the operation in progress (dump, done). The handler
3279 * automatically unregisters itself once transfer is complete.
3280 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003281static void http_stats_io_handler(struct appctx *appctx)
3282{
3283 struct stream_interface *si = appctx->owner;
3284 struct stream *s = si_strm(si);
3285 struct channel *req = si_oc(si);
3286 struct channel *res = si_ic(si);
3287
Christopher Fauletef779222018-10-31 08:47:01 +01003288 if (IS_HTX_STRM(s))
3289 return htx_stats_io_handler(appctx);
3290
William Lallemand74c24fb2016-11-21 17:18:36 +01003291 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3292 goto out;
3293
Joseph Herlant29023ec2018-11-15 13:39:46 -08003294 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003295 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003296 /* already subscribed, we'll be called later once the buffer is
3297 * available.
3298 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003299 goto out;
3300 }
3301
William Lallemand74c24fb2016-11-21 17:18:36 +01003302 /* check that the output is not closed */
3303 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3304 appctx->st0 = STAT_HTTP_DONE;
3305
3306 /* all states are processed in sequence */
3307 if (appctx->st0 == STAT_HTTP_HEAD) {
3308 if (stats_send_http_headers(si)) {
3309 if (s->txn->meth == HTTP_METH_HEAD)
3310 appctx->st0 = STAT_HTTP_DONE;
3311 else
3312 appctx->st0 = STAT_HTTP_DUMP;
3313 }
3314 }
3315
3316 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003317 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003318 unsigned int data_len;
3319 unsigned int last_len;
3320 unsigned int last_fwd = 0;
3321
3322 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3323 /* One difficulty we're facing is that we must prevent
3324 * the input data from being automatically forwarded to
3325 * the output area. For this, we temporarily disable
3326 * forwarding on the channel.
3327 */
3328 last_fwd = si_ic(si)->to_forward;
3329 si_ic(si)->to_forward = 0;
3330 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003331 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003332 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003333 si_ic(si)->to_forward = last_fwd;
3334 goto out;
3335 }
3336 }
3337
Willy Tarreau97f538b2018-06-15 19:41:31 +02003338 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003339 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003340 appctx->st0 = STAT_HTTP_DONE;
3341
Willy Tarreau97f538b2018-06-15 19:41:31 +02003342 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003343
3344 /* Now we must either adjust or remove the chunk size. This is
3345 * not easy because the chunk size might wrap at the end of the
3346 * buffer, so we pretend we have nothing in the buffer, we write
3347 * the size, then restore the buffer's contents. Note that we can
3348 * only do that because no forwarding is scheduled on the stats
3349 * applet.
3350 */
3351 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3352 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003353 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003354
3355 if (last_len != data_len) {
3356 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003357 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003358 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003359
3360 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003361 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003362 }
3363 /* now re-enable forwarding */
3364 channel_forward(si_ic(si), last_fwd);
3365 }
3366 }
3367
3368 if (appctx->st0 == STAT_HTTP_POST) {
3369 if (stats_process_http_post(si))
3370 appctx->st0 = STAT_HTTP_LAST;
3371 else if (si_oc(si)->flags & CF_SHUTR)
3372 appctx->st0 = STAT_HTTP_DONE;
3373 }
3374
3375 if (appctx->st0 == STAT_HTTP_LAST) {
3376 if (stats_send_http_redirect(si))
3377 appctx->st0 = STAT_HTTP_DONE;
3378 }
3379
3380 if (appctx->st0 == STAT_HTTP_DONE) {
3381 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3382 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003383 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003384 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003385 goto out;
3386 }
3387 }
3388 /* eat the whole request */
Willy Tarreau97f538b2018-06-15 19:41:31 +02003389 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003390 res->flags |= CF_READ_NULL;
3391 si_shutr(si);
3392 }
3393
3394 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3395 si_shutw(si);
3396
3397 if (appctx->st0 == STAT_HTTP_DONE) {
3398 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3399 si_shutr(si);
3400 res->flags |= CF_READ_NULL;
3401 }
3402 }
3403 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003404 /* we have left the request in the buffer for the case where we
3405 * process a POST, and this automatically re-enables activity on
3406 * read. It's better to indicate that we want to stop reading when
3407 * we're sending, so that we know there's at most one direction
3408 * deciding to wake the applet up. It saves it from looping when
3409 * emitting large blocks into small TCP windows.
3410 */
3411 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003412 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003413}
3414
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003415/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003416static int stats_dump_info_fields(struct buffer *out,
3417 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003418{
3419 int field;
3420
3421 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3422 if (!field_format(info, field))
3423 continue;
3424
3425 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3426 return 0;
3427 if (!stats_emit_raw_data_field(out, &info[field]))
3428 return 0;
3429 if (!chunk_strcat(out, "\n"))
3430 return 0;
3431 }
3432 return 1;
3433}
3434
3435/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003436static int stats_dump_typed_info_fields(struct buffer *out,
3437 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003438{
3439 int field;
3440
3441 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3442 if (!field_format(info, field))
3443 continue;
3444
3445 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3446 return 0;
3447 if (!stats_emit_field_tags(out, &info[field], ':'))
3448 return 0;
3449 if (!stats_emit_typed_data_field(out, &info[field]))
3450 return 0;
3451 if (!chunk_strcat(out, "\n"))
3452 return 0;
3453 }
3454 return 1;
3455}
3456
3457/* Fill <info> with HAProxy global info. <info> is preallocated
3458 * array of length <len>. The length of the aray must be
3459 * INF_TOTAL_FIELDS. If this length is less then this value, the
3460 * function returns 0, otherwise, it returns 1.
3461 */
3462int stats_fill_info(struct field *info, int len)
3463{
3464 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003465 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003466
3467#ifdef USE_OPENSSL
3468 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3469 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3470 int ssl_reuse = 0;
3471
3472 if (ssl_key_rate < ssl_sess_rate) {
3473 /* count the ssl reuse ratio and avoid overflows in both directions */
3474 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3475 }
3476#endif
3477
3478 if (len < INF_TOTAL_FIELDS)
3479 return 0;
3480
3481 chunk_reset(out);
3482 memset(info, 0, sizeof(*info) * len);
3483
3484 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3485 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3486 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3487
Yves Lafon95317282018-02-26 11:10:37 +01003488 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003489 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3490 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3491 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3492
3493 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3494 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3495
3496 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3497 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3498 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3499 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3500 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3501 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3502 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3503 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3504 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3505 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3506 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3507 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3508#ifdef USE_OPENSSL
3509 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3510 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3511 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3512#endif
3513 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3514 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3515 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3516 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3517 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3518 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3519 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3520 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3521 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3522
3523#ifdef USE_OPENSSL
3524 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3525 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3526 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3527 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3528 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3529 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3530 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3531 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3532 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3533 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3534#endif
3535 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3536 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3537 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3538#ifdef USE_ZLIB
3539 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3540 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3541#endif
3542 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003543 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003544 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3545 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3546 if (global.desc)
3547 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003548 info[INF_STOPPING] = mkf_u32(0, stopping);
3549 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003550 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003551 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003552 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003553 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003554 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003555 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003556
3557 return 1;
3558}
3559
3560/* This function dumps information onto the stream interface's read buffer.
3561 * It returns 0 as long as it does not complete, non-zero upon completion.
3562 * No state is used.
3563 */
3564static int stats_dump_info_to_buffer(struct stream_interface *si)
3565{
3566 struct appctx *appctx = __objt_appctx(si->end);
3567
3568 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3569 return 0;
3570
3571 chunk_reset(&trash);
3572
3573 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3574 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003575 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3576 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003577 else
3578 stats_dump_info_fields(&trash, info);
3579
Willy Tarreau06d80a92017-10-19 14:32:15 +02003580 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003581 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003582 return 0;
3583 }
3584
3585 return 1;
3586}
3587
Simon Horman6f6bb382017-01-04 09:37:26 +01003588/* This function dumps the schema onto the stream interface's read buffer.
3589 * It returns 0 as long as it does not complete, non-zero upon completion.
3590 * No state is used.
3591 *
3592 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3593 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3594 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003595static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003596{
3597
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003598 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003599
3600 chunk_strcat(out,
3601 "{"
3602 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3603 "\"oneOf\":["
3604 "{"
3605 "\"title\":\"Info\","
3606 "\"type\":\"array\","
3607 "\"items\":{"
3608 "\"properties\":{"
3609 "\"title\":\"InfoItem\","
3610 "\"type\":\"object\","
3611 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3612 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3613 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3614 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3615 "},"
3616 "\"required\":[\"field\",\"processNum\",\"tags\","
3617 "\"value\"]"
3618 "}"
3619 "},"
3620 "{"
3621 "\"title\":\"Stat\","
3622 "\"type\":\"array\","
3623 "\"items\":{"
3624 "\"title\":\"InfoItem\","
3625 "\"type\":\"object\","
3626 "\"properties\":{"
3627 "\"objType\":{"
3628 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3629 "\"Server\",\"Unknown\"]"
3630 "},"
3631 "\"proxyId\":{"
3632 "\"type\":\"integer\","
3633 "\"minimum\":0"
3634 "},"
3635 "\"id\":{"
3636 "\"type\":\"integer\","
3637 "\"minimum\":0"
3638 "},"
3639 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3640 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3641 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3642 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3643 "},"
3644 "\"required\":[\"objType\",\"proxyId\",\"id\","
3645 "\"field\",\"processNum\",\"tags\","
3646 "\"value\"]"
3647 "}"
3648 "},"
3649 "{"
3650 "\"title\":\"Error\","
3651 "\"type\":\"object\","
3652 "\"properties\":{"
3653 "\"errorStr\":{"
3654 "\"type\":\"string\""
3655 "},"
3656 "\"required\":[\"errorStr\"]"
3657 "}"
3658 "}"
3659 "],"
3660 "\"definitions\":{"
3661 "\"field\":{"
3662 "\"type\":\"object\","
3663 "\"pos\":{"
3664 "\"type\":\"integer\","
3665 "\"minimum\":0"
3666 "},"
3667 "\"name\":{"
3668 "\"type\":\"string\""
3669 "},"
3670 "\"required\":[\"pos\",\"name\"]"
3671 "},"
3672 "\"processNum\":{"
3673 "\"type\":\"integer\","
3674 "\"minimum\":1"
3675 "},"
3676 "\"tags\":{"
3677 "\"type\":\"object\","
3678 "\"origin\":{"
3679 "\"type\":\"string\","
3680 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3681 "\"Config\",\"Product\",\"Unknown\"]"
3682 "},"
3683 "\"nature\":{"
3684 "\"type\":\"string\","
3685 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3686 "\"Rate\",\"Counter\",\"Duration\","
3687 "\"Age\",\"Time\",\"Name\",\"Output\","
3688 "\"Avg\", \"Unknown\"]"
3689 "},"
3690 "\"scope\":{"
3691 "\"type\":\"string\","
3692 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3693 "\"System\",\"Unknown\"]"
3694 "},"
3695 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3696 "},"
3697 "\"typedValue\":{"
3698 "\"type\":\"object\","
3699 "\"oneOf\":["
3700 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3701 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3702 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3703 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3704 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3705 "],"
3706 "\"definitions\":{"
3707 "\"s32Value\":{"
3708 "\"properties\":{"
3709 "\"type\":{"
3710 "\"type\":\"string\","
3711 "\"enum\":[\"s32\"]"
3712 "},"
3713 "\"value\":{"
3714 "\"type\":\"integer\","
3715 "\"minimum\":-2147483648,"
3716 "\"maximum\":2147483647"
3717 "}"
3718 "},"
3719 "\"required\":[\"type\",\"value\"]"
3720 "},"
3721 "\"s64Value\":{"
3722 "\"properties\":{"
3723 "\"type\":{"
3724 "\"type\":\"string\","
3725 "\"enum\":[\"s64\"]"
3726 "},"
3727 "\"value\":{"
3728 "\"type\":\"integer\","
3729 "\"minimum\":-9007199254740991,"
3730 "\"maximum\":9007199254740991"
3731 "}"
3732 "},"
3733 "\"required\":[\"type\",\"value\"]"
3734 "},"
3735 "\"u32Value\":{"
3736 "\"properties\":{"
3737 "\"type\":{"
3738 "\"type\":\"string\","
3739 "\"enum\":[\"u32\"]"
3740 "},"
3741 "\"value\":{"
3742 "\"type\":\"integer\","
3743 "\"minimum\":0,"
3744 "\"maximum\":4294967295"
3745 "}"
3746 "},"
3747 "\"required\":[\"type\",\"value\"]"
3748 "},"
3749 "\"u64Value\":{"
3750 "\"properties\":{"
3751 "\"type\":{"
3752 "\"type\":\"string\","
3753 "\"enum\":[\"u64\"]"
3754 "},"
3755 "\"value\":{"
3756 "\"type\":\"integer\","
3757 "\"minimum\":0,"
3758 "\"maximum\":9007199254740991"
3759 "}"
3760 "},"
3761 "\"required\":[\"type\",\"value\"]"
3762 "},"
3763 "\"strValue\":{"
3764 "\"properties\":{"
3765 "\"type\":{"
3766 "\"type\":\"string\","
3767 "\"enum\":[\"str\"]"
3768 "},"
3769 "\"value\":{\"type\":\"string\"}"
3770 "},"
3771 "\"required\":[\"type\",\"value\"]"
3772 "},"
3773 "\"unknownValue\":{"
3774 "\"properties\":{"
3775 "\"type\":{"
3776 "\"type\":\"integer\","
3777 "\"minimum\":0"
3778 "},"
3779 "\"value\":{"
3780 "\"type\":\"string\","
3781 "\"enum\":[\"unknown\"]"
3782 "}"
3783 "},"
3784 "\"required\":[\"type\",\"value\"]"
3785 "}"
3786 "}"
3787 "}"
3788 "}"
3789 "}");
3790
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003791 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003792 chunk_reset(out);
3793 chunk_appendf(out,
3794 "{\"errorStr\":\"output buffer too short\"}");
3795 }
3796}
3797
3798/* This function dumps the schema onto the stream interface's read buffer.
3799 * It returns 0 as long as it does not complete, non-zero upon completion.
3800 * No state is used.
3801 */
3802static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3803{
3804 chunk_reset(&trash);
3805
3806 stats_dump_json_schema(&trash);
3807
Willy Tarreau06d80a92017-10-19 14:32:15 +02003808 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003809 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003810 return 0;
3811 }
3812
3813 return 1;
3814}
3815
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003816static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003817{
3818 struct proxy *px;
3819 struct server *sv;
3820 struct listener *li;
3821 int clrall = 0;
3822
3823 if (strcmp(args[2], "all") == 0)
3824 clrall = 1;
3825
3826 /* check permissions */
3827 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3828 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3829 return 1;
3830
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003831 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003832 if (clrall) {
3833 memset(&px->be_counters, 0, sizeof(px->be_counters));
3834 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3835 }
3836 else {
3837 px->be_counters.conn_max = 0;
3838 px->be_counters.p.http.rps_max = 0;
3839 px->be_counters.sps_max = 0;
3840 px->be_counters.cps_max = 0;
3841 px->be_counters.nbpend_max = 0;
3842
3843 px->fe_counters.conn_max = 0;
3844 px->fe_counters.p.http.rps_max = 0;
3845 px->fe_counters.sps_max = 0;
3846 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003847 }
3848
3849 for (sv = px->srv; sv; sv = sv->next)
3850 if (clrall)
3851 memset(&sv->counters, 0, sizeof(sv->counters));
3852 else {
3853 sv->counters.cur_sess_max = 0;
3854 sv->counters.nbpend_max = 0;
3855 sv->counters.sps_max = 0;
3856 }
3857
3858 list_for_each_entry(li, &px->conf.listeners, by_fe)
3859 if (li->counters) {
3860 if (clrall)
3861 memset(li->counters, 0, sizeof(*li->counters));
3862 else
3863 li->counters->conn_max = 0;
3864 }
3865 }
3866
3867 global.cps_max = 0;
3868 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003869 global.ssl_max = 0;
3870 global.ssl_fe_keys_max = 0;
3871 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003872
3873 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003874 return 1;
3875}
3876
3877
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003878static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003879{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003880 appctx->ctx.stats.scope_str = 0;
3881 appctx->ctx.stats.scope_len = 0;
3882 appctx->ctx.stats.flags = 0;
3883
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003884 if (strcmp(args[2], "typed") == 0)
3885 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003886 else if (strcmp(args[2], "json") == 0)
3887 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003888 return 0;
3889}
3890
3891
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003892static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003893{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003894 appctx->ctx.stats.scope_str = 0;
3895 appctx->ctx.stats.scope_len = 0;
3896 appctx->ctx.stats.flags = 0;
3897
Willy Tarreau2b812e22016-11-22 16:18:05 +01003898 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003899 struct proxy *px;
3900
3901 px = proxy_find_by_name(args[2], 0, 0);
3902 if (px)
3903 appctx->ctx.stats.iid = px->uuid;
3904 else
3905 appctx->ctx.stats.iid = atoi(args[2]);
3906
3907 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003908 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003909 appctx->ctx.cli.msg = "No such proxy.\n";
3910 appctx->st0 = CLI_ST_PRINT;
3911 return 1;
3912 }
3913
Willy Tarreau2b812e22016-11-22 16:18:05 +01003914 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003915 appctx->ctx.stats.type = atoi(args[3]);
3916 appctx->ctx.stats.sid = atoi(args[4]);
3917 if (strcmp(args[5], "typed") == 0)
3918 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003919 else if (strcmp(args[5], "json") == 0)
3920 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003921 }
3922 else if (strcmp(args[2], "typed") == 0)
3923 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003924 else if (strcmp(args[2], "json") == 0)
3925 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003926
Willy Tarreau2b812e22016-11-22 16:18:05 +01003927 return 0;
3928}
3929
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003930static int cli_io_handler_dump_info(struct appctx *appctx)
3931{
3932 return stats_dump_info_to_buffer(appctx->owner);
3933}
3934
Willy Tarreau2b812e22016-11-22 16:18:05 +01003935/* This I/O handler runs as an applet embedded in a stream interface. It is
3936 * used to send raw stats over a socket.
3937 */
3938static int cli_io_handler_dump_stat(struct appctx *appctx)
3939{
Christopher Fauletef779222018-10-31 08:47:01 +01003940 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003941}
3942
Simon Horman6f6bb382017-01-04 09:37:26 +01003943static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3944{
3945 return stats_dump_json_schema_to_buffer(appctx->owner);
3946}
3947
Willy Tarreau2b812e22016-11-22 16:18:05 +01003948/* register cli keywords */
3949static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003950 { { "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 +01003951 { { "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 +01003952 { { "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 +01003953 { { "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 +01003954 {{},}
3955}};
3956
Willy Tarreau0108d902018-11-25 19:14:37 +01003957INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3958
William Lallemand74c24fb2016-11-21 17:18:36 +01003959struct applet http_stats_applet = {
3960 .obj_type = OBJ_TYPE_APPLET,
3961 .name = "<STATS>", /* used for logging */
3962 .fct = http_stats_io_handler,
3963 .release = NULL,
3964};
3965
William Lallemand74c24fb2016-11-21 17:18:36 +01003966/*
3967 * Local variables:
3968 * c-indent-level: 8
3969 * c-basic-offset: 8
3970 * End:
3971 */