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