blob: 3ee168912584a8ecaa8e80fd81c4e3be2cee99db [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
27#include <common/cfgparse.h>
28#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020031#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010032#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010033#include <common/initcall.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
37#include <common/ticks.h>
38#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
41#include <common/base64.h>
42
43#include <types/applet.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010044#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010045#include <types/global.h>
46#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010048
49#include <proto/backend.h>
50#include <proto/channel.h>
51#include <proto/checks.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010052#include <proto/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010053#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010054#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010055#include <proto/fd.h>
56#include <proto/freq_ctr.h>
57#include <proto/frontend.h>
58#include <proto/log.h>
59#include <proto/pattern.h>
60#include <proto/pipe.h>
61#include <proto/listener.h>
62#include <proto/map.h>
Christopher Fauletef779222018-10-31 08:47:01 +010063#include <proto/proto_http.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010064#include <proto/proxy.h>
65#include <proto/sample.h>
66#include <proto/session.h>
67#include <proto/stream.h>
68#include <proto/server.h>
69#include <proto/raw_sock.h>
70#include <proto/stream_interface.h>
71#include <proto/task.h>
72
73#ifdef USE_OPENSSL
74#include <proto/ssl_sock.h>
75#include <types/ssl_sock.h>
76#endif
77
78
Willy Tarreau0baac8c2016-11-22 16:36:53 +010079/* These are the field names for each INF_* field position. Please pay attention
80 * to always use the exact same name except that the strings for new names must
81 * be lower case or CamelCase while the enum entries must be upper case.
82 */
83const char *info_field_names[INF_TOTAL_FIELDS] = {
84 [INF_NAME] = "Name",
85 [INF_VERSION] = "Version",
86 [INF_RELEASE_DATE] = "Release_date",
Yves Lafon95317282018-02-26 11:10:37 +010087 [INF_NBTHREAD] = "Nbthread",
Willy Tarreau0baac8c2016-11-22 16:36:53 +010088 [INF_NBPROC] = "Nbproc",
89 [INF_PROCESS_NUM] = "Process_num",
90 [INF_PID] = "Pid",
91 [INF_UPTIME] = "Uptime",
92 [INF_UPTIME_SEC] = "Uptime_sec",
93 [INF_MEMMAX_MB] = "Memmax_MB",
94 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
95 [INF_POOL_USED_MB] = "PoolUsed_MB",
96 [INF_POOL_FAILED] = "PoolFailed",
97 [INF_ULIMIT_N] = "Ulimit-n",
98 [INF_MAXSOCK] = "Maxsock",
99 [INF_MAXCONN] = "Maxconn",
100 [INF_HARD_MAXCONN] = "Hard_maxconn",
101 [INF_CURR_CONN] = "CurrConns",
102 [INF_CUM_CONN] = "CumConns",
103 [INF_CUM_REQ] = "CumReq",
104 [INF_MAX_SSL_CONNS] = "MaxSslConns",
105 [INF_CURR_SSL_CONNS] = "CurrSslConns",
106 [INF_CUM_SSL_CONNS] = "CumSslConns",
107 [INF_MAXPIPES] = "Maxpipes",
108 [INF_PIPES_USED] = "PipesUsed",
109 [INF_PIPES_FREE] = "PipesFree",
110 [INF_CONN_RATE] = "ConnRate",
111 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
112 [INF_MAX_CONN_RATE] = "MaxConnRate",
113 [INF_SESS_RATE] = "SessRate",
114 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
115 [INF_MAX_SESS_RATE] = "MaxSessRate",
116 [INF_SSL_RATE] = "SslRate",
117 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
118 [INF_MAX_SSL_RATE] = "MaxSslRate",
119 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
120 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
121 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
122 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
123 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
124 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
125 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
126 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
127 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
128 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
129 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
130 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
131 [INF_TASKS] = "Tasks",
132 [INF_RUN_QUEUE] = "Run_queue",
133 [INF_IDLE_PCT] = "Idle_pct",
134 [INF_NODE] = "node",
135 [INF_DESCRIPTION] = "description",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100136 [INF_STOPPING] = "Stopping",
137 [INF_JOBS] = "Jobs",
William Lallemanda7199262018-11-16 16:57:20 +0100138 [INF_UNSTOPPABLE_JOBS] = "Unstoppable Jobs",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100139 [INF_LISTENERS] = "Listeners",
Willy Tarreau199ad242018-11-05 16:31:22 +0100140 [INF_ACTIVE_PEERS] = "ActivePeers",
Willy Tarreau2d372c22018-11-05 17:12:27 +0100141 [INF_CONNECTED_PEERS] = "ConnectedPeers",
Willy Tarreau13ef7732018-11-12 07:25:28 +0100142 [INF_DROPPED_LOGS] = "DroppedLogs",
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100143 [INF_BUSY_POLLING] = "BusyPolling",
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100144};
145
William Lallemand74c24fb2016-11-21 17:18:36 +0100146const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
147 [ST_F_PXNAME] = "pxname",
148 [ST_F_SVNAME] = "svname",
149 [ST_F_QCUR] = "qcur",
150 [ST_F_QMAX] = "qmax",
151 [ST_F_SCUR] = "scur",
152 [ST_F_SMAX] = "smax",
153 [ST_F_SLIM] = "slim",
154 [ST_F_STOT] = "stot",
155 [ST_F_BIN] = "bin",
156 [ST_F_BOUT] = "bout",
157 [ST_F_DREQ] = "dreq",
158 [ST_F_DRESP] = "dresp",
159 [ST_F_EREQ] = "ereq",
160 [ST_F_ECON] = "econ",
161 [ST_F_ERESP] = "eresp",
162 [ST_F_WRETR] = "wretr",
163 [ST_F_WREDIS] = "wredis",
164 [ST_F_STATUS] = "status",
165 [ST_F_WEIGHT] = "weight",
166 [ST_F_ACT] = "act",
167 [ST_F_BCK] = "bck",
168 [ST_F_CHKFAIL] = "chkfail",
169 [ST_F_CHKDOWN] = "chkdown",
170 [ST_F_LASTCHG] = "lastchg",
171 [ST_F_DOWNTIME] = "downtime",
172 [ST_F_QLIMIT] = "qlimit",
173 [ST_F_PID] = "pid",
174 [ST_F_IID] = "iid",
175 [ST_F_SID] = "sid",
176 [ST_F_THROTTLE] = "throttle",
177 [ST_F_LBTOT] = "lbtot",
178 [ST_F_TRACKED] = "tracked",
179 [ST_F_TYPE] = "type",
180 [ST_F_RATE] = "rate",
181 [ST_F_RATE_LIM] = "rate_lim",
182 [ST_F_RATE_MAX] = "rate_max",
183 [ST_F_CHECK_STATUS] = "check_status",
184 [ST_F_CHECK_CODE] = "check_code",
185 [ST_F_CHECK_DURATION] = "check_duration",
186 [ST_F_HRSP_1XX] = "hrsp_1xx",
187 [ST_F_HRSP_2XX] = "hrsp_2xx",
188 [ST_F_HRSP_3XX] = "hrsp_3xx",
189 [ST_F_HRSP_4XX] = "hrsp_4xx",
190 [ST_F_HRSP_5XX] = "hrsp_5xx",
191 [ST_F_HRSP_OTHER] = "hrsp_other",
192 [ST_F_HANAFAIL] = "hanafail",
193 [ST_F_REQ_RATE] = "req_rate",
194 [ST_F_REQ_RATE_MAX] = "req_rate_max",
195 [ST_F_REQ_TOT] = "req_tot",
196 [ST_F_CLI_ABRT] = "cli_abrt",
197 [ST_F_SRV_ABRT] = "srv_abrt",
198 [ST_F_COMP_IN] = "comp_in",
199 [ST_F_COMP_OUT] = "comp_out",
200 [ST_F_COMP_BYP] = "comp_byp",
201 [ST_F_COMP_RSP] = "comp_rsp",
202 [ST_F_LASTSESS] = "lastsess",
203 [ST_F_LAST_CHK] = "last_chk",
204 [ST_F_LAST_AGT] = "last_agt",
205 [ST_F_QTIME] = "qtime",
206 [ST_F_CTIME] = "ctime",
207 [ST_F_RTIME] = "rtime",
208 [ST_F_TTIME] = "ttime",
209 [ST_F_AGENT_STATUS] = "agent_status",
210 [ST_F_AGENT_CODE] = "agent_code",
211 [ST_F_AGENT_DURATION] = "agent_duration",
212 [ST_F_CHECK_DESC] = "check_desc",
213 [ST_F_AGENT_DESC] = "agent_desc",
214 [ST_F_CHECK_RISE] = "check_rise",
215 [ST_F_CHECK_FALL] = "check_fall",
216 [ST_F_CHECK_HEALTH] = "check_health",
217 [ST_F_AGENT_RISE] = "agent_rise",
218 [ST_F_AGENT_FALL] = "agent_fall",
219 [ST_F_AGENT_HEALTH] = "agent_health",
220 [ST_F_ADDR] = "addr",
221 [ST_F_COOKIE] = "cookie",
222 [ST_F_MODE] = "mode",
223 [ST_F_ALGO] = "algo",
224 [ST_F_CONN_RATE] = "conn_rate",
225 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
226 [ST_F_CONN_TOT] = "conn_tot",
227 [ST_F_INTERCEPTED] = "intercepted",
228 [ST_F_DCON] = "dcon",
229 [ST_F_DSES] = "dses",
Tim Duesterhus3fd19732018-05-27 20:35:08 +0200230 [ST_F_WREW] = "wrew",
Willy Tarreauf1573842018-12-14 11:35:36 +0100231 [ST_F_CONNECT] = "connect",
232 [ST_F_REUSE] = "reuse",
Willy Tarreaua1214a52018-12-14 14:00:25 +0100233 [ST_F_CACHE_LOOKUPS] = "cache_lookups",
234 [ST_F_CACHE_HITS] = "cache_hits",
William Lallemand74c24fb2016-11-21 17:18:36 +0100235};
236
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100237/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100238static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100239/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100240static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100241
242
Christopher Fauletef779222018-10-31 08:47:01 +0100243static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
244{
245 if (htx) {
246 if (!htx_add_data(htx, ist2(chk->area, chk->data)))
247 return 0;
248 chn->total += chk->data;
249 chk->data = 0;
250 /* notify that some data was read from the SI into the buffer */
251 chn->flags |= CF_READ_PARTIAL;
252 }
253 else {
254 if (ci_putchk(chn, chk) == -1)
255 return 0;
256 }
257 return 1;
258}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100259
William Lallemand74c24fb2016-11-21 17:18:36 +0100260/*
261 * http_stats_io_handler()
262 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
263 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100264 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100265 * -> stats_dump_html_head() // emits the HTML headers
266 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
267 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
268 * -> stats_dump_html_px_hdr()
269 * -> stats_dump_fe_stats()
270 * -> stats_dump_li_stats()
271 * -> stats_dump_sv_stats()
272 * -> stats_dump_be_stats()
273 * -> stats_dump_html_px_end()
274 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100275 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100276 */
277
278
279extern const char *stat_status_codes[];
280
281/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
282 * for clearing it if needed.
283 * NOTE: Some tools happen to rely on the field position instead of its name,
284 * so please only append new fields at the end, never in the middle.
285 */
286static void stats_dump_csv_header()
287{
288 int field;
289
290 chunk_appendf(&trash, "# ");
291 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
292 chunk_appendf(&trash, "%s,", stat_field_names[field]);
293
294 chunk_appendf(&trash, "\n");
295}
296
297
298/* Emits a stats field without any surrounding element and properly encoded to
299 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
300 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200301int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100302{
303 switch (field_format(f, 0)) {
304 case FF_EMPTY: return 1;
305 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
306 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
307 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
308 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
309 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
310 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
311 }
312}
313
314/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
315 * output is supposed to be used on its own line. Returns non-zero on success, 0
316 * if the buffer is full.
317 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200318int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100319{
320 switch (field_format(f, 0)) {
321 case FF_EMPTY: return 1;
322 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
323 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
324 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
325 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
326 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
327 default: return chunk_appendf(out, "%08x:?", f->type);
328 }
329}
330
Simon Horman05ee2132017-01-04 09:37:25 +0100331/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
332 * the recommendation for interoperable integers in section 6 of RFC 7159.
333 */
334#define JSON_INT_MAX ((1ULL << 53) - 1)
335#define JSON_INT_MIN (0 - JSON_INT_MAX)
336
337/* Emits a stats field value and its type in JSON.
338 * Returns non-zero on success, 0 on error.
339 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200340int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100341{
342 int old_len;
343 char buf[20];
344 const char *type, *value = buf, *quote = "";
345
346 switch (field_format(f, 0)) {
347 case FF_EMPTY: return 1;
348 case FF_S32: type = "\"s32\"";
349 snprintf(buf, sizeof(buf), "%d", f->u.s32);
350 break;
351 case FF_U32: type = "\"u32\"";
352 snprintf(buf, sizeof(buf), "%u", f->u.u32);
353 break;
354 case FF_S64: type = "\"s64\"";
355 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
356 return 0;
357 type = "\"s64\"";
358 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
359 break;
360 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
361 return 0;
362 type = "\"u64\"";
363 snprintf(buf, sizeof(buf), "%llu",
364 (unsigned long long) f->u.u64);
365 break;
366 case FF_STR: type = "\"str\"";
367 value = field_str(f, 0);
368 quote = "\"";
369 break;
370 default: snprintf(buf, sizeof(buf), "%u", f->type);
371 type = buf;
372 value = "unknown";
373 quote = "\"";
374 break;
375 }
376
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200377 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100378 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
379 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200380 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100381}
382
William Lallemand74c24fb2016-11-21 17:18:36 +0100383/* Emits an encoding of the field type on 3 characters followed by a delimiter.
384 * Returns non-zero on success, 0 if the buffer is full.
385 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200386int stats_emit_field_tags(struct buffer *out, const struct field *f,
387 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100388{
389 char origin, nature, scope;
390
391 switch (field_origin(f, 0)) {
392 case FO_METRIC: origin = 'M'; break;
393 case FO_STATUS: origin = 'S'; break;
394 case FO_KEY: origin = 'K'; break;
395 case FO_CONFIG: origin = 'C'; break;
396 case FO_PRODUCT: origin = 'P'; break;
397 default: origin = '?'; break;
398 }
399
400 switch (field_nature(f, 0)) {
401 case FN_GAUGE: nature = 'G'; break;
402 case FN_LIMIT: nature = 'L'; break;
403 case FN_MIN: nature = 'm'; break;
404 case FN_MAX: nature = 'M'; break;
405 case FN_RATE: nature = 'R'; break;
406 case FN_COUNTER: nature = 'C'; break;
407 case FN_DURATION: nature = 'D'; break;
408 case FN_AGE: nature = 'A'; break;
409 case FN_TIME: nature = 'T'; break;
410 case FN_NAME: nature = 'N'; break;
411 case FN_OUTPUT: nature = 'O'; break;
412 case FN_AVG: nature = 'a'; break;
413 default: nature = '?'; break;
414 }
415
416 switch (field_scope(f, 0)) {
417 case FS_PROCESS: scope = 'P'; break;
418 case FS_SERVICE: scope = 'S'; break;
419 case FS_SYSTEM: scope = 's'; break;
420 case FS_CLUSTER: scope = 'C'; break;
421 default: scope = '?'; break;
422 }
423
424 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
425}
426
Simon Horman05ee2132017-01-04 09:37:25 +0100427/* Emits an encoding of the field type as JSON.
428 * Returns non-zero on success, 0 if the buffer is full.
429 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200430int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100431{
432 const char *origin, *nature, *scope;
433 int old_len;
434
435 switch (field_origin(f, 0)) {
436 case FO_METRIC: origin = "Metric"; break;
437 case FO_STATUS: origin = "Status"; break;
438 case FO_KEY: origin = "Key"; break;
439 case FO_CONFIG: origin = "Config"; break;
440 case FO_PRODUCT: origin = "Product"; break;
441 default: origin = "Unknown"; break;
442 }
443
444 switch (field_nature(f, 0)) {
445 case FN_GAUGE: nature = "Gauge"; break;
446 case FN_LIMIT: nature = "Limit"; break;
447 case FN_MIN: nature = "Min"; break;
448 case FN_MAX: nature = "Max"; break;
449 case FN_RATE: nature = "Rate"; break;
450 case FN_COUNTER: nature = "Counter"; break;
451 case FN_DURATION: nature = "Duration"; break;
452 case FN_AGE: nature = "Age"; break;
453 case FN_TIME: nature = "Time"; break;
454 case FN_NAME: nature = "Name"; break;
455 case FN_OUTPUT: nature = "Output"; break;
456 case FN_AVG: nature = "Avg"; break;
457 default: nature = "Unknown"; break;
458 }
459
460 switch (field_scope(f, 0)) {
461 case FS_PROCESS: scope = "Process"; break;
462 case FS_SERVICE: scope = "Service"; break;
463 case FS_SYSTEM: scope = "System"; break;
464 case FS_CLUSTER: scope = "Cluster"; break;
465 default: scope = "Unknown"; break;
466 }
467
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200468 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100469 chunk_appendf(out, "\"tags\":{"
470 "\"origin\":\"%s\","
471 "\"nature\":\"%s\","
472 "\"scope\":\"%s\""
473 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200474 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100475}
William Lallemand74c24fb2016-11-21 17:18:36 +0100476
477/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200478static int stats_dump_fields_csv(struct buffer *out,
479 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100480{
481 int field;
482
483 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
484 if (!stats_emit_raw_data_field(out, &stats[field]))
485 return 0;
486 if (!chunk_strcat(out, ","))
487 return 0;
488 }
489 chunk_strcat(out, "\n");
490 return 1;
491}
492
493/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200494static int stats_dump_fields_typed(struct buffer *out,
495 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100496{
497 int field;
498
499 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
500 if (!stats[field].type)
501 continue;
502
503 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
504 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
505 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
506 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
507 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
508 '?',
509 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
510 field, stat_field_names[field], stats[ST_F_PID].u.u32);
511
512 if (!stats_emit_field_tags(out, &stats[field], ':'))
513 return 0;
514 if (!stats_emit_typed_data_field(out, &stats[field]))
515 return 0;
516 if (!chunk_strcat(out, "\n"))
517 return 0;
518 }
519 return 1;
520}
521
Simon Horman05ee2132017-01-04 09:37:25 +0100522/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200523static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100524 const struct field *info)
525{
526 int field;
527 int started = 0;
528
529 if (!chunk_strcat(out, "["))
530 return 0;
531
532 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
533 int old_len;
534
535 if (!field_format(info, field))
536 continue;
537
538 if (started && !chunk_strcat(out, ","))
539 goto err;
540 started = 1;
541
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200542 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100543 chunk_appendf(out,
544 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
545 "\"processNum\":%u,",
546 field, info_field_names[field],
547 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200548 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100549 goto err;
550
551 if (!stats_emit_json_field_tags(out, &info[field]))
552 goto err;
553
554 if (!stats_emit_json_data_field(out, &info[field]))
555 goto err;
556
557 if (!chunk_strcat(out, "}"))
558 goto err;
559 }
560
561 if (!chunk_strcat(out, "]"))
562 goto err;
563 return 1;
564
565err:
566 chunk_reset(out);
567 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
568 return 0;
569}
570
571/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200572static int stats_dump_fields_json(struct buffer *out,
573 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100574 int first_stat)
575{
576 int field;
577 int started = 0;
578
579 if (!first_stat && !chunk_strcat(out, ","))
580 return 0;
581 if (!chunk_strcat(out, "["))
582 return 0;
583
584 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
585 const char *obj_type;
586 int old_len;
587
588 if (!stats[field].type)
589 continue;
590
591 if (started && !chunk_strcat(out, ","))
592 goto err;
593 started = 1;
594
595 switch (stats[ST_F_TYPE].u.u32) {
596 case STATS_TYPE_FE: obj_type = "Frontend"; break;
597 case STATS_TYPE_BE: obj_type = "Backend"; break;
598 case STATS_TYPE_SO: obj_type = "Listener"; break;
599 case STATS_TYPE_SV: obj_type = "Server"; break;
600 default: obj_type = "Unknown"; break;
601 }
602
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200603 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100604 chunk_appendf(out,
605 "{"
606 "\"objType\":\"%s\","
607 "\"proxyId\":%d,"
608 "\"id\":%d,"
609 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
610 "\"processNum\":%u,",
611 obj_type, stats[ST_F_IID].u.u32,
612 stats[ST_F_SID].u.u32, field,
613 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200614 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100615 goto err;
616
617 if (!stats_emit_json_field_tags(out, &stats[field]))
618 goto err;
619
620 if (!stats_emit_json_data_field(out, &stats[field]))
621 goto err;
622
623 if (!chunk_strcat(out, "}"))
624 goto err;
625 }
626
627 if (!chunk_strcat(out, "]"))
628 goto err;
629
630 return 1;
631
632err:
633 chunk_reset(out);
634 if (!first_stat)
635 chunk_strcat(out, ",");
636 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
637 return 0;
638}
639
William Lallemand74c24fb2016-11-21 17:18:36 +0100640/* Dump all fields from <stats> into <out> using the HTML format. A column is
641 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
642 * are provided if ST_SHLGNDS is present in <flags>.
643 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200644static int stats_dump_fields_html(struct buffer *out,
645 const struct field *stats,
646 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100647{
Willy Tarreau83061a82018-07-13 11:56:34 +0200648 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100649
650 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
651 chunk_appendf(out,
652 /* name, queue */
653 "<tr class=\"frontend\">");
654
655 if (flags & ST_SHOWADMIN) {
656 /* Column sub-heading for Enable or Disable server */
657 chunk_appendf(out, "<td></td>");
658 }
659
660 chunk_appendf(out,
661 "<td class=ac>"
662 "<a name=\"%s/Frontend\"></a>"
663 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
664 "<td colspan=3></td>"
665 "",
666 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
667
668 chunk_appendf(out,
669 /* sessions rate : current */
670 "<td><u>%s<div class=tips><table class=det>"
671 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
672 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
673 "",
674 U2H(stats[ST_F_RATE].u.u32),
675 U2H(stats[ST_F_CONN_RATE].u.u32),
676 U2H(stats[ST_F_RATE].u.u32));
677
678 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
679 chunk_appendf(out,
680 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
681 U2H(stats[ST_F_REQ_RATE].u.u32));
682
683 chunk_appendf(out,
684 "</table></div></u></td>"
685 /* sessions rate : max */
686 "<td><u>%s<div class=tips><table class=det>"
687 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
688 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
689 "",
690 U2H(stats[ST_F_RATE_MAX].u.u32),
691 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
692 U2H(stats[ST_F_RATE_MAX].u.u32));
693
694 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
695 chunk_appendf(out,
696 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
697 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
698
699 chunk_appendf(out,
700 "</table></div></u></td>"
701 /* sessions rate : limit */
702 "<td>%s</td>",
703 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
704
705 chunk_appendf(out,
706 /* sessions: current, max, limit, total */
707 "<td>%s</td><td>%s</td><td>%s</td>"
708 "<td><u>%s<div class=tips><table class=det>"
709 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
710 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
711 "",
712 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
713 U2H(stats[ST_F_STOT].u.u64),
714 U2H(stats[ST_F_CONN_TOT].u.u64),
715 U2H(stats[ST_F_STOT].u.u64));
716
717 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
718 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
719 chunk_appendf(out,
720 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
721 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
722 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
723 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
724 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
725 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
726 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
727 "<tr><th>- other responses:</th><td>%s</td></tr>"
728 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100729 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
730 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200731 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100732 "",
733 U2H(stats[ST_F_REQ_TOT].u.u64),
734 U2H(stats[ST_F_HRSP_1XX].u.u64),
735 U2H(stats[ST_F_HRSP_2XX].u.u64),
736 U2H(stats[ST_F_COMP_RSP].u.u64),
737 stats[ST_F_HRSP_2XX].u.u64 ?
738 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
739 U2H(stats[ST_F_HRSP_3XX].u.u64),
740 U2H(stats[ST_F_HRSP_4XX].u.u64),
741 U2H(stats[ST_F_HRSP_5XX].u.u64),
742 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200743 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100744 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
745 U2H(stats[ST_F_CACHE_HITS].u.u64),
746 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
747 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200748 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100749 }
750
751 chunk_appendf(out,
752 "</table></div></u></td>"
753 /* sessions: lbtot, lastsess */
754 "<td></td><td></td>"
755 /* bytes : in */
756 "<td>%s</td>"
757 "",
758 U2H(stats[ST_F_BIN].u.u64));
759
760 chunk_appendf(out,
761 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
762 "<td>%s%s<div class=tips><table class=det>"
763 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
764 "<tr><th>Compression in:</th><td>%s</td></tr>"
765 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
766 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
767 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
768 "</table></div>%s</td>",
769 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
770 U2H(stats[ST_F_BOUT].u.u64),
771 U2H(stats[ST_F_BOUT].u.u64),
772 U2H(stats[ST_F_COMP_IN].u.u64),
773 U2H(stats[ST_F_COMP_OUT].u.u64),
774 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
775 U2H(stats[ST_F_COMP_BYP].u.u64),
776 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
777 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,
778 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
779
780 chunk_appendf(out,
781 /* denied: req, resp */
782 "<td>%s</td><td>%s</td>"
783 /* errors : request, connect, response */
784 "<td>%s</td><td></td><td></td>"
785 /* warnings: retries, redispatches */
786 "<td></td><td></td>"
787 /* server status : reflect frontend status */
788 "<td class=ac>%s</td>"
789 /* rest of server: nothing */
790 "<td class=ac colspan=8></td></tr>"
791 "",
792 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
793 U2H(stats[ST_F_EREQ].u.u64),
794 field_str(stats, ST_F_STATUS));
795 }
796 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
797 chunk_appendf(out, "<tr class=socket>");
798 if (flags & ST_SHOWADMIN) {
799 /* Column sub-heading for Enable or Disable server */
800 chunk_appendf(out, "<td></td>");
801 }
802
803 chunk_appendf(out,
804 /* frontend name, listener name */
805 "<td class=ac><a name=\"%s/+%s\"></a>%s"
806 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
807 "",
808 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
809 (flags & ST_SHLGNDS)?"<u>":"",
810 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
811
812 if (flags & ST_SHLGNDS) {
813 chunk_appendf(out, "<div class=tips>");
814
815 if (isdigit(*field_str(stats, ST_F_ADDR)))
816 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
817 else if (*field_str(stats, ST_F_ADDR) == '[')
818 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
819 else if (*field_str(stats, ST_F_ADDR))
820 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
821
822 /* id */
823 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
824 }
825
826 chunk_appendf(out,
827 /* queue */
828 "%s</td><td colspan=3></td>"
829 /* sessions rate: current, max, limit */
830 "<td colspan=3>&nbsp;</td>"
831 /* sessions: current, max, limit, total, lbtot, lastsess */
832 "<td>%s</td><td>%s</td><td>%s</td>"
833 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
834 /* bytes: in, out */
835 "<td>%s</td><td>%s</td>"
836 "",
837 (flags & ST_SHLGNDS)?"</u>":"",
838 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
839 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
840
841 chunk_appendf(out,
842 /* denied: req, resp */
843 "<td>%s</td><td>%s</td>"
844 /* errors: request, connect, response */
845 "<td>%s</td><td></td><td></td>"
846 /* warnings: retries, redispatches */
847 "<td></td><td></td>"
848 /* server status: reflect listener status */
849 "<td class=ac>%s</td>"
850 /* rest of server: nothing */
851 "<td class=ac colspan=8></td></tr>"
852 "",
853 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
854 U2H(stats[ST_F_EREQ].u.u64),
855 field_str(stats, ST_F_STATUS));
856 }
857 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
858 const char *style;
859
860 /* determine the style to use depending on the server's state,
861 * its health and weight. There isn't a 1-to-1 mapping between
862 * state and styles for the cases where the server is (still)
863 * up. The reason is that we don't want to report nolb and
864 * drain with the same color.
865 */
866
867 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
868 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
869 style = "down";
870 }
871 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
872 style = "going_up";
873 }
874 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
875 style = "going_down";
876 }
877 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
878 style = "nolb";
879 }
880 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
881 style = "no_check";
882 }
883 else if (!stats[ST_F_CHKFAIL].type ||
884 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
885 /* no check or max health = UP */
886 if (stats[ST_F_WEIGHT].u.u32)
887 style = "up";
888 else
889 style = "draining";
890 }
891 else {
892 style = "going_down";
893 }
894
895 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
896 chunk_appendf(out, "<tr class=\"maintain\">");
897 else
898 chunk_appendf(out,
899 "<tr class=\"%s_%s\">",
900 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
901
902
903 if (flags & ST_SHOWADMIN)
904 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000905 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
906 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100907 field_str(stats, ST_F_SVNAME));
908
909 chunk_appendf(out,
910 "<td class=ac><a name=\"%s/%s\"></a>%s"
911 "<a class=lfsb href=\"#%s/%s\">%s</a>"
912 "",
913 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
914 (flags & ST_SHLGNDS) ? "<u>" : "",
915 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
916
917 if (flags & ST_SHLGNDS) {
918 chunk_appendf(out, "<div class=tips>");
919
920 if (isdigit(*field_str(stats, ST_F_ADDR)))
921 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
922 else if (*field_str(stats, ST_F_ADDR) == '[')
923 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
924 else if (*field_str(stats, ST_F_ADDR))
925 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
926
927 /* id */
928 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
929
930 /* cookie */
931 if (stats[ST_F_COOKIE].type) {
932 chunk_appendf(out, ", cookie: '");
933 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
934 chunk_htmlencode(out, &src);
935 chunk_appendf(out, "'");
936 }
937
938 chunk_appendf(out, "</div>");
939 }
940
941 chunk_appendf(out,
942 /* queue : current, max, limit */
943 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
944 /* sessions rate : current, max, limit */
945 "<td>%s</td><td>%s</td><td></td>"
946 "",
947 (flags & ST_SHLGNDS) ? "</u>" : "",
948 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
949 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
950
951 chunk_appendf(out,
952 /* sessions: current, max, limit, total */
953 "<td>%s</td><td>%s</td><td>%s</td>"
954 "<td><u>%s<div class=tips><table class=det>"
955 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
956 "",
957 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
958 U2H(stats[ST_F_STOT].u.u64),
959 U2H(stats[ST_F_STOT].u.u64));
960
961 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
962 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
963 unsigned long long tot;
964
965 tot = stats[ST_F_HRSP_OTHER].u.u64;
966 tot += stats[ST_F_HRSP_1XX].u.u64;
967 tot += stats[ST_F_HRSP_2XX].u.u64;
968 tot += stats[ST_F_HRSP_3XX].u.u64;
969 tot += stats[ST_F_HRSP_4XX].u.u64;
970 tot += stats[ST_F_HRSP_5XX].u.u64;
971
972 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +0100973 "<tr><th>New connections:</th><td>%s</td></tr>"
974 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100975 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
976 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
977 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
978 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
979 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
980 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
981 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200982 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100983 "",
Willy Tarreauf1573842018-12-14 11:35:36 +0100984 U2H(stats[ST_F_CONNECT].u.u64),
985 U2H(stats[ST_F_REUSE].u.u64),
986 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
987 (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0,
William Lallemand74c24fb2016-11-21 17:18:36 +0100988 U2H(tot),
989 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
990 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
991 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
992 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
993 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 +0200994 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
995 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100996 }
997
998 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
999 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1000 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1001 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1002 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1003 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1004
1005 chunk_appendf(out,
1006 "</table></div></u></td>"
1007 /* sessions: lbtot, last */
1008 "<td>%s</td><td>%s</td>",
1009 U2H(stats[ST_F_LBTOT].u.u64),
1010 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1011
1012 chunk_appendf(out,
1013 /* bytes : in, out */
1014 "<td>%s</td><td>%s</td>"
1015 /* denied: req, resp */
1016 "<td></td><td>%s</td>"
1017 /* errors : request, connect */
1018 "<td></td><td>%s</td>"
1019 /* errors : response */
1020 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1021 /* warnings: retries, redispatches */
1022 "<td>%lld</td><td>%lld</td>"
1023 "",
1024 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1025 U2H(stats[ST_F_DRESP].u.u64),
1026 U2H(stats[ST_F_ECON].u.u64),
1027 U2H(stats[ST_F_ERESP].u.u64),
1028 (long long)stats[ST_F_CLI_ABRT].u.u64,
1029 (long long)stats[ST_F_SRV_ABRT].u.u64,
1030 (long long)stats[ST_F_WRETR].u.u64,
1031 (long long)stats[ST_F_WREDIS].u.u64);
1032
1033 /* status, last change */
1034 chunk_appendf(out, "<td class=ac>");
1035
1036 /* FIXME!!!!
1037 * LASTCHG should contain the last change for *this* server and must be computed
1038 * properly above, as was done below, ie: this server if maint, otherwise ref server
1039 * if tracking. Note that ref is either local or remote depending on tracking.
1040 */
1041
1042
1043 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1044 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1045 }
1046 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1047 chunk_strcat(out, "<i>no check</i>");
1048 }
1049 else {
1050 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1051 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1052 if (stats[ST_F_CHECK_HEALTH].u.u32)
1053 chunk_strcat(out, " &uarr;");
1054 }
1055 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1056 chunk_strcat(out, " &darr;");
1057 }
1058
1059 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1060 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1061 chunk_appendf(out,
1062 "</td><td class=ac><u> %s",
1063 field_str(stats, ST_F_AGENT_STATUS));
1064
1065 if (stats[ST_F_AGENT_CODE].type)
1066 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1067
1068 if (stats[ST_F_AGENT_DURATION].type)
1069 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1070
1071 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1072
1073 if (*field_str(stats, ST_F_LAST_AGT)) {
1074 chunk_appendf(out, ": ");
1075 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1076 chunk_htmlencode(out, &src);
1077 }
1078 chunk_appendf(out, "</div></u>");
1079 }
1080 else if (stats[ST_F_CHECK_STATUS].type) {
1081 chunk_appendf(out,
1082 "</td><td class=ac><u> %s",
1083 field_str(stats, ST_F_CHECK_STATUS));
1084
1085 if (stats[ST_F_CHECK_CODE].type)
1086 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1087
1088 if (stats[ST_F_CHECK_DURATION].type)
1089 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1090
1091 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1092
1093 if (*field_str(stats, ST_F_LAST_CHK)) {
1094 chunk_appendf(out, ": ");
1095 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1096 chunk_htmlencode(out, &src);
1097 }
1098 chunk_appendf(out, "</div></u>");
1099 }
1100 else
1101 chunk_appendf(out, "</td><td>");
1102
1103 chunk_appendf(out,
1104 /* weight */
1105 "</td><td class=ac>%d</td>"
1106 /* act, bck */
1107 "<td class=ac>%s</td><td class=ac>%s</td>"
1108 "",
1109 stats[ST_F_WEIGHT].u.u32,
1110 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1111 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1112
1113 /* check failures: unique, fatal, down time */
1114 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1115 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1116 }
1117 else if (stats[ST_F_CHKFAIL].type) {
1118 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1119
1120 if (stats[ST_F_HANAFAIL].type)
1121 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1122
1123 chunk_appendf(out,
1124 "<div class=tips>Failed Health Checks%s</div></u></td>"
1125 "<td>%lld</td><td>%s</td>"
1126 "",
1127 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1128 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1129 }
1130 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1131 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1132 chunk_appendf(out,
1133 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1134 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1135 }
1136 else
1137 chunk_appendf(out, "<td colspan=3></td>");
1138
1139 /* throttle */
1140 if (stats[ST_F_THROTTLE].type)
1141 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1142 else
1143 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1144 }
1145 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1146 chunk_appendf(out, "<tr class=\"backend\">");
1147 if (flags & ST_SHOWADMIN) {
1148 /* Column sub-heading for Enable or Disable server */
1149 chunk_appendf(out, "<td></td>");
1150 }
1151 chunk_appendf(out,
1152 "<td class=ac>"
1153 /* name */
1154 "%s<a name=\"%s/Backend\"></a>"
1155 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1156 "",
1157 (flags & ST_SHLGNDS)?"<u>":"",
1158 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1159
1160 if (flags & ST_SHLGNDS) {
1161 /* balancing */
1162 chunk_appendf(out, "<div class=tips>balancing: %s",
1163 field_str(stats, ST_F_ALGO));
1164
1165 /* cookie */
1166 if (stats[ST_F_COOKIE].type) {
1167 chunk_appendf(out, ", cookie: '");
1168 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1169 chunk_htmlencode(out, &src);
1170 chunk_appendf(out, "'");
1171 }
1172 chunk_appendf(out, "</div>");
1173 }
1174
1175 chunk_appendf(out,
1176 "%s</td>"
1177 /* queue : current, max */
1178 "<td>%s</td><td>%s</td><td></td>"
1179 /* sessions rate : current, max, limit */
1180 "<td>%s</td><td>%s</td><td></td>"
1181 "",
1182 (flags & ST_SHLGNDS)?"</u>":"",
1183 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1184 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1185
1186 chunk_appendf(out,
1187 /* sessions: current, max, limit, total */
1188 "<td>%s</td><td>%s</td><td>%s</td>"
1189 "<td><u>%s<div class=tips><table class=det>"
1190 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1191 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001192 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 +01001193 U2H(stats[ST_F_STOT].u.u64),
1194 U2H(stats[ST_F_STOT].u.u64));
1195
1196 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1197 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1198 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001199 "<tr><th>New connections:</th><td>%s</td></tr>"
1200 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001201 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1202 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1203 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1204 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1205 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1206 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1207 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1208 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001209 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1210 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001211 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001212 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1213 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001214 U2H(stats[ST_F_CONNECT].u.u64),
1215 U2H(stats[ST_F_REUSE].u.u64),
1216 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1217 (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0,
William Lallemand74c24fb2016-11-21 17:18:36 +01001218 U2H(stats[ST_F_REQ_TOT].u.u64),
1219 U2H(stats[ST_F_HRSP_1XX].u.u64),
1220 U2H(stats[ST_F_HRSP_2XX].u.u64),
1221 U2H(stats[ST_F_COMP_RSP].u.u64),
1222 stats[ST_F_HRSP_2XX].u.u64 ?
1223 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1224 U2H(stats[ST_F_HRSP_3XX].u.u64),
1225 U2H(stats[ST_F_HRSP_4XX].u.u64),
1226 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001227 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001228 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1229 U2H(stats[ST_F_CACHE_HITS].u.u64),
1230 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1231 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001232 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001233 }
1234
1235 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1236 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1237 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1238 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1239 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1240
1241 chunk_appendf(out,
1242 "</table></div></u></td>"
1243 /* sessions: lbtot, last */
1244 "<td>%s</td><td>%s</td>"
1245 /* bytes: in */
1246 "<td>%s</td>"
1247 "",
1248 U2H(stats[ST_F_LBTOT].u.u64),
1249 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1250 U2H(stats[ST_F_BIN].u.u64));
1251
1252 chunk_appendf(out,
1253 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1254 "<td>%s%s<div class=tips><table class=det>"
1255 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1256 "<tr><th>Compression in:</th><td>%s</td></tr>"
1257 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1258 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1259 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1260 "</table></div>%s</td>",
1261 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1262 U2H(stats[ST_F_BOUT].u.u64),
1263 U2H(stats[ST_F_BOUT].u.u64),
1264 U2H(stats[ST_F_COMP_IN].u.u64),
1265 U2H(stats[ST_F_COMP_OUT].u.u64),
1266 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1267 U2H(stats[ST_F_COMP_BYP].u.u64),
1268 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1269 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,
1270 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1271
1272 chunk_appendf(out,
1273 /* denied: req, resp */
1274 "<td>%s</td><td>%s</td>"
1275 /* errors : request, connect */
1276 "<td></td><td>%s</td>"
1277 /* errors : response */
1278 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1279 /* warnings: retries, redispatches */
1280 "<td>%lld</td><td>%lld</td>"
1281 /* backend status: reflect backend status (up/down): we display UP
1282 * if the backend has known working servers or if it has no server at
1283 * all (eg: for stats). Then we display the total weight, number of
1284 * active and backups. */
1285 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1286 "<td class=ac>%d</td><td class=ac>%d</td>"
1287 "",
1288 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1289 U2H(stats[ST_F_ECON].u.u64),
1290 U2H(stats[ST_F_ERESP].u.u64),
1291 (long long)stats[ST_F_CLI_ABRT].u.u64,
1292 (long long)stats[ST_F_SRV_ABRT].u.u64,
1293 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1294 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1295 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1296 stats[ST_F_WEIGHT].u.u32,
1297 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1298
1299 chunk_appendf(out,
1300 /* rest of backend: nothing, down transitions, total downtime, throttle */
1301 "<td class=ac>&nbsp;</td><td>%d</td>"
1302 "<td>%s</td>"
1303 "<td></td>"
1304 "</tr>",
1305 stats[ST_F_CHKDOWN].u.u32,
1306 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1307 }
1308 return 1;
1309}
1310
1311int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1312{
Simon Horman05ee2132017-01-04 09:37:25 +01001313 int ret;
1314
William Lallemand74c24fb2016-11-21 17:18:36 +01001315 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1316 flags |= ST_SHOWADMIN;
1317
1318 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001319 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001320 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001321 ret = stats_dump_fields_typed(&trash, stats);
1322 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1323 ret = stats_dump_fields_json(&trash, stats,
1324 !(appctx->ctx.stats.flags &
1325 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001326 else
Simon Horman05ee2132017-01-04 09:37:25 +01001327 ret = stats_dump_fields_csv(&trash, stats);
1328
1329 if (ret)
1330 appctx->ctx.stats.flags |= STAT_STARTED;
1331
1332 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001333}
1334
1335/* Fill <stats> with the frontend statistics. <stats> is
1336 * preallocated array of length <len>. The length of the array
1337 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1338 * this value, the function returns 0, otherwise, it returns 1.
1339 */
1340int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1341{
1342 if (len < ST_F_TOTAL_FIELDS)
1343 return 0;
1344
1345 memset(stats, 0, sizeof(*stats) * len);
1346
1347 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1348 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1349 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1350 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1351 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1352 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1353 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1354 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1355 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1356 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1357 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1358 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1359 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1360 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1361 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1362 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1363 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1364 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1365 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1366 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1367 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1368 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001369 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001370
1371 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1372 if (px->mode == PR_MODE_HTTP) {
1373 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1374 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1375 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1376 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1377 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1378 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1379 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001380 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1381 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001382 }
1383
1384 /* requests : req_rate, req_rate_max, req_tot, */
1385 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1386 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1387 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1388
1389 /* compression: in, out, bypassed, responses */
1390 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1391 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1392 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1393 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1394
1395 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1396 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1397 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1398 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1399
1400 return 1;
1401}
1402
1403/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1404 * the state from stream interface <si>. The caller is responsible for clearing
1405 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1406 */
1407static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1408{
1409 struct appctx *appctx = __objt_appctx(si->end);
1410
1411 if (!(px->cap & PR_CAP_FE))
1412 return 0;
1413
1414 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1415 return 0;
1416
1417 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1418 return 0;
1419
1420 return stats_dump_one_line(stats, 0, px, appctx);
1421}
1422
1423/* Fill <stats> with the listener statistics. <stats> is
1424 * preallocated array of length <len>. The length of the array
1425 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1426 * then this value, the function returns 0, otherwise, it
1427 * returns 1. <flags> can take the value ST_SHLGNDS.
1428 */
1429int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1430 struct field *stats, int len)
1431{
Willy Tarreau83061a82018-07-13 11:56:34 +02001432 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001433
1434 if (len < ST_F_TOTAL_FIELDS)
1435 return 0;
1436
1437 if (!l->counters)
1438 return 0;
1439
1440 chunk_reset(out);
1441 memset(stats, 0, sizeof(*stats) * len);
1442
1443 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1444 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1445 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1446 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1447 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1448 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1449 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1450 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1451 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1452 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1453 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1454 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1455 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1456 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1457 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
1458 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1459 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1460 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1461 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001462 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001463
1464 if (flags & ST_SHLGNDS) {
1465 char str[INET6_ADDRSTRLEN];
1466 int port;
1467
1468 port = get_host_port(&l->addr);
1469 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1470 case AF_INET:
1471 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1472 chunk_appendf(out, "%s:%d", str, port);
1473 break;
1474 case AF_INET6:
1475 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1476 chunk_appendf(out, "[%s]:%d", str, port);
1477 break;
1478 case AF_UNIX:
1479 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1480 break;
1481 case -1:
1482 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1483 chunk_strcat(out, strerror(errno));
1484 break;
1485 default: /* address family not supported */
1486 break;
1487 }
1488 }
1489
1490 return 1;
1491}
1492
1493/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1494 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1495 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1496 * otherwise.
1497 */
1498static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1499{
1500 struct appctx *appctx = __objt_appctx(si->end);
1501
1502 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1503 return 0;
1504
1505 return stats_dump_one_line(stats, flags, px, appctx);
1506}
1507
1508enum srv_stats_state {
1509 SRV_STATS_STATE_DOWN = 0,
1510 SRV_STATS_STATE_DOWN_AGENT,
1511 SRV_STATS_STATE_GOING_UP,
1512 SRV_STATS_STATE_UP_GOING_DOWN,
1513 SRV_STATS_STATE_UP,
1514 SRV_STATS_STATE_NOLB_GOING_DOWN,
1515 SRV_STATS_STATE_NOLB,
1516 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1517 SRV_STATS_STATE_DRAIN,
1518 SRV_STATS_STATE_DRAIN_AGENT,
1519 SRV_STATS_STATE_NO_CHECK,
1520
1521 SRV_STATS_STATE_COUNT, /* Must be last */
1522};
1523
1524static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1525 [SRV_STATS_STATE_DOWN] = "DOWN",
1526 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1527 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1528 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1529 [SRV_STATS_STATE_UP] = "UP",
1530 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1531 [SRV_STATS_STATE_NOLB] = "NOLB",
1532 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1533 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1534 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1535 [SRV_STATS_STATE_NO_CHECK] = "no check"
1536};
1537
1538/* Fill <stats> with the server statistics. <stats> is
1539 * preallocated array of length <len>. The length of the array
1540 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1541 * then this value, the function returns 0, otherwise, it
1542 * returns 1. <flags> can take the value ST_SHLGNDS.
1543 */
1544int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1545 struct field *stats, int len)
1546{
1547 struct server *via, *ref;
1548 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001549 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001550 enum srv_stats_state state;
1551 char *fld_status;
1552
1553 if (len < ST_F_TOTAL_FIELDS)
1554 return 0;
1555
1556 memset(stats, 0, sizeof(*stats) * len);
1557
1558 /* we have "via" which is the tracked server as described in the configuration,
1559 * and "ref" which is the checked server and the end of the chain.
1560 */
1561 via = sv->track ? sv->track : sv;
1562 ref = via;
1563 while (ref->track)
1564 ref = ref->track;
1565
Emeric Brun52a91d32017-08-31 14:41:55 +02001566 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001567 if ((ref->check.state & CHK_ST_ENABLED) &&
1568 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1569 state = SRV_STATS_STATE_UP_GOING_DOWN;
1570 } else {
1571 state = SRV_STATS_STATE_UP;
1572 }
1573
Emeric Brun52a91d32017-08-31 14:41:55 +02001574 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001575 if (ref->agent.state & CHK_ST_ENABLED)
1576 state = SRV_STATS_STATE_DRAIN_AGENT;
1577 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1578 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1579 else
1580 state = SRV_STATS_STATE_DRAIN;
1581 }
1582
1583 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1584 state = SRV_STATS_STATE_NO_CHECK;
1585 }
1586 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001587 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001588 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1589 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1590 state = SRV_STATS_STATE_NOLB;
1591 } else {
1592 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1593 }
1594 }
1595 else { /* stopped */
1596 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1597 state = SRV_STATS_STATE_DOWN_AGENT;
1598 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1599 state = SRV_STATS_STATE_DOWN; /* DOWN */
1600 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1601 state = SRV_STATS_STATE_GOING_UP;
1602 } else {
1603 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1604 }
1605 }
1606
1607 chunk_reset(out);
1608
1609 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1610 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1611 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1612 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1613 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1614 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1615 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1616
1617 if (sv->maxconn)
1618 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1619
1620 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1621 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1622 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1623 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1624 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1625 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1626 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1627 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001628 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001629 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1630 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001631
1632 /* status */
1633 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001634 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001635 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001636 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001637 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001638 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001639 chunk_appendf(out, "MAINT");
1640 else
1641 chunk_appendf(out,
1642 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001643 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1644 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001645
1646 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1647 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001648 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 +01001649 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1650 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1651
1652 /* check failures: unique, fatal; last change, total downtime */
1653 if (sv->check.state & CHK_ST_ENABLED) {
1654 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1655 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1656 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1657 }
1658
1659 if (sv->maxqueue)
1660 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1661
1662 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1663 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1664 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1665
Emeric Brun52a91d32017-08-31 14:41:55 +02001666 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001667 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1668
1669 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1670
1671 if (sv->track) {
1672 char *fld_track = chunk_newstr(out);
1673
1674 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1675 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1676 }
1677
1678 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1679 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1680 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1681
1682 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1683 const char *fld_chksts;
1684
1685 fld_chksts = chunk_newstr(out);
1686 chunk_strcat(out, "* "); // for check in progress
1687 chunk_strcat(out, get_check_status_info(sv->check.status));
1688 if (!(sv->check.state & CHK_ST_INPROGRESS))
1689 fld_chksts += 2; // skip "* "
1690 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1691
1692 if (sv->check.status >= HCHK_STATUS_L57DATA)
1693 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1694
1695 if (sv->check.status >= HCHK_STATUS_CHECKED)
1696 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1697
1698 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1699 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1700 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1701 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1702 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1703 }
1704
1705 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1706 const char *fld_chksts;
1707
1708 fld_chksts = chunk_newstr(out);
1709 chunk_strcat(out, "* "); // for check in progress
1710 chunk_strcat(out, get_check_status_info(sv->agent.status));
1711 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1712 fld_chksts += 2; // skip "* "
1713 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1714
1715 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1716 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1717
1718 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1719 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1720
1721 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1722 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1723 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1724 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1725 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1726 }
1727
1728 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1729 if (px->mode == PR_MODE_HTTP) {
1730 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1731 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1732 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1733 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1734 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1735 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1736 }
1737
1738 if (ref->observe)
1739 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1740
1741 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1742 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1743 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1744
1745 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1746 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1747 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1748 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1749
1750 if (flags & ST_SHLGNDS) {
1751 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1752 case AF_INET:
1753 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001754 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001755 break;
1756 case AF_INET6:
1757 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001758 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001759 break;
1760 case AF_UNIX:
1761 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1762 break;
1763 case -1:
1764 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1765 chunk_strcat(out, strerror(errno));
1766 break;
1767 default: /* address family not supported */
1768 break;
1769 }
1770
1771 if (sv->cookie)
1772 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1773 }
1774
1775 return 1;
1776}
1777
1778/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1779 * from stream interface <si>, stats flags <flags>, and server state <state>.
1780 * The caller is responsible for clearing the trash if needed. Returns non-zero
1781 * if it emits anything, zero otherwise.
1782 */
1783static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1784{
1785 struct appctx *appctx = __objt_appctx(si->end);
1786
1787 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1788 return 0;
1789
1790 return stats_dump_one_line(stats, flags, px, appctx);
1791}
1792
1793/* Fill <stats> with the backend statistics. <stats> is
1794 * preallocated array of length <len>. The length of the array
1795 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1796 * then this value, the function returns 0, otherwise, it
1797 * returns 1. <flags> can take the value ST_SHLGNDS.
1798 */
1799int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1800{
1801 if (len < ST_F_TOTAL_FIELDS)
1802 return 0;
1803
1804 memset(stats, 0, sizeof(*stats) * len);
1805
1806 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1807 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1808 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1809 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1810 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001811 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001812 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1813 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1814 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1815 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1816 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1817 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1818 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1819 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1820 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1821 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1822 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001823 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001824 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1825 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001826 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1827 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1828 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1829 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1830 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1831 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1832 if (px->srv)
1833 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1834
1835 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1836 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1837 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1838 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1839 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1840 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1841 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1842
1843 if (flags & ST_SHLGNDS) {
1844 if (px->cookie_name)
1845 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1846 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1847 }
1848
1849 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1850 if (px->mode == PR_MODE_HTTP) {
1851 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1852 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1853 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1854 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1855 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1856 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1857 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001858 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1859 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001860 }
1861
1862 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1863 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1864
1865 /* compression: in, out, bypassed, responses */
1866 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1867 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1868 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1869 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1870 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1871
1872 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1873 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1874 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1875 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1876
1877 return 1;
1878}
1879
1880/* Dumps a line for backend <px> to the trash for and uses the state from stream
1881 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1882 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1883 */
1884static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1885{
1886 struct appctx *appctx = __objt_appctx(si->end);
1887
1888 if (!(px->cap & PR_CAP_BE))
1889 return 0;
1890
1891 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1892 return 0;
1893
1894 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1895 return 0;
1896
1897 return stats_dump_one_line(stats, flags, px, appctx);
1898}
1899
1900/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1901 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1902 * for clearing the trash if needed.
1903 */
1904static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1905{
1906 struct appctx *appctx = __objt_appctx(si->end);
1907 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1908
1909 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1910 /* A form to enable/disable this proxy servers */
1911
1912 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1913 scope_txt[0] = 0;
1914 if (appctx->ctx.stats.scope_len) {
1915 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02001916 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 +01001917 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1918 }
1919
1920 chunk_appendf(&trash,
1921 "<form method=\"post\">");
1922 }
1923
1924 /* print a new table */
1925 chunk_appendf(&trash,
1926 "<table class=\"tbl\" width=\"100%%\">\n"
1927 "<tr class=\"titre\">"
1928 "<th class=\"pxname\" width=\"10%%\">");
1929
1930 chunk_appendf(&trash,
1931 "<a name=\"%s\"></a>%s"
1932 "<a class=px href=\"#%s\">%s</a>",
1933 px->id,
1934 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1935 px->id, px->id);
1936
1937 if (uri->flags & ST_SHLGNDS) {
1938 /* cap, mode, id */
1939 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1940 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1941 px->uuid);
1942 chunk_appendf(&trash, "</div>");
1943 }
1944
1945 chunk_appendf(&trash,
1946 "%s</th>"
1947 "<th class=\"%s\" width=\"90%%\">%s</th>"
1948 "</tr>\n"
1949 "</table>\n"
1950 "<table class=\"tbl\" width=\"100%%\">\n"
1951 "<tr class=\"titre\">",
1952 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1953 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1954
1955 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1956 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00001957 chunk_appendf(&trash,
1958 "<th rowspan=2 width=1><input type=\"checkbox\" \
1959 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
1960 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
1961 px->id,
1962 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01001963 }
1964
1965 chunk_appendf(&trash,
1966 "<th rowspan=2></th>"
1967 "<th colspan=3>Queue</th>"
1968 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
1969 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
1970 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
1971 "<th colspan=9>Server</th>"
1972 "</tr>\n"
1973 "<tr class=\"titre\">"
1974 "<th>Cur</th><th>Max</th><th>Limit</th>"
1975 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
1976 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
1977 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
1978 "<th>Resp</th><th>Retr</th><th>Redis</th>"
1979 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
1980 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1981 "<th>Thrtle</th>\n"
1982 "</tr>");
1983}
1984
1985/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
1986 * stream interface <si>. The caller is responsible for clearing the trash if needed.
1987 */
1988static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
1989{
1990 struct appctx *appctx = __objt_appctx(si->end);
1991 chunk_appendf(&trash, "</table>");
1992
1993 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1994 /* close the form used to enable/disable this proxy servers */
1995 chunk_appendf(&trash,
1996 "Choose the action to perform on the checked servers : "
1997 "<select name=action>"
1998 "<option value=\"\"></option>"
1999 "<option value=\"ready\">Set state to READY</option>"
2000 "<option value=\"drain\">Set state to DRAIN</option>"
2001 "<option value=\"maint\">Set state to MAINT</option>"
2002 "<option value=\"dhlth\">Health: disable checks</option>"
2003 "<option value=\"ehlth\">Health: enable checks</option>"
2004 "<option value=\"hrunn\">Health: force UP</option>"
2005 "<option value=\"hnolb\">Health: force NOLB</option>"
2006 "<option value=\"hdown\">Health: force DOWN</option>"
2007 "<option value=\"dagent\">Agent: disable checks</option>"
2008 "<option value=\"eagent\">Agent: enable checks</option>"
2009 "<option value=\"arunn\">Agent: force UP</option>"
2010 "<option value=\"adown\">Agent: force DOWN</option>"
2011 "<option value=\"shutdown\">Kill Sessions</option>"
2012 "</select>"
2013 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2014 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2015 "</form>",
2016 px->uuid);
2017 }
2018
2019 chunk_appendf(&trash, "<p>\n");
2020}
2021
2022/*
2023 * Dumps statistics for a proxy. The output is sent to the stream interface's
2024 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2025 * buffer space, or non-zero if everything completed. This function is used
2026 * both by the CLI and the HTTP entry points, and is able to dump the output
2027 * in HTML or CSV formats. If the later, <uri> must be NULL.
2028 */
Christopher Fauletef779222018-10-31 08:47:01 +01002029int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2030 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002031{
2032 struct appctx *appctx = __objt_appctx(si->end);
2033 struct stream *s = si_strm(si);
2034 struct channel *rep = si_ic(si);
2035 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2036 struct listener *l;
2037 unsigned int flags;
2038
2039 if (uri)
2040 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002041 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002042 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2043 else
2044 flags = ST_SHNODE | ST_SHDESC;
2045
2046 chunk_reset(&trash);
2047
2048 switch (appctx->ctx.stats.px_st) {
2049 case STAT_PX_ST_INIT:
2050 /* we are on a new proxy */
2051 if (uri && uri->scope) {
2052 /* we have a limited scope, we have to check the proxy name */
2053 struct stat_scope *scope;
2054 int len;
2055
2056 len = strlen(px->id);
2057 scope = uri->scope;
2058
2059 while (scope) {
2060 /* match exact proxy name */
2061 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2062 break;
2063
2064 /* match '.' which means 'self' proxy */
2065 if (!strcmp(scope->px_id, ".") && px == s->be)
2066 break;
2067 scope = scope->next;
2068 }
2069
2070 /* proxy name not found : don't dump anything */
2071 if (scope == NULL)
2072 return 1;
2073 }
2074
2075 /* if the user has requested a limited output and the proxy
2076 * name does not match, skip it.
2077 */
2078 if (appctx->ctx.stats.scope_len &&
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002079 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 +01002080 return 1;
2081
2082 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2083 (appctx->ctx.stats.iid != -1) &&
2084 (px->uuid != appctx->ctx.stats.iid))
2085 return 1;
2086
2087 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2088 /* fall through */
2089
2090 case STAT_PX_ST_TH:
2091 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2092 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002093 if (!stats_putchk(rep, htx, &trash))
2094 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002095 }
2096
2097 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2098 /* fall through */
2099
2100 case STAT_PX_ST_FE:
2101 /* print the frontend */
2102 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002103 if (!stats_putchk(rep, htx, &trash))
2104 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002105 }
2106
2107 appctx->ctx.stats.l = px->conf.listeners.n;
2108 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2109 /* fall through */
2110
2111 case STAT_PX_ST_LI:
2112 /* stats.l has been initialized above */
2113 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002114 if (htx) {
2115 if (htx_almost_full(htx))
2116 goto full;
2117 }
2118 else {
2119 if (buffer_almost_full(&rep->buf))
2120 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002121 }
2122
2123 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2124 if (!l->counters)
2125 continue;
2126
2127 if (appctx->ctx.stats.flags & STAT_BOUND) {
2128 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2129 break;
2130
2131 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2132 continue;
2133 }
2134
2135 /* print the frontend */
2136 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002137 if (!stats_putchk(rep, htx, &trash))
2138 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002139 }
2140 }
2141
2142 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2143 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2144 /* fall through */
2145
2146 case STAT_PX_ST_SV:
2147 /* stats.sv has been initialized above */
2148 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002149 if (htx) {
2150 if (htx_almost_full(htx))
2151 goto full;
2152 }
2153 else {
2154 if (buffer_almost_full(&rep->buf))
2155 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002156 }
2157
2158 sv = appctx->ctx.stats.sv;
2159
2160 if (appctx->ctx.stats.flags & STAT_BOUND) {
2161 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2162 break;
2163
2164 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2165 continue;
2166 }
2167
2168 svs = sv;
2169 while (svs->track)
2170 svs = svs->track;
2171
2172 /* do not report servers which are DOWN and not changing state */
2173 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002174 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2175 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002176 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2177 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2178 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2179 continue;
2180 }
2181
2182 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002183 if (!stats_putchk(rep, htx, &trash))
2184 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002185 }
2186 } /* for sv */
2187
2188 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2189 /* fall through */
2190
2191 case STAT_PX_ST_BE:
2192 /* print the backend */
2193 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002194 if (!stats_putchk(rep, htx, &trash))
2195 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002196 }
2197
2198 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2199 /* fall through */
2200
2201 case STAT_PX_ST_END:
2202 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2203 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002204 if (!stats_putchk(rep, htx, &trash))
2205 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002206 }
2207
2208 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2209 /* fall through */
2210
2211 case STAT_PX_ST_FIN:
2212 return 1;
2213
2214 default:
2215 /* unknown state, we should put an abort() here ! */
2216 return 1;
2217 }
Christopher Fauletef779222018-10-31 08:47:01 +01002218
2219 full:
2220 si_rx_room_blk(si);
2221 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002222}
2223
2224/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2225 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2226 */
2227static void stats_dump_html_head(struct uri_auth *uri)
2228{
2229 /* WARNING! This must fit in the first buffer !!! */
2230 chunk_appendf(&trash,
2231 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2232 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2233 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2234 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2235 "<style type=\"text/css\"><!--\n"
2236 "body {"
2237 " font-family: arial, helvetica, sans-serif;"
2238 " font-size: 12px;"
2239 " font-weight: normal;"
2240 " color: black;"
2241 " background: white;"
2242 "}\n"
2243 "th,td {"
2244 " font-size: 10px;"
2245 "}\n"
2246 "h1 {"
2247 " font-size: x-large;"
2248 " margin-bottom: 0.5em;"
2249 "}\n"
2250 "h2 {"
2251 " font-family: helvetica, arial;"
2252 " font-size: x-large;"
2253 " font-weight: bold;"
2254 " font-style: italic;"
2255 " color: #6020a0;"
2256 " margin-top: 0em;"
2257 " margin-bottom: 0em;"
2258 "}\n"
2259 "h3 {"
2260 " font-family: helvetica, arial;"
2261 " font-size: 16px;"
2262 " font-weight: bold;"
2263 " color: #b00040;"
2264 " background: #e8e8d0;"
2265 " margin-top: 0em;"
2266 " margin-bottom: 0em;"
2267 "}\n"
2268 "li {"
2269 " margin-top: 0.25em;"
2270 " margin-right: 2em;"
2271 "}\n"
2272 ".hr {margin-top: 0.25em;"
2273 " border-color: black;"
2274 " border-bottom-style: solid;"
2275 "}\n"
2276 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2277 ".total {background: #20D0D0;color: #ffff80;}\n"
2278 ".frontend {background: #e8e8d0;}\n"
2279 ".socket {background: #d0d0d0;}\n"
2280 ".backend {background: #e8e8d0;}\n"
2281 ".active_down {background: #ff9090;}\n"
2282 ".active_going_up {background: #ffd020;}\n"
2283 ".active_going_down {background: #ffffa0;}\n"
2284 ".active_up {background: #c0ffc0;}\n"
2285 ".active_nolb {background: #20a0ff;}\n"
2286 ".active_draining {background: #20a0FF;}\n"
2287 ".active_no_check {background: #e0e0e0;}\n"
2288 ".backup_down {background: #ff9090;}\n"
2289 ".backup_going_up {background: #ff80ff;}\n"
2290 ".backup_going_down {background: #c060ff;}\n"
2291 ".backup_up {background: #b0d0ff;}\n"
2292 ".backup_nolb {background: #90b0e0;}\n"
2293 ".backup_draining {background: #cc9900;}\n"
2294 ".backup_no_check {background: #e0e0e0;}\n"
2295 ".maintain {background: #c07820;}\n"
2296 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2297 "\n"
2298 "a.px:link {color: #ffff40; text-decoration: none;}"
2299 "a.px:visited {color: #ffff40; text-decoration: none;}"
2300 "a.px:hover {color: #ffffff; text-decoration: none;}"
2301 "a.lfsb:link {color: #000000; text-decoration: none;}"
2302 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2303 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2304 "\n"
2305 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2306 "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"
2307 "table.tbl td.ac { text-align: center;}\n"
2308 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2309 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2310 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2311 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2312 "\n"
2313 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2314 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2315 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2316 "table.det { border-collapse: collapse; border-style: none; }\n"
2317 "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"
2318 "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"
2319 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2320 "div.tips {\n"
2321 " display:block;\n"
2322 " visibility:hidden;\n"
2323 " z-index:2147483647;\n"
2324 " position:absolute;\n"
2325 " padding:2px 4px 3px;\n"
2326 " background:#f0f060; color:#000000;\n"
2327 " border:1px solid #7040c0;\n"
2328 " white-space:nowrap;\n"
2329 " font-style:normal;font-size:11px;font-weight:normal;\n"
2330 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2331 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2332 "}\n"
2333 "u:hover div.tips {visibility:visible;}\n"
2334 "-->\n"
2335 "</style></head>\n",
2336 (uri->flags & ST_SHNODE) ? " on " : "",
2337 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2338 );
2339}
2340
2341/* Dumps the HTML stats information block to the trash for and uses the state from
2342 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2343 * for clearing the trash if needed.
2344 */
2345static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2346{
2347 struct appctx *appctx = __objt_appctx(si->end);
2348 unsigned int up = (now.tv_sec - start_date.tv_sec);
2349 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2350
2351 /* WARNING! this has to fit the first packet too.
2352 * We are around 3.5 kB, add adding entries will
2353 * become tricky if we want to support 4kB buffers !
2354 */
2355 chunk_appendf(&trash,
2356 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2357 PRODUCT_NAME "%s</a></h1>\n"
2358 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2359 "<hr width=\"100%%\" class=\"hr\">\n"
2360 "<h3>&gt; General process information</h3>\n"
2361 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002362 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002363 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2364 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2365 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
2366 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
2367 "Running tasks: %d/%d; idle = %d %%<br>\n"
2368 "</td><td align=\"center\" nowrap>\n"
2369 "<table class=\"lgd\"><tr>\n"
2370 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2371 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2372 "</tr><tr>\n"
2373 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2374 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2375 "</tr><tr>\n"
2376 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2377 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2378 "</tr><tr>\n"
2379 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2380 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2381 "</tr><tr>\n"
2382 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2383 "</tr><tr>\n"
2384 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2385 "</tr></table>\n"
2386 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2387 "</td>"
2388 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2389 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2390 "",
2391 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
2392 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2393 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2394 (uri->flags & ST_SHDESC) ? ": " : "",
2395 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002396 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002397 up / 86400, (up % 86400) / 3600,
2398 (up % 3600) / 60, (up % 60),
2399 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2400 global.rlimit_memmax ? " MB" : "",
2401 global.rlimit_nofile,
2402 global.maxsock, global.maxconn, global.maxpipes,
2403 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002404 tasks_run_queue_cur, nb_tasks_cur, idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002405 );
2406
2407 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002408 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 +01002409 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2410
2411 chunk_appendf(&trash,
2412 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2413 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2414 STAT_SCOPE_TXT_MAXLEN);
2415
2416 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2417 scope_txt[0] = 0;
2418 if (appctx->ctx.stats.scope_len) {
2419 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002420 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 +01002421 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2422 }
2423
2424 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2425 chunk_appendf(&trash,
2426 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2427 uri->uri_prefix,
2428 "",
2429 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2430 scope_txt);
2431 else
2432 chunk_appendf(&trash,
2433 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2434 uri->uri_prefix,
2435 ";up",
2436 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2437 scope_txt);
2438
2439 if (uri->refresh > 0) {
2440 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2441 chunk_appendf(&trash,
2442 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2443 uri->uri_prefix,
2444 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2445 "",
2446 scope_txt);
2447 else
2448 chunk_appendf(&trash,
2449 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2450 uri->uri_prefix,
2451 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2452 ";norefresh",
2453 scope_txt);
2454 }
2455
2456 chunk_appendf(&trash,
2457 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2458 uri->uri_prefix,
2459 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2460 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2461 scope_txt);
2462
2463 chunk_appendf(&trash,
2464 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2465 uri->uri_prefix,
2466 (uri->refresh > 0) ? ";norefresh" : "",
2467 scope_txt);
2468
2469 chunk_appendf(&trash,
2470 "</ul></td>"
2471 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2472 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2473 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2474 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2475 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2476 "</ul>"
2477 "</td>"
2478 "</tr></table>\n"
2479 ""
2480 );
2481
2482 if (appctx->ctx.stats.st_code) {
2483 switch (appctx->ctx.stats.st_code) {
2484 case STAT_STATUS_DONE:
2485 chunk_appendf(&trash,
2486 "<p><div class=active_up>"
2487 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2488 "Action processed successfully."
2489 "</div>\n", uri->uri_prefix,
2490 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2491 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2492 scope_txt);
2493 break;
2494 case STAT_STATUS_NONE:
2495 chunk_appendf(&trash,
2496 "<p><div class=active_going_down>"
2497 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2498 "Nothing has changed."
2499 "</div>\n", uri->uri_prefix,
2500 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2501 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2502 scope_txt);
2503 break;
2504 case STAT_STATUS_PART:
2505 chunk_appendf(&trash,
2506 "<p><div class=active_going_down>"
2507 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2508 "Action partially processed.<br>"
2509 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2510 "</div>\n", uri->uri_prefix,
2511 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2512 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2513 scope_txt);
2514 break;
2515 case STAT_STATUS_ERRP:
2516 chunk_appendf(&trash,
2517 "<p><div class=active_down>"
2518 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2519 "Action not processed because of invalid parameters."
2520 "<ul>"
2521 "<li>The action is maybe unknown.</li>"
2522 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2523 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2524 "</ul>"
2525 "</div>\n", uri->uri_prefix,
2526 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2527 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2528 scope_txt);
2529 break;
2530 case STAT_STATUS_EXCD:
2531 chunk_appendf(&trash,
2532 "<p><div class=active_down>"
2533 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2534 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2535 "You should retry with less servers at a time.</b>"
2536 "</div>\n", uri->uri_prefix,
2537 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2538 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2539 scope_txt);
2540 break;
2541 case STAT_STATUS_DENY:
2542 chunk_appendf(&trash,
2543 "<p><div class=active_down>"
2544 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2545 "<b>Action denied.</b>"
2546 "</div>\n", uri->uri_prefix,
2547 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2548 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2549 scope_txt);
2550 break;
2551 default:
2552 chunk_appendf(&trash,
2553 "<p><div class=active_no_check>"
2554 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2555 "Unexpected result."
2556 "</div>\n", uri->uri_prefix,
2557 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2558 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2559 scope_txt);
2560 }
2561 chunk_appendf(&trash, "<p>\n");
2562 }
2563}
2564
2565/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2566 * for clearing the trash if needed.
2567 */
2568static void stats_dump_html_end()
2569{
2570 chunk_appendf(&trash, "</body></html>\n");
2571}
2572
Simon Horman05ee2132017-01-04 09:37:25 +01002573/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2574 * for clearing it if needed.
2575 */
2576static void stats_dump_json_header()
2577{
2578 chunk_strcat(&trash, "[");
2579}
2580
2581
2582/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2583 * for clearing the trash if needed.
2584 */
2585static void stats_dump_json_end()
2586{
2587 chunk_strcat(&trash, "]");
2588}
2589
William Lallemand74c24fb2016-11-21 17:18:36 +01002590/* This function dumps statistics onto the stream interface's read buffer in
2591 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2592 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2593 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2594 * and the stream must be closed, or -1 in case of any error. This function is
2595 * used by both the CLI and the HTTP handlers.
2596 */
Christopher Fauletef779222018-10-31 08:47:01 +01002597static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2598 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002599{
2600 struct appctx *appctx = __objt_appctx(si->end);
2601 struct channel *rep = si_ic(si);
2602 struct proxy *px;
2603
2604 chunk_reset(&trash);
2605
2606 switch (appctx->st2) {
2607 case STAT_ST_INIT:
2608 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2609 /* fall through */
2610
2611 case STAT_ST_HEAD:
2612 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2613 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002614 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002615 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002616 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2617 stats_dump_csv_header();
2618
Christopher Fauletef779222018-10-31 08:47:01 +01002619 if (!stats_putchk(rep, htx, &trash))
2620 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002621
2622 appctx->st2 = STAT_ST_INFO;
2623 /* fall through */
2624
2625 case STAT_ST_INFO:
2626 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2627 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002628 if (!stats_putchk(rep, htx, &trash))
2629 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002630 }
2631
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002632 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002633 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2634 appctx->st2 = STAT_ST_LIST;
2635 /* fall through */
2636
2637 case STAT_ST_LIST:
2638 /* dump proxies */
2639 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002640 if (htx) {
2641 if (htx_almost_full(htx))
2642 goto full;
2643 }
2644 else {
2645 if (buffer_almost_full(&rep->buf))
2646 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002647 }
2648
2649 px = appctx->ctx.stats.px;
2650 /* skip the disabled proxies, global frontend and non-networked ones */
2651 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002652 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002653 return 0;
2654
2655 appctx->ctx.stats.px = px->next;
2656 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2657 }
2658 /* here, we just have reached the last proxy */
2659
2660 appctx->st2 = STAT_ST_END;
2661 /* fall through */
2662
2663 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002664 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2665 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2666 stats_dump_html_end();
2667 else
2668 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002669 if (!stats_putchk(rep, htx, &trash))
2670 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002671 }
2672
2673 appctx->st2 = STAT_ST_FIN;
2674 /* fall through */
2675
2676 case STAT_ST_FIN:
2677 return 1;
2678
2679 default:
2680 /* unknown state ! */
2681 appctx->st2 = STAT_ST_FIN;
2682 return -1;
2683 }
Christopher Fauletef779222018-10-31 08:47:01 +01002684
2685 full:
2686 si_rx_room_blk(si);
2687 return 0;
2688
William Lallemand74c24fb2016-11-21 17:18:36 +01002689}
2690
2691/* We reached the stats page through a POST request. The appctx is
2692 * expected to have already been allocated by the caller.
2693 * Parse the posted data and enable/disable servers if necessary.
2694 * Returns 1 if request was parsed or zero if it needs more data.
2695 */
2696static int stats_process_http_post(struct stream_interface *si)
2697{
2698 struct stream *s = si_strm(si);
2699 struct appctx *appctx = objt_appctx(si->end);
2700
2701 struct proxy *px = NULL;
2702 struct server *sv = NULL;
2703
2704 char key[LINESIZE];
2705 int action = ST_ADM_ACTION_NONE;
2706 int reprocess = 0;
2707
2708 int total_servers = 0;
2709 int altered_servers = 0;
2710
2711 char *first_param, *cur_param, *next_param, *end_params;
2712 char *st_cur_param = NULL;
2713 char *st_next_param = NULL;
2714
Willy Tarreau83061a82018-07-13 11:56:34 +02002715 struct buffer *temp;
William Lallemand74c24fb2016-11-21 17:18:36 +01002716 int reql;
2717
2718 temp = get_trash_chunk();
2719 if (temp->size < s->txn->req.body_len) {
2720 /* too large request */
2721 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2722 goto out;
2723 }
2724
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002725 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2726 s->txn->req.eoh + 2);
William Lallemand74c24fb2016-11-21 17:18:36 +01002727 if (reql <= 0) {
2728 /* we need more data */
2729 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2730 return 0;
2731 }
2732
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002733 first_param = temp->area;
2734 end_params = temp->area + reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002735 cur_param = next_param = end_params;
2736 *end_params = '\0';
2737
2738 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2739
2740 /*
2741 * Parse the parameters in reverse order to only store the last value.
2742 * From the html form, the backend and the action are at the end.
2743 */
2744 while (cur_param > first_param) {
2745 char *value;
2746 int poffset, plen;
2747
2748 cur_param--;
2749
2750 if ((*cur_param == '&') || (cur_param == first_param)) {
2751 reprocess_servers:
2752 /* Parse the key */
2753 poffset = (cur_param != first_param ? 1 : 0);
2754 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2755 if ((plen > 0) && (plen <= sizeof(key))) {
2756 strncpy(key, cur_param + poffset, plen);
2757 key[plen - 1] = '\0';
2758 } else {
2759 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2760 goto out;
2761 }
2762
2763 /* Parse the value */
2764 value = key;
2765 while (*value != '\0' && *value != '=') {
2766 value++;
2767 }
2768 if (*value == '=') {
2769 /* Ok, a value is found, we can mark the end of the key */
2770 *value++ = '\0';
2771 }
2772 if (url_decode(key) < 0 || url_decode(value) < 0)
2773 break;
2774
2775 /* Now we can check the key to see what to do */
2776 if (!px && (strcmp(key, "b") == 0)) {
2777 if ((px = proxy_be_by_name(value)) == NULL) {
2778 /* the backend name is unknown or ambiguous (duplicate names) */
2779 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2780 goto out;
2781 }
2782 }
2783 else if (!action && (strcmp(key, "action") == 0)) {
2784 if (strcmp(value, "ready") == 0) {
2785 action = ST_ADM_ACTION_READY;
2786 }
2787 else if (strcmp(value, "drain") == 0) {
2788 action = ST_ADM_ACTION_DRAIN;
2789 }
2790 else if (strcmp(value, "maint") == 0) {
2791 action = ST_ADM_ACTION_MAINT;
2792 }
2793 else if (strcmp(value, "shutdown") == 0) {
2794 action = ST_ADM_ACTION_SHUTDOWN;
2795 }
2796 else if (strcmp(value, "dhlth") == 0) {
2797 action = ST_ADM_ACTION_DHLTH;
2798 }
2799 else if (strcmp(value, "ehlth") == 0) {
2800 action = ST_ADM_ACTION_EHLTH;
2801 }
2802 else if (strcmp(value, "hrunn") == 0) {
2803 action = ST_ADM_ACTION_HRUNN;
2804 }
2805 else if (strcmp(value, "hnolb") == 0) {
2806 action = ST_ADM_ACTION_HNOLB;
2807 }
2808 else if (strcmp(value, "hdown") == 0) {
2809 action = ST_ADM_ACTION_HDOWN;
2810 }
2811 else if (strcmp(value, "dagent") == 0) {
2812 action = ST_ADM_ACTION_DAGENT;
2813 }
2814 else if (strcmp(value, "eagent") == 0) {
2815 action = ST_ADM_ACTION_EAGENT;
2816 }
2817 else if (strcmp(value, "arunn") == 0) {
2818 action = ST_ADM_ACTION_ARUNN;
2819 }
2820 else if (strcmp(value, "adown") == 0) {
2821 action = ST_ADM_ACTION_ADOWN;
2822 }
2823 /* else these are the old supported methods */
2824 else if (strcmp(value, "disable") == 0) {
2825 action = ST_ADM_ACTION_DISABLE;
2826 }
2827 else if (strcmp(value, "enable") == 0) {
2828 action = ST_ADM_ACTION_ENABLE;
2829 }
2830 else if (strcmp(value, "stop") == 0) {
2831 action = ST_ADM_ACTION_STOP;
2832 }
2833 else if (strcmp(value, "start") == 0) {
2834 action = ST_ADM_ACTION_START;
2835 }
2836 else {
2837 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2838 goto out;
2839 }
2840 }
2841 else if (strcmp(key, "s") == 0) {
2842 if (!(px && action)) {
2843 /*
2844 * Indicates that we'll need to reprocess the parameters
2845 * as soon as backend and action are known
2846 */
2847 if (!reprocess) {
2848 st_cur_param = cur_param;
2849 st_next_param = next_param;
2850 }
2851 reprocess = 1;
2852 }
2853 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002854 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002855 switch (action) {
2856 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002857 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002858 altered_servers++;
2859 total_servers++;
2860 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2861 }
2862 break;
2863 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002864 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002865 altered_servers++;
2866 total_servers++;
2867 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2868 }
2869 break;
2870 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002871 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002872 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2873 altered_servers++;
2874 total_servers++;
2875 }
2876 break;
2877 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002878 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002879 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2880 altered_servers++;
2881 total_servers++;
2882 }
2883 break;
2884 case ST_ADM_ACTION_DHLTH:
2885 if (sv->check.state & CHK_ST_CONFIGURED) {
2886 sv->check.state &= ~CHK_ST_ENABLED;
2887 altered_servers++;
2888 total_servers++;
2889 }
2890 break;
2891 case ST_ADM_ACTION_EHLTH:
2892 if (sv->check.state & CHK_ST_CONFIGURED) {
2893 sv->check.state |= CHK_ST_ENABLED;
2894 altered_servers++;
2895 total_servers++;
2896 }
2897 break;
2898 case ST_ADM_ACTION_HRUNN:
2899 if (!(sv->track)) {
2900 sv->check.health = sv->check.rise + sv->check.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_HNOLB:
2907 if (!(sv->track)) {
2908 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002909 srv_set_stopping(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_HDOWN:
2915 if (!(sv->track)) {
2916 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002917 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002918 altered_servers++;
2919 total_servers++;
2920 }
2921 break;
2922 case ST_ADM_ACTION_DAGENT:
2923 if (sv->agent.state & CHK_ST_CONFIGURED) {
2924 sv->agent.state &= ~CHK_ST_ENABLED;
2925 altered_servers++;
2926 total_servers++;
2927 }
2928 break;
2929 case ST_ADM_ACTION_EAGENT:
2930 if (sv->agent.state & CHK_ST_CONFIGURED) {
2931 sv->agent.state |= CHK_ST_ENABLED;
2932 altered_servers++;
2933 total_servers++;
2934 }
2935 break;
2936 case ST_ADM_ACTION_ARUNN:
2937 if (sv->agent.state & CHK_ST_ENABLED) {
2938 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002939 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002940 altered_servers++;
2941 total_servers++;
2942 }
2943 break;
2944 case ST_ADM_ACTION_ADOWN:
2945 if (sv->agent.state & CHK_ST_ENABLED) {
2946 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002947 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002948 altered_servers++;
2949 total_servers++;
2950 }
2951 break;
2952 case ST_ADM_ACTION_READY:
2953 srv_adm_set_ready(sv);
2954 altered_servers++;
2955 total_servers++;
2956 break;
2957 case ST_ADM_ACTION_DRAIN:
2958 srv_adm_set_drain(sv);
2959 altered_servers++;
2960 total_servers++;
2961 break;
2962 case ST_ADM_ACTION_MAINT:
2963 srv_adm_set_maint(sv);
2964 altered_servers++;
2965 total_servers++;
2966 break;
2967 case ST_ADM_ACTION_SHUTDOWN:
2968 if (px->state != PR_STSTOPPED) {
2969 struct stream *sess, *sess_bck;
2970
2971 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2972 if (sess->srv_conn == sv)
2973 stream_shutdown(sess, SF_ERR_KILLED);
2974
2975 altered_servers++;
2976 total_servers++;
2977 }
2978 break;
2979 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002980 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002981 } else {
2982 /* the server name is unknown or ambiguous (duplicate names) */
2983 total_servers++;
2984 }
2985 }
2986 if (reprocess && px && action) {
2987 /* Now, we know the backend and the action chosen by the user.
2988 * We can safely restart from the first server parameter
2989 * to reprocess them
2990 */
2991 cur_param = st_cur_param;
2992 next_param = st_next_param;
2993 reprocess = 0;
2994 goto reprocess_servers;
2995 }
2996
2997 next_param = cur_param;
2998 }
2999 }
3000
3001 if (total_servers == 0) {
3002 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3003 }
3004 else if (altered_servers == 0) {
3005 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3006 }
3007 else if (altered_servers == total_servers) {
3008 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3009 }
3010 else {
3011 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3012 }
3013 out:
3014 return 1;
Christopher Fauletef779222018-10-31 08:47:01 +01003015}
3016
3017
3018static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
3019{
3020 struct stream *s = si_strm(si);
3021 struct uri_auth *uri = s->be->uri_auth;
3022 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003023 struct htx_sl *sl;
3024 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003025
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003026 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_ENC|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3027 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3028 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003029 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003030 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003031
3032 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
3033 !htx_add_header(htx, ist("Connection"), ist("close")))
3034 goto full;
3035 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3036 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3037 goto full;
3038 }
3039 else {
3040 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3041 goto full;
3042 }
3043
3044 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3045 const char *refresh = U2A(uri->refresh);
3046 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3047 goto full;
3048 }
3049
3050 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3051 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3052 goto full;
3053 }
3054
3055 if (!htx_add_endof(htx, HTX_BLK_EOH))
3056 goto full;
3057
3058 return 1;
3059
3060 full:
3061 htx_reset(htx);
3062 si_rx_room_blk(si);
3063 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003064}
3065
Christopher Fauletef779222018-10-31 08:47:01 +01003066
3067static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx)
3068{
3069 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3070 struct stream *s = si_strm(si);
3071 struct uri_auth *uri = s->be->uri_auth;
3072 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003073 struct htx_sl *sl;
3074 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003075
3076 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3077 scope_txt[0] = 0;
3078 if (appctx->ctx.stats.scope_len) {
3079 strcpy(scope_txt, STAT_SCOPE_PATTERN);
3080 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), co_head(si_oc(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3081 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3082 }
3083
3084 /* We don't want to land on the posted stats page because a refresh will
3085 * repost the data. We don't want this to happen on accident so we redirect
3086 * the browse to the stats page with a GET.
3087 */
3088 chunk_printf(&trash, "%s;st=%s%s%s%s",
3089 uri->uri_prefix,
3090 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3091 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3092 stat_status_codes[appctx->ctx.stats.st_code]) ?
3093 stat_status_codes[appctx->ctx.stats.st_code] :
3094 stat_status_codes[STAT_STATUS_UNKN],
3095 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3096 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3097 scope_txt);
3098
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003099 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3100 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3101 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003102 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003103 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003104
3105 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
3106 !htx_add_header(htx, ist("Connection"), ist("close")) ||
3107 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3108 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3109 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3110 goto full;
3111
3112 if (!htx_add_endof(htx, HTX_BLK_EOH))
3113 goto full;
3114
3115 return 1;
3116
3117full:
3118 htx_reset(htx);
3119 si_rx_room_blk(si);
3120 return 0;
3121}
William Lallemand74c24fb2016-11-21 17:18:36 +01003122
3123static int stats_send_http_headers(struct stream_interface *si)
3124{
3125 struct stream *s = si_strm(si);
3126 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003127 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003128
3129 chunk_printf(&trash,
3130 "HTTP/1.1 200 OK\r\n"
3131 "Cache-Control: no-cache\r\n"
3132 "Connection: close\r\n"
3133 "Content-Type: %s\r\n",
3134 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
3135
3136 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
3137 chunk_appendf(&trash, "Refresh: %d\r\n",
3138 uri->refresh);
3139
3140 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
3141
3142 if (appctx->ctx.stats.flags & STAT_CHUNKED)
3143 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
3144 else
3145 chunk_appendf(&trash, "\r\n");
3146
Willy Tarreau06d80a92017-10-19 14:32:15 +02003147 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003148 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003149 return 0;
3150 }
3151
3152 return 1;
3153}
3154
3155static int stats_send_http_redirect(struct stream_interface *si)
3156{
3157 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3158 struct stream *s = si_strm(si);
3159 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003160 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003161
3162 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3163 scope_txt[0] = 0;
3164 if (appctx->ctx.stats.scope_len) {
3165 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02003166 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 +01003167 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3168 }
3169
3170 /* We don't want to land on the posted stats page because a refresh will
3171 * repost the data. We don't want this to happen on accident so we redirect
3172 * the browse to the stats page with a GET.
3173 */
3174 chunk_printf(&trash,
3175 "HTTP/1.1 303 See Other\r\n"
3176 "Cache-Control: no-cache\r\n"
3177 "Content-Type: text/plain\r\n"
3178 "Connection: close\r\n"
3179 "Location: %s;st=%s%s%s%s\r\n"
3180 "Content-length: 0\r\n"
3181 "\r\n",
3182 uri->uri_prefix,
3183 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3184 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3185 stat_status_codes[appctx->ctx.stats.st_code]) ?
3186 stat_status_codes[appctx->ctx.stats.st_code] :
3187 stat_status_codes[STAT_STATUS_UNKN],
3188 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3189 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3190 scope_txt);
3191
Willy Tarreau06d80a92017-10-19 14:32:15 +02003192 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003193 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003194 return 0;
3195 }
3196
3197 return 1;
3198}
3199
Simon Horman05ee2132017-01-04 09:37:25 +01003200
William Lallemand74c24fb2016-11-21 17:18:36 +01003201/* This I/O handler runs as an applet embedded in a stream interface. It is
3202 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3203 * appctx->st0 contains the operation in progress (dump, done). The handler
3204 * automatically unregisters itself once transfer is complete.
3205 */
Christopher Fauletef779222018-10-31 08:47:01 +01003206static void htx_stats_io_handler(struct appctx *appctx)
3207{
3208 struct stream_interface *si = appctx->owner;
3209 struct stream *s = si_strm(si);
3210 struct channel *req = si_oc(si);
3211 struct channel *res = si_ic(si);
3212 struct htx *req_htx, *res_htx;
3213
3214 res_htx = htx_from_buf(&res->buf);
3215
3216 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3217 goto out;
3218
3219 /* Check if the input buffer is avalaible. */
3220 if (!b_size(&res->buf)) {
3221 si_rx_room_blk(si);
3222 goto out;
3223 }
3224
3225 /* check that the output is not closed */
3226 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3227 appctx->st0 = STAT_HTTP_DONE;
3228
3229 /* all states are processed in sequence */
3230 if (appctx->st0 == STAT_HTTP_HEAD) {
3231 if (stats_send_htx_headers(si, res_htx)) {
3232 if (s->txn->meth == HTTP_METH_HEAD)
3233 appctx->st0 = STAT_HTTP_DONE;
3234 else
3235 appctx->st0 = STAT_HTTP_DUMP;
3236 }
3237 }
3238
3239 if (appctx->st0 == STAT_HTTP_DUMP) {
3240 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3241 appctx->st0 = STAT_HTTP_DONE;
3242 }
3243
3244 if (appctx->st0 == STAT_HTTP_POST) {
3245 if (stats_process_http_post(si))
3246 appctx->st0 = STAT_HTTP_LAST;
3247 else if (si_oc(si)->flags & CF_SHUTR)
3248 appctx->st0 = STAT_HTTP_DONE;
3249 }
3250
3251 if (appctx->st0 == STAT_HTTP_LAST) {
3252 if (stats_send_htx_redirect(si, res_htx))
3253 appctx->st0 = STAT_HTTP_DONE;
3254 }
3255
3256 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Fauletb1b08212018-11-21 13:51:07 +01003257 /* Don't add EOD and TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003258 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3259 si_rx_room_blk(si);
3260 goto out;
3261 }
3262
3263 /* eat the whole request */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003264 req_htx = htxbuf(&req->buf);
3265 htx_reset(req_htx);
3266 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9b95d312018-11-19 21:58:10 +01003267 co_set_data(req, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01003268 res->flags |= CF_READ_NULL;
3269 si_shutr(si);
3270 }
3271
3272 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3273 si_shutw(si);
3274
3275 if (appctx->st0 == STAT_HTTP_DONE) {
3276 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3277 si_shutr(si);
3278 res->flags |= CF_READ_NULL;
3279 }
3280 }
3281 out:
3282 /* we have left the request in the buffer for the case where we
3283 * process a POST, and this automatically re-enables activity on
3284 * read. It's better to indicate that we want to stop reading when
3285 * we're sending, so that we know there's at most one direction
3286 * deciding to wake the applet up. It saves it from looping when
3287 * emitting large blocks into small TCP windows.
3288 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003289 htx_to_buf(res_htx, &res->buf);
3290 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003291 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003292}
3293
3294
3295/* This I/O handler runs as an applet embedded in a stream interface. It is
3296 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3297 * appctx->st0 contains the operation in progress (dump, done). The handler
3298 * automatically unregisters itself once transfer is complete.
3299 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003300static void http_stats_io_handler(struct appctx *appctx)
3301{
3302 struct stream_interface *si = appctx->owner;
3303 struct stream *s = si_strm(si);
3304 struct channel *req = si_oc(si);
3305 struct channel *res = si_ic(si);
3306
Christopher Fauletef779222018-10-31 08:47:01 +01003307 if (IS_HTX_STRM(s))
3308 return htx_stats_io_handler(appctx);
3309
William Lallemand74c24fb2016-11-21 17:18:36 +01003310 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3311 goto out;
3312
Joseph Herlant29023ec2018-11-15 13:39:46 -08003313 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003314 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003315 /* already subscribed, we'll be called later once the buffer is
3316 * available.
3317 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003318 goto out;
3319 }
3320
William Lallemand74c24fb2016-11-21 17:18:36 +01003321 /* check that the output is not closed */
3322 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3323 appctx->st0 = STAT_HTTP_DONE;
3324
3325 /* all states are processed in sequence */
3326 if (appctx->st0 == STAT_HTTP_HEAD) {
3327 if (stats_send_http_headers(si)) {
3328 if (s->txn->meth == HTTP_METH_HEAD)
3329 appctx->st0 = STAT_HTTP_DONE;
3330 else
3331 appctx->st0 = STAT_HTTP_DUMP;
3332 }
3333 }
3334
3335 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003336 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003337 unsigned int data_len;
3338 unsigned int last_len;
3339 unsigned int last_fwd = 0;
3340
3341 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3342 /* One difficulty we're facing is that we must prevent
3343 * the input data from being automatically forwarded to
3344 * the output area. For this, we temporarily disable
3345 * forwarding on the channel.
3346 */
3347 last_fwd = si_ic(si)->to_forward;
3348 si_ic(si)->to_forward = 0;
3349 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003350 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003351 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003352 si_ic(si)->to_forward = last_fwd;
3353 goto out;
3354 }
3355 }
3356
Willy Tarreau97f538b2018-06-15 19:41:31 +02003357 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003358 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003359 appctx->st0 = STAT_HTTP_DONE;
3360
Willy Tarreau97f538b2018-06-15 19:41:31 +02003361 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003362
3363 /* Now we must either adjust or remove the chunk size. This is
3364 * not easy because the chunk size might wrap at the end of the
3365 * buffer, so we pretend we have nothing in the buffer, we write
3366 * the size, then restore the buffer's contents. Note that we can
3367 * only do that because no forwarding is scheduled on the stats
3368 * applet.
3369 */
3370 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3371 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003372 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003373
3374 if (last_len != data_len) {
3375 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003376 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003377 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003378
3379 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003380 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003381 }
3382 /* now re-enable forwarding */
3383 channel_forward(si_ic(si), last_fwd);
3384 }
3385 }
3386
3387 if (appctx->st0 == STAT_HTTP_POST) {
3388 if (stats_process_http_post(si))
3389 appctx->st0 = STAT_HTTP_LAST;
3390 else if (si_oc(si)->flags & CF_SHUTR)
3391 appctx->st0 = STAT_HTTP_DONE;
3392 }
3393
3394 if (appctx->st0 == STAT_HTTP_LAST) {
3395 if (stats_send_http_redirect(si))
3396 appctx->st0 = STAT_HTTP_DONE;
3397 }
3398
3399 if (appctx->st0 == STAT_HTTP_DONE) {
3400 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3401 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003402 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003403 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003404 goto out;
3405 }
3406 }
3407 /* eat the whole request */
Willy Tarreau97f538b2018-06-15 19:41:31 +02003408 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003409 res->flags |= CF_READ_NULL;
3410 si_shutr(si);
3411 }
3412
3413 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3414 si_shutw(si);
3415
3416 if (appctx->st0 == STAT_HTTP_DONE) {
3417 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3418 si_shutr(si);
3419 res->flags |= CF_READ_NULL;
3420 }
3421 }
3422 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003423 /* we have left the request in the buffer for the case where we
3424 * process a POST, and this automatically re-enables activity on
3425 * read. It's better to indicate that we want to stop reading when
3426 * we're sending, so that we know there's at most one direction
3427 * deciding to wake the applet up. It saves it from looping when
3428 * emitting large blocks into small TCP windows.
3429 */
3430 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003431 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003432}
3433
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003434/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003435static int stats_dump_info_fields(struct buffer *out,
3436 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003437{
3438 int field;
3439
3440 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3441 if (!field_format(info, field))
3442 continue;
3443
3444 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3445 return 0;
3446 if (!stats_emit_raw_data_field(out, &info[field]))
3447 return 0;
3448 if (!chunk_strcat(out, "\n"))
3449 return 0;
3450 }
3451 return 1;
3452}
3453
3454/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003455static int stats_dump_typed_info_fields(struct buffer *out,
3456 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003457{
3458 int field;
3459
3460 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3461 if (!field_format(info, field))
3462 continue;
3463
3464 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3465 return 0;
3466 if (!stats_emit_field_tags(out, &info[field], ':'))
3467 return 0;
3468 if (!stats_emit_typed_data_field(out, &info[field]))
3469 return 0;
3470 if (!chunk_strcat(out, "\n"))
3471 return 0;
3472 }
3473 return 1;
3474}
3475
3476/* Fill <info> with HAProxy global info. <info> is preallocated
3477 * array of length <len>. The length of the aray must be
3478 * INF_TOTAL_FIELDS. If this length is less then this value, the
3479 * function returns 0, otherwise, it returns 1.
3480 */
3481int stats_fill_info(struct field *info, int len)
3482{
3483 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003484 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003485
3486#ifdef USE_OPENSSL
3487 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3488 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3489 int ssl_reuse = 0;
3490
3491 if (ssl_key_rate < ssl_sess_rate) {
3492 /* count the ssl reuse ratio and avoid overflows in both directions */
3493 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3494 }
3495#endif
3496
3497 if (len < INF_TOTAL_FIELDS)
3498 return 0;
3499
3500 chunk_reset(out);
3501 memset(info, 0, sizeof(*info) * len);
3502
3503 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3504 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3505 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3506
Yves Lafon95317282018-02-26 11:10:37 +01003507 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003508 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3509 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3510 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3511
3512 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3513 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3514
3515 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3516 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3517 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3518 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3519 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3520 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3521 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3522 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3523 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3524 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3525 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3526 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3527#ifdef USE_OPENSSL
3528 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3529 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3530 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3531#endif
3532 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3533 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3534 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3535 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3536 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3537 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3538 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3539 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3540 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3541
3542#ifdef USE_OPENSSL
3543 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3544 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3545 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3546 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3547 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3548 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3549 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3550 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3551 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3552 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3553#endif
3554 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3555 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3556 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3557#ifdef USE_ZLIB
3558 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3559 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3560#endif
3561 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003562 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003563 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3564 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3565 if (global.desc)
3566 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003567 info[INF_STOPPING] = mkf_u32(0, stopping);
3568 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003569 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003570 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003571 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003572 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003573 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003574 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003575
3576 return 1;
3577}
3578
3579/* This function dumps information onto the stream interface's read buffer.
3580 * It returns 0 as long as it does not complete, non-zero upon completion.
3581 * No state is used.
3582 */
3583static int stats_dump_info_to_buffer(struct stream_interface *si)
3584{
3585 struct appctx *appctx = __objt_appctx(si->end);
3586
3587 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3588 return 0;
3589
3590 chunk_reset(&trash);
3591
3592 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3593 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003594 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3595 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003596 else
3597 stats_dump_info_fields(&trash, info);
3598
Willy Tarreau06d80a92017-10-19 14:32:15 +02003599 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003600 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003601 return 0;
3602 }
3603
3604 return 1;
3605}
3606
Simon Horman6f6bb382017-01-04 09:37:26 +01003607/* This function dumps the schema onto the stream interface's read buffer.
3608 * It returns 0 as long as it does not complete, non-zero upon completion.
3609 * No state is used.
3610 *
3611 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3612 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3613 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003614static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003615{
3616
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003617 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003618
3619 chunk_strcat(out,
3620 "{"
3621 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3622 "\"oneOf\":["
3623 "{"
3624 "\"title\":\"Info\","
3625 "\"type\":\"array\","
3626 "\"items\":{"
3627 "\"properties\":{"
3628 "\"title\":\"InfoItem\","
3629 "\"type\":\"object\","
3630 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3631 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3632 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3633 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3634 "},"
3635 "\"required\":[\"field\",\"processNum\",\"tags\","
3636 "\"value\"]"
3637 "}"
3638 "},"
3639 "{"
3640 "\"title\":\"Stat\","
3641 "\"type\":\"array\","
3642 "\"items\":{"
3643 "\"title\":\"InfoItem\","
3644 "\"type\":\"object\","
3645 "\"properties\":{"
3646 "\"objType\":{"
3647 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3648 "\"Server\",\"Unknown\"]"
3649 "},"
3650 "\"proxyId\":{"
3651 "\"type\":\"integer\","
3652 "\"minimum\":0"
3653 "},"
3654 "\"id\":{"
3655 "\"type\":\"integer\","
3656 "\"minimum\":0"
3657 "},"
3658 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3659 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3660 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3661 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3662 "},"
3663 "\"required\":[\"objType\",\"proxyId\",\"id\","
3664 "\"field\",\"processNum\",\"tags\","
3665 "\"value\"]"
3666 "}"
3667 "},"
3668 "{"
3669 "\"title\":\"Error\","
3670 "\"type\":\"object\","
3671 "\"properties\":{"
3672 "\"errorStr\":{"
3673 "\"type\":\"string\""
3674 "},"
3675 "\"required\":[\"errorStr\"]"
3676 "}"
3677 "}"
3678 "],"
3679 "\"definitions\":{"
3680 "\"field\":{"
3681 "\"type\":\"object\","
3682 "\"pos\":{"
3683 "\"type\":\"integer\","
3684 "\"minimum\":0"
3685 "},"
3686 "\"name\":{"
3687 "\"type\":\"string\""
3688 "},"
3689 "\"required\":[\"pos\",\"name\"]"
3690 "},"
3691 "\"processNum\":{"
3692 "\"type\":\"integer\","
3693 "\"minimum\":1"
3694 "},"
3695 "\"tags\":{"
3696 "\"type\":\"object\","
3697 "\"origin\":{"
3698 "\"type\":\"string\","
3699 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3700 "\"Config\",\"Product\",\"Unknown\"]"
3701 "},"
3702 "\"nature\":{"
3703 "\"type\":\"string\","
3704 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3705 "\"Rate\",\"Counter\",\"Duration\","
3706 "\"Age\",\"Time\",\"Name\",\"Output\","
3707 "\"Avg\", \"Unknown\"]"
3708 "},"
3709 "\"scope\":{"
3710 "\"type\":\"string\","
3711 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3712 "\"System\",\"Unknown\"]"
3713 "},"
3714 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3715 "},"
3716 "\"typedValue\":{"
3717 "\"type\":\"object\","
3718 "\"oneOf\":["
3719 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3720 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3721 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3722 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3723 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3724 "],"
3725 "\"definitions\":{"
3726 "\"s32Value\":{"
3727 "\"properties\":{"
3728 "\"type\":{"
3729 "\"type\":\"string\","
3730 "\"enum\":[\"s32\"]"
3731 "},"
3732 "\"value\":{"
3733 "\"type\":\"integer\","
3734 "\"minimum\":-2147483648,"
3735 "\"maximum\":2147483647"
3736 "}"
3737 "},"
3738 "\"required\":[\"type\",\"value\"]"
3739 "},"
3740 "\"s64Value\":{"
3741 "\"properties\":{"
3742 "\"type\":{"
3743 "\"type\":\"string\","
3744 "\"enum\":[\"s64\"]"
3745 "},"
3746 "\"value\":{"
3747 "\"type\":\"integer\","
3748 "\"minimum\":-9007199254740991,"
3749 "\"maximum\":9007199254740991"
3750 "}"
3751 "},"
3752 "\"required\":[\"type\",\"value\"]"
3753 "},"
3754 "\"u32Value\":{"
3755 "\"properties\":{"
3756 "\"type\":{"
3757 "\"type\":\"string\","
3758 "\"enum\":[\"u32\"]"
3759 "},"
3760 "\"value\":{"
3761 "\"type\":\"integer\","
3762 "\"minimum\":0,"
3763 "\"maximum\":4294967295"
3764 "}"
3765 "},"
3766 "\"required\":[\"type\",\"value\"]"
3767 "},"
3768 "\"u64Value\":{"
3769 "\"properties\":{"
3770 "\"type\":{"
3771 "\"type\":\"string\","
3772 "\"enum\":[\"u64\"]"
3773 "},"
3774 "\"value\":{"
3775 "\"type\":\"integer\","
3776 "\"minimum\":0,"
3777 "\"maximum\":9007199254740991"
3778 "}"
3779 "},"
3780 "\"required\":[\"type\",\"value\"]"
3781 "},"
3782 "\"strValue\":{"
3783 "\"properties\":{"
3784 "\"type\":{"
3785 "\"type\":\"string\","
3786 "\"enum\":[\"str\"]"
3787 "},"
3788 "\"value\":{\"type\":\"string\"}"
3789 "},"
3790 "\"required\":[\"type\",\"value\"]"
3791 "},"
3792 "\"unknownValue\":{"
3793 "\"properties\":{"
3794 "\"type\":{"
3795 "\"type\":\"integer\","
3796 "\"minimum\":0"
3797 "},"
3798 "\"value\":{"
3799 "\"type\":\"string\","
3800 "\"enum\":[\"unknown\"]"
3801 "}"
3802 "},"
3803 "\"required\":[\"type\",\"value\"]"
3804 "}"
3805 "}"
3806 "}"
3807 "}"
3808 "}");
3809
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003810 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003811 chunk_reset(out);
3812 chunk_appendf(out,
3813 "{\"errorStr\":\"output buffer too short\"}");
3814 }
3815}
3816
3817/* This function dumps the schema onto the stream interface's read buffer.
3818 * It returns 0 as long as it does not complete, non-zero upon completion.
3819 * No state is used.
3820 */
3821static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3822{
3823 chunk_reset(&trash);
3824
3825 stats_dump_json_schema(&trash);
3826
Willy Tarreau06d80a92017-10-19 14:32:15 +02003827 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003828 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003829 return 0;
3830 }
3831
3832 return 1;
3833}
3834
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003835static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003836{
3837 struct proxy *px;
3838 struct server *sv;
3839 struct listener *li;
3840 int clrall = 0;
3841
3842 if (strcmp(args[2], "all") == 0)
3843 clrall = 1;
3844
3845 /* check permissions */
3846 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3847 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3848 return 1;
3849
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003850 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003851 if (clrall) {
3852 memset(&px->be_counters, 0, sizeof(px->be_counters));
3853 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3854 }
3855 else {
3856 px->be_counters.conn_max = 0;
3857 px->be_counters.p.http.rps_max = 0;
3858 px->be_counters.sps_max = 0;
3859 px->be_counters.cps_max = 0;
3860 px->be_counters.nbpend_max = 0;
3861
3862 px->fe_counters.conn_max = 0;
3863 px->fe_counters.p.http.rps_max = 0;
3864 px->fe_counters.sps_max = 0;
3865 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003866 }
3867
3868 for (sv = px->srv; sv; sv = sv->next)
3869 if (clrall)
3870 memset(&sv->counters, 0, sizeof(sv->counters));
3871 else {
3872 sv->counters.cur_sess_max = 0;
3873 sv->counters.nbpend_max = 0;
3874 sv->counters.sps_max = 0;
3875 }
3876
3877 list_for_each_entry(li, &px->conf.listeners, by_fe)
3878 if (li->counters) {
3879 if (clrall)
3880 memset(li->counters, 0, sizeof(*li->counters));
3881 else
3882 li->counters->conn_max = 0;
3883 }
3884 }
3885
3886 global.cps_max = 0;
3887 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003888 global.ssl_max = 0;
3889 global.ssl_fe_keys_max = 0;
3890 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003891
3892 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003893 return 1;
3894}
3895
3896
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003897static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003898{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003899 appctx->ctx.stats.scope_str = 0;
3900 appctx->ctx.stats.scope_len = 0;
3901 appctx->ctx.stats.flags = 0;
3902
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003903 if (strcmp(args[2], "typed") == 0)
3904 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003905 else if (strcmp(args[2], "json") == 0)
3906 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003907 return 0;
3908}
3909
3910
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003911static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003912{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003913 appctx->ctx.stats.scope_str = 0;
3914 appctx->ctx.stats.scope_len = 0;
3915 appctx->ctx.stats.flags = 0;
3916
Willy Tarreau2b812e22016-11-22 16:18:05 +01003917 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003918 struct proxy *px;
3919
3920 px = proxy_find_by_name(args[2], 0, 0);
3921 if (px)
3922 appctx->ctx.stats.iid = px->uuid;
3923 else
3924 appctx->ctx.stats.iid = atoi(args[2]);
3925
3926 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003927 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003928 appctx->ctx.cli.msg = "No such proxy.\n";
3929 appctx->st0 = CLI_ST_PRINT;
3930 return 1;
3931 }
3932
Willy Tarreau2b812e22016-11-22 16:18:05 +01003933 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003934 appctx->ctx.stats.type = atoi(args[3]);
3935 appctx->ctx.stats.sid = atoi(args[4]);
3936 if (strcmp(args[5], "typed") == 0)
3937 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003938 else if (strcmp(args[5], "json") == 0)
3939 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003940 }
3941 else if (strcmp(args[2], "typed") == 0)
3942 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003943 else if (strcmp(args[2], "json") == 0)
3944 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003945
Willy Tarreau2b812e22016-11-22 16:18:05 +01003946 return 0;
3947}
3948
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003949static int cli_io_handler_dump_info(struct appctx *appctx)
3950{
3951 return stats_dump_info_to_buffer(appctx->owner);
3952}
3953
Willy Tarreau2b812e22016-11-22 16:18:05 +01003954/* This I/O handler runs as an applet embedded in a stream interface. It is
3955 * used to send raw stats over a socket.
3956 */
3957static int cli_io_handler_dump_stat(struct appctx *appctx)
3958{
Christopher Fauletef779222018-10-31 08:47:01 +01003959 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003960}
3961
Simon Horman6f6bb382017-01-04 09:37:26 +01003962static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3963{
3964 return stats_dump_json_schema_to_buffer(appctx->owner);
3965}
3966
Willy Tarreau2b812e22016-11-22 16:18:05 +01003967/* register cli keywords */
3968static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003969 { { "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 +01003970 { { "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 +01003971 { { "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 +01003972 { { "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 +01003973 {{},}
3974}};
3975
Willy Tarreau0108d902018-11-25 19:14:37 +01003976INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3977
William Lallemand74c24fb2016-11-21 17:18:36 +01003978struct applet http_stats_applet = {
3979 .obj_type = OBJ_TYPE_APPLET,
3980 .name = "<STATS>", /* used for logging */
3981 .fct = http_stats_io_handler,
3982 .release = NULL,
3983};
3984
William Lallemand74c24fb2016-11-21 17:18:36 +01003985/*
3986 * Local variables:
3987 * c-indent-level: 8
3988 * c-basic-offset: 8
3989 * End:
3990 */