blob: f69aa90abd9baaf8942838a7d0f20f8b4190dc7e [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
Christopher Fauleta3618372018-12-13 21:59:56 +01002715 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002716
Christopher Fauleta3618372018-12-13 21:59:56 +01002717 if (IS_HTX_STRM(s)) {
2718 struct htx *htx = htxbuf(&s->req.buf);
2719 struct htx_blk *blk;
2720 size_t count = co_data(&s->req);
2721
2722 /* Remove the headers */
2723 blk = htx_get_head_blk(htx);
2724 while (count && blk) {
2725 enum htx_blk_type type = htx_get_blk_type(blk);
2726 uint32_t sz = htx_get_blksz(blk);
2727
2728 if (sz > count) {
2729 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2730 return 0;
2731 }
2732
2733 count -= sz;
2734 co_set_data(&s->req, co_data(&s->req) - sz);
2735 blk = htx_remove_blk(htx, blk);
2736
2737 if (type == HTX_BLK_EOH)
2738 break;
2739 }
2740
William Lallemand74c24fb2016-11-21 17:18:36 +01002741 /* too large request */
Christopher Fauleta3618372018-12-13 21:59:56 +01002742 if (htx->data + htx->extra > b_size(temp)) {
2743 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2744 goto out;
2745 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002746
William Lallemand74c24fb2016-11-21 17:18:36 +01002747 /* we need more data */
Christopher Fauleta3618372018-12-13 21:59:56 +01002748 if (htx->extra || htx->data > count) {
2749 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2750 return 0;
2751 }
2752
2753 while (count && blk) {
2754 enum htx_blk_type type = htx_get_blk_type(blk);
2755 uint32_t sz = htx_get_blksz(blk);
2756
2757 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2758 break;
2759 if (type == HTX_BLK_DATA) {
2760 struct ist v = htx_get_blk_value(htx, blk);
2761
2762 if (!chunk_memcat(temp, v.ptr, v.len)) {
2763 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2764 goto out;
2765 }
2766 }
2767
2768 count -= sz;
2769 co_set_data(&s->req, co_data(&s->req) - sz);
2770 blk = htx_remove_blk(htx, blk);
2771 }
2772 }
2773 else {
2774 int reql;
2775
2776 if (temp->size < s->txn->req.body_len) {
2777 /* too large request */
2778 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2779 goto out;
2780 }
2781
2782 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2783 s->txn->req.eoh + 2);
2784 if (reql <= 0) {
2785 /* we need more data */
2786 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2787 return 0;
2788 }
2789 temp->data = reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002790 }
2791
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002792 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002793 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002794 cur_param = next_param = end_params;
2795 *end_params = '\0';
2796
2797 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2798
2799 /*
2800 * Parse the parameters in reverse order to only store the last value.
2801 * From the html form, the backend and the action are at the end.
2802 */
2803 while (cur_param > first_param) {
2804 char *value;
2805 int poffset, plen;
2806
2807 cur_param--;
2808
2809 if ((*cur_param == '&') || (cur_param == first_param)) {
2810 reprocess_servers:
2811 /* Parse the key */
2812 poffset = (cur_param != first_param ? 1 : 0);
2813 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2814 if ((plen > 0) && (plen <= sizeof(key))) {
2815 strncpy(key, cur_param + poffset, plen);
2816 key[plen - 1] = '\0';
2817 } else {
2818 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2819 goto out;
2820 }
2821
2822 /* Parse the value */
2823 value = key;
2824 while (*value != '\0' && *value != '=') {
2825 value++;
2826 }
2827 if (*value == '=') {
2828 /* Ok, a value is found, we can mark the end of the key */
2829 *value++ = '\0';
2830 }
2831 if (url_decode(key) < 0 || url_decode(value) < 0)
2832 break;
2833
2834 /* Now we can check the key to see what to do */
2835 if (!px && (strcmp(key, "b") == 0)) {
2836 if ((px = proxy_be_by_name(value)) == NULL) {
2837 /* the backend name is unknown or ambiguous (duplicate names) */
2838 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2839 goto out;
2840 }
2841 }
2842 else if (!action && (strcmp(key, "action") == 0)) {
2843 if (strcmp(value, "ready") == 0) {
2844 action = ST_ADM_ACTION_READY;
2845 }
2846 else if (strcmp(value, "drain") == 0) {
2847 action = ST_ADM_ACTION_DRAIN;
2848 }
2849 else if (strcmp(value, "maint") == 0) {
2850 action = ST_ADM_ACTION_MAINT;
2851 }
2852 else if (strcmp(value, "shutdown") == 0) {
2853 action = ST_ADM_ACTION_SHUTDOWN;
2854 }
2855 else if (strcmp(value, "dhlth") == 0) {
2856 action = ST_ADM_ACTION_DHLTH;
2857 }
2858 else if (strcmp(value, "ehlth") == 0) {
2859 action = ST_ADM_ACTION_EHLTH;
2860 }
2861 else if (strcmp(value, "hrunn") == 0) {
2862 action = ST_ADM_ACTION_HRUNN;
2863 }
2864 else if (strcmp(value, "hnolb") == 0) {
2865 action = ST_ADM_ACTION_HNOLB;
2866 }
2867 else if (strcmp(value, "hdown") == 0) {
2868 action = ST_ADM_ACTION_HDOWN;
2869 }
2870 else if (strcmp(value, "dagent") == 0) {
2871 action = ST_ADM_ACTION_DAGENT;
2872 }
2873 else if (strcmp(value, "eagent") == 0) {
2874 action = ST_ADM_ACTION_EAGENT;
2875 }
2876 else if (strcmp(value, "arunn") == 0) {
2877 action = ST_ADM_ACTION_ARUNN;
2878 }
2879 else if (strcmp(value, "adown") == 0) {
2880 action = ST_ADM_ACTION_ADOWN;
2881 }
2882 /* else these are the old supported methods */
2883 else if (strcmp(value, "disable") == 0) {
2884 action = ST_ADM_ACTION_DISABLE;
2885 }
2886 else if (strcmp(value, "enable") == 0) {
2887 action = ST_ADM_ACTION_ENABLE;
2888 }
2889 else if (strcmp(value, "stop") == 0) {
2890 action = ST_ADM_ACTION_STOP;
2891 }
2892 else if (strcmp(value, "start") == 0) {
2893 action = ST_ADM_ACTION_START;
2894 }
2895 else {
2896 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2897 goto out;
2898 }
2899 }
2900 else if (strcmp(key, "s") == 0) {
2901 if (!(px && action)) {
2902 /*
2903 * Indicates that we'll need to reprocess the parameters
2904 * as soon as backend and action are known
2905 */
2906 if (!reprocess) {
2907 st_cur_param = cur_param;
2908 st_next_param = next_param;
2909 }
2910 reprocess = 1;
2911 }
2912 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002913 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002914 switch (action) {
2915 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002916 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002917 altered_servers++;
2918 total_servers++;
2919 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2920 }
2921 break;
2922 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002923 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002924 altered_servers++;
2925 total_servers++;
2926 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2927 }
2928 break;
2929 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002930 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002931 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2932 altered_servers++;
2933 total_servers++;
2934 }
2935 break;
2936 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002937 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002938 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2939 altered_servers++;
2940 total_servers++;
2941 }
2942 break;
2943 case ST_ADM_ACTION_DHLTH:
2944 if (sv->check.state & CHK_ST_CONFIGURED) {
2945 sv->check.state &= ~CHK_ST_ENABLED;
2946 altered_servers++;
2947 total_servers++;
2948 }
2949 break;
2950 case ST_ADM_ACTION_EHLTH:
2951 if (sv->check.state & CHK_ST_CONFIGURED) {
2952 sv->check.state |= CHK_ST_ENABLED;
2953 altered_servers++;
2954 total_servers++;
2955 }
2956 break;
2957 case ST_ADM_ACTION_HRUNN:
2958 if (!(sv->track)) {
2959 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002960 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002961 altered_servers++;
2962 total_servers++;
2963 }
2964 break;
2965 case ST_ADM_ACTION_HNOLB:
2966 if (!(sv->track)) {
2967 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002968 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002969 altered_servers++;
2970 total_servers++;
2971 }
2972 break;
2973 case ST_ADM_ACTION_HDOWN:
2974 if (!(sv->track)) {
2975 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002976 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002977 altered_servers++;
2978 total_servers++;
2979 }
2980 break;
2981 case ST_ADM_ACTION_DAGENT:
2982 if (sv->agent.state & CHK_ST_CONFIGURED) {
2983 sv->agent.state &= ~CHK_ST_ENABLED;
2984 altered_servers++;
2985 total_servers++;
2986 }
2987 break;
2988 case ST_ADM_ACTION_EAGENT:
2989 if (sv->agent.state & CHK_ST_CONFIGURED) {
2990 sv->agent.state |= CHK_ST_ENABLED;
2991 altered_servers++;
2992 total_servers++;
2993 }
2994 break;
2995 case ST_ADM_ACTION_ARUNN:
2996 if (sv->agent.state & CHK_ST_ENABLED) {
2997 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002998 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002999 altered_servers++;
3000 total_servers++;
3001 }
3002 break;
3003 case ST_ADM_ACTION_ADOWN:
3004 if (sv->agent.state & CHK_ST_ENABLED) {
3005 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003006 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003007 altered_servers++;
3008 total_servers++;
3009 }
3010 break;
3011 case ST_ADM_ACTION_READY:
3012 srv_adm_set_ready(sv);
3013 altered_servers++;
3014 total_servers++;
3015 break;
3016 case ST_ADM_ACTION_DRAIN:
3017 srv_adm_set_drain(sv);
3018 altered_servers++;
3019 total_servers++;
3020 break;
3021 case ST_ADM_ACTION_MAINT:
3022 srv_adm_set_maint(sv);
3023 altered_servers++;
3024 total_servers++;
3025 break;
3026 case ST_ADM_ACTION_SHUTDOWN:
3027 if (px->state != PR_STSTOPPED) {
3028 struct stream *sess, *sess_bck;
3029
3030 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3031 if (sess->srv_conn == sv)
3032 stream_shutdown(sess, SF_ERR_KILLED);
3033
3034 altered_servers++;
3035 total_servers++;
3036 }
3037 break;
3038 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003039 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003040 } else {
3041 /* the server name is unknown or ambiguous (duplicate names) */
3042 total_servers++;
3043 }
3044 }
3045 if (reprocess && px && action) {
3046 /* Now, we know the backend and the action chosen by the user.
3047 * We can safely restart from the first server parameter
3048 * to reprocess them
3049 */
3050 cur_param = st_cur_param;
3051 next_param = st_next_param;
3052 reprocess = 0;
3053 goto reprocess_servers;
3054 }
3055
3056 next_param = cur_param;
3057 }
3058 }
3059
3060 if (total_servers == 0) {
3061 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3062 }
3063 else if (altered_servers == 0) {
3064 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3065 }
3066 else if (altered_servers == total_servers) {
3067 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3068 }
3069 else {
3070 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3071 }
3072 out:
3073 return 1;
Christopher Fauletef779222018-10-31 08:47:01 +01003074}
3075
3076
3077static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
3078{
3079 struct stream *s = si_strm(si);
3080 struct uri_auth *uri = s->be->uri_auth;
3081 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003082 struct htx_sl *sl;
3083 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003084
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003085 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);
3086 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3087 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003088 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003089 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003090
3091 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
3092 !htx_add_header(htx, ist("Connection"), ist("close")))
3093 goto full;
3094 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3095 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3096 goto full;
3097 }
3098 else {
3099 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3100 goto full;
3101 }
3102
3103 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3104 const char *refresh = U2A(uri->refresh);
3105 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3106 goto full;
3107 }
3108
3109 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3110 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3111 goto full;
3112 }
3113
3114 if (!htx_add_endof(htx, HTX_BLK_EOH))
3115 goto full;
3116
3117 return 1;
3118
3119 full:
3120 htx_reset(htx);
3121 si_rx_room_blk(si);
3122 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003123}
3124
Christopher Fauletef779222018-10-31 08:47:01 +01003125
3126static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx)
3127{
3128 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3129 struct stream *s = si_strm(si);
3130 struct uri_auth *uri = s->be->uri_auth;
3131 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003132 struct htx_sl *sl;
3133 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003134
3135 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3136 scope_txt[0] = 0;
3137 if (appctx->ctx.stats.scope_len) {
3138 strcpy(scope_txt, STAT_SCOPE_PATTERN);
3139 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), co_head(si_oc(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3140 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3141 }
3142
3143 /* We don't want to land on the posted stats page because a refresh will
3144 * repost the data. We don't want this to happen on accident so we redirect
3145 * the browse to the stats page with a GET.
3146 */
3147 chunk_printf(&trash, "%s;st=%s%s%s%s",
3148 uri->uri_prefix,
3149 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3150 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3151 stat_status_codes[appctx->ctx.stats.st_code]) ?
3152 stat_status_codes[appctx->ctx.stats.st_code] :
3153 stat_status_codes[STAT_STATUS_UNKN],
3154 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3155 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3156 scope_txt);
3157
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003158 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3159 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3160 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003161 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003162 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003163
3164 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
3165 !htx_add_header(htx, ist("Connection"), ist("close")) ||
3166 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3167 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3168 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3169 goto full;
3170
3171 if (!htx_add_endof(htx, HTX_BLK_EOH))
3172 goto full;
3173
3174 return 1;
3175
3176full:
3177 htx_reset(htx);
3178 si_rx_room_blk(si);
3179 return 0;
3180}
William Lallemand74c24fb2016-11-21 17:18:36 +01003181
3182static int stats_send_http_headers(struct stream_interface *si)
3183{
3184 struct stream *s = si_strm(si);
3185 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003186 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003187
3188 chunk_printf(&trash,
3189 "HTTP/1.1 200 OK\r\n"
3190 "Cache-Control: no-cache\r\n"
3191 "Connection: close\r\n"
3192 "Content-Type: %s\r\n",
3193 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
3194
3195 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
3196 chunk_appendf(&trash, "Refresh: %d\r\n",
3197 uri->refresh);
3198
3199 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
3200
3201 if (appctx->ctx.stats.flags & STAT_CHUNKED)
3202 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
3203 else
3204 chunk_appendf(&trash, "\r\n");
3205
Willy Tarreau06d80a92017-10-19 14:32:15 +02003206 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003207 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003208 return 0;
3209 }
3210
3211 return 1;
3212}
3213
3214static int stats_send_http_redirect(struct stream_interface *si)
3215{
3216 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3217 struct stream *s = si_strm(si);
3218 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003219 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003220
3221 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3222 scope_txt[0] = 0;
3223 if (appctx->ctx.stats.scope_len) {
3224 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02003225 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 +01003226 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3227 }
3228
3229 /* We don't want to land on the posted stats page because a refresh will
3230 * repost the data. We don't want this to happen on accident so we redirect
3231 * the browse to the stats page with a GET.
3232 */
3233 chunk_printf(&trash,
3234 "HTTP/1.1 303 See Other\r\n"
3235 "Cache-Control: no-cache\r\n"
3236 "Content-Type: text/plain\r\n"
3237 "Connection: close\r\n"
3238 "Location: %s;st=%s%s%s%s\r\n"
3239 "Content-length: 0\r\n"
3240 "\r\n",
3241 uri->uri_prefix,
3242 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3243 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3244 stat_status_codes[appctx->ctx.stats.st_code]) ?
3245 stat_status_codes[appctx->ctx.stats.st_code] :
3246 stat_status_codes[STAT_STATUS_UNKN],
3247 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3248 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3249 scope_txt);
3250
Willy Tarreau06d80a92017-10-19 14:32:15 +02003251 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003252 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003253 return 0;
3254 }
3255
3256 return 1;
3257}
3258
Simon Horman05ee2132017-01-04 09:37:25 +01003259
William Lallemand74c24fb2016-11-21 17:18:36 +01003260/* This I/O handler runs as an applet embedded in a stream interface. It is
3261 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3262 * appctx->st0 contains the operation in progress (dump, done). The handler
3263 * automatically unregisters itself once transfer is complete.
3264 */
Christopher Fauletef779222018-10-31 08:47:01 +01003265static void htx_stats_io_handler(struct appctx *appctx)
3266{
3267 struct stream_interface *si = appctx->owner;
3268 struct stream *s = si_strm(si);
3269 struct channel *req = si_oc(si);
3270 struct channel *res = si_ic(si);
3271 struct htx *req_htx, *res_htx;
3272
3273 res_htx = htx_from_buf(&res->buf);
3274
3275 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3276 goto out;
3277
3278 /* Check if the input buffer is avalaible. */
3279 if (!b_size(&res->buf)) {
3280 si_rx_room_blk(si);
3281 goto out;
3282 }
3283
3284 /* check that the output is not closed */
3285 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3286 appctx->st0 = STAT_HTTP_DONE;
3287
3288 /* all states are processed in sequence */
3289 if (appctx->st0 == STAT_HTTP_HEAD) {
3290 if (stats_send_htx_headers(si, res_htx)) {
3291 if (s->txn->meth == HTTP_METH_HEAD)
3292 appctx->st0 = STAT_HTTP_DONE;
3293 else
3294 appctx->st0 = STAT_HTTP_DUMP;
3295 }
3296 }
3297
3298 if (appctx->st0 == STAT_HTTP_DUMP) {
3299 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3300 appctx->st0 = STAT_HTTP_DONE;
3301 }
3302
3303 if (appctx->st0 == STAT_HTTP_POST) {
3304 if (stats_process_http_post(si))
3305 appctx->st0 = STAT_HTTP_LAST;
3306 else if (si_oc(si)->flags & CF_SHUTR)
3307 appctx->st0 = STAT_HTTP_DONE;
3308 }
3309
3310 if (appctx->st0 == STAT_HTTP_LAST) {
3311 if (stats_send_htx_redirect(si, res_htx))
3312 appctx->st0 = STAT_HTTP_DONE;
3313 }
3314
3315 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Fauletb1b08212018-11-21 13:51:07 +01003316 /* Don't add EOD and TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003317 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3318 si_rx_room_blk(si);
3319 goto out;
3320 }
3321
3322 /* eat the whole request */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003323 req_htx = htxbuf(&req->buf);
3324 htx_reset(req_htx);
3325 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9b95d312018-11-19 21:58:10 +01003326 co_set_data(req, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01003327 res->flags |= CF_READ_NULL;
3328 si_shutr(si);
3329 }
3330
3331 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3332 si_shutw(si);
3333
3334 if (appctx->st0 == STAT_HTTP_DONE) {
3335 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3336 si_shutr(si);
3337 res->flags |= CF_READ_NULL;
3338 }
3339 }
3340 out:
3341 /* we have left the request in the buffer for the case where we
3342 * process a POST, and this automatically re-enables activity on
3343 * read. It's better to indicate that we want to stop reading when
3344 * we're sending, so that we know there's at most one direction
3345 * deciding to wake the applet up. It saves it from looping when
3346 * emitting large blocks into small TCP windows.
3347 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003348 htx_to_buf(res_htx, &res->buf);
3349 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003350 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003351}
3352
3353
3354/* This I/O handler runs as an applet embedded in a stream interface. It is
3355 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3356 * appctx->st0 contains the operation in progress (dump, done). The handler
3357 * automatically unregisters itself once transfer is complete.
3358 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003359static void http_stats_io_handler(struct appctx *appctx)
3360{
3361 struct stream_interface *si = appctx->owner;
3362 struct stream *s = si_strm(si);
3363 struct channel *req = si_oc(si);
3364 struct channel *res = si_ic(si);
3365
Christopher Fauletef779222018-10-31 08:47:01 +01003366 if (IS_HTX_STRM(s))
3367 return htx_stats_io_handler(appctx);
3368
William Lallemand74c24fb2016-11-21 17:18:36 +01003369 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3370 goto out;
3371
Joseph Herlant29023ec2018-11-15 13:39:46 -08003372 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003373 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003374 /* already subscribed, we'll be called later once the buffer is
3375 * available.
3376 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003377 goto out;
3378 }
3379
William Lallemand74c24fb2016-11-21 17:18:36 +01003380 /* check that the output is not closed */
3381 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3382 appctx->st0 = STAT_HTTP_DONE;
3383
3384 /* all states are processed in sequence */
3385 if (appctx->st0 == STAT_HTTP_HEAD) {
3386 if (stats_send_http_headers(si)) {
3387 if (s->txn->meth == HTTP_METH_HEAD)
3388 appctx->st0 = STAT_HTTP_DONE;
3389 else
3390 appctx->st0 = STAT_HTTP_DUMP;
3391 }
3392 }
3393
3394 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003395 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003396 unsigned int data_len;
3397 unsigned int last_len;
3398 unsigned int last_fwd = 0;
3399
3400 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3401 /* One difficulty we're facing is that we must prevent
3402 * the input data from being automatically forwarded to
3403 * the output area. For this, we temporarily disable
3404 * forwarding on the channel.
3405 */
3406 last_fwd = si_ic(si)->to_forward;
3407 si_ic(si)->to_forward = 0;
3408 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003409 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003410 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003411 si_ic(si)->to_forward = last_fwd;
3412 goto out;
3413 }
3414 }
3415
Willy Tarreau97f538b2018-06-15 19:41:31 +02003416 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003417 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003418 appctx->st0 = STAT_HTTP_DONE;
3419
Willy Tarreau97f538b2018-06-15 19:41:31 +02003420 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003421
3422 /* Now we must either adjust or remove the chunk size. This is
3423 * not easy because the chunk size might wrap at the end of the
3424 * buffer, so we pretend we have nothing in the buffer, we write
3425 * the size, then restore the buffer's contents. Note that we can
3426 * only do that because no forwarding is scheduled on the stats
3427 * applet.
3428 */
3429 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3430 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003431 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003432
3433 if (last_len != data_len) {
3434 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003435 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003436 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003437
3438 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003439 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003440 }
3441 /* now re-enable forwarding */
3442 channel_forward(si_ic(si), last_fwd);
3443 }
3444 }
3445
3446 if (appctx->st0 == STAT_HTTP_POST) {
3447 if (stats_process_http_post(si))
3448 appctx->st0 = STAT_HTTP_LAST;
3449 else if (si_oc(si)->flags & CF_SHUTR)
3450 appctx->st0 = STAT_HTTP_DONE;
3451 }
3452
3453 if (appctx->st0 == STAT_HTTP_LAST) {
3454 if (stats_send_http_redirect(si))
3455 appctx->st0 = STAT_HTTP_DONE;
3456 }
3457
3458 if (appctx->st0 == STAT_HTTP_DONE) {
3459 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3460 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003461 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003462 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003463 goto out;
3464 }
3465 }
3466 /* eat the whole request */
Willy Tarreau97f538b2018-06-15 19:41:31 +02003467 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003468 res->flags |= CF_READ_NULL;
3469 si_shutr(si);
3470 }
3471
3472 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3473 si_shutw(si);
3474
3475 if (appctx->st0 == STAT_HTTP_DONE) {
3476 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3477 si_shutr(si);
3478 res->flags |= CF_READ_NULL;
3479 }
3480 }
3481 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003482 /* we have left the request in the buffer for the case where we
3483 * process a POST, and this automatically re-enables activity on
3484 * read. It's better to indicate that we want to stop reading when
3485 * we're sending, so that we know there's at most one direction
3486 * deciding to wake the applet up. It saves it from looping when
3487 * emitting large blocks into small TCP windows.
3488 */
3489 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003490 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003491}
3492
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003493/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003494static int stats_dump_info_fields(struct buffer *out,
3495 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003496{
3497 int field;
3498
3499 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3500 if (!field_format(info, field))
3501 continue;
3502
3503 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3504 return 0;
3505 if (!stats_emit_raw_data_field(out, &info[field]))
3506 return 0;
3507 if (!chunk_strcat(out, "\n"))
3508 return 0;
3509 }
3510 return 1;
3511}
3512
3513/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003514static int stats_dump_typed_info_fields(struct buffer *out,
3515 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003516{
3517 int field;
3518
3519 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3520 if (!field_format(info, field))
3521 continue;
3522
3523 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3524 return 0;
3525 if (!stats_emit_field_tags(out, &info[field], ':'))
3526 return 0;
3527 if (!stats_emit_typed_data_field(out, &info[field]))
3528 return 0;
3529 if (!chunk_strcat(out, "\n"))
3530 return 0;
3531 }
3532 return 1;
3533}
3534
3535/* Fill <info> with HAProxy global info. <info> is preallocated
3536 * array of length <len>. The length of the aray must be
3537 * INF_TOTAL_FIELDS. If this length is less then this value, the
3538 * function returns 0, otherwise, it returns 1.
3539 */
3540int stats_fill_info(struct field *info, int len)
3541{
3542 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003543 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003544
3545#ifdef USE_OPENSSL
3546 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3547 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3548 int ssl_reuse = 0;
3549
3550 if (ssl_key_rate < ssl_sess_rate) {
3551 /* count the ssl reuse ratio and avoid overflows in both directions */
3552 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3553 }
3554#endif
3555
3556 if (len < INF_TOTAL_FIELDS)
3557 return 0;
3558
3559 chunk_reset(out);
3560 memset(info, 0, sizeof(*info) * len);
3561
3562 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3563 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3564 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3565
Yves Lafon95317282018-02-26 11:10:37 +01003566 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003567 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3568 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3569 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3570
3571 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3572 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3573
3574 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3575 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3576 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3577 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3578 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3579 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3580 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3581 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3582 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3583 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3584 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3585 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3586#ifdef USE_OPENSSL
3587 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3588 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3589 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3590#endif
3591 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3592 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3593 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3594 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3595 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3596 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3597 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3598 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3599 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3600
3601#ifdef USE_OPENSSL
3602 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3603 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3604 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3605 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3606 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3607 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3608 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3609 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3610 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3611 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3612#endif
3613 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3614 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3615 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3616#ifdef USE_ZLIB
3617 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3618 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3619#endif
3620 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003621 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003622 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3623 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3624 if (global.desc)
3625 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003626 info[INF_STOPPING] = mkf_u32(0, stopping);
3627 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003628 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003629 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003630 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003631 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003632 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003633 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003634
3635 return 1;
3636}
3637
3638/* This function dumps information onto the stream interface's read buffer.
3639 * It returns 0 as long as it does not complete, non-zero upon completion.
3640 * No state is used.
3641 */
3642static int stats_dump_info_to_buffer(struct stream_interface *si)
3643{
3644 struct appctx *appctx = __objt_appctx(si->end);
3645
3646 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3647 return 0;
3648
3649 chunk_reset(&trash);
3650
3651 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3652 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003653 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3654 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003655 else
3656 stats_dump_info_fields(&trash, info);
3657
Willy Tarreau06d80a92017-10-19 14:32:15 +02003658 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003659 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003660 return 0;
3661 }
3662
3663 return 1;
3664}
3665
Simon Horman6f6bb382017-01-04 09:37:26 +01003666/* This function dumps the schema onto the stream interface's read buffer.
3667 * It returns 0 as long as it does not complete, non-zero upon completion.
3668 * No state is used.
3669 *
3670 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3671 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3672 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003673static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003674{
3675
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003676 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003677
3678 chunk_strcat(out,
3679 "{"
3680 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3681 "\"oneOf\":["
3682 "{"
3683 "\"title\":\"Info\","
3684 "\"type\":\"array\","
3685 "\"items\":{"
3686 "\"properties\":{"
3687 "\"title\":\"InfoItem\","
3688 "\"type\":\"object\","
3689 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3690 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3691 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3692 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3693 "},"
3694 "\"required\":[\"field\",\"processNum\",\"tags\","
3695 "\"value\"]"
3696 "}"
3697 "},"
3698 "{"
3699 "\"title\":\"Stat\","
3700 "\"type\":\"array\","
3701 "\"items\":{"
3702 "\"title\":\"InfoItem\","
3703 "\"type\":\"object\","
3704 "\"properties\":{"
3705 "\"objType\":{"
3706 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3707 "\"Server\",\"Unknown\"]"
3708 "},"
3709 "\"proxyId\":{"
3710 "\"type\":\"integer\","
3711 "\"minimum\":0"
3712 "},"
3713 "\"id\":{"
3714 "\"type\":\"integer\","
3715 "\"minimum\":0"
3716 "},"
3717 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3718 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3719 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3720 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3721 "},"
3722 "\"required\":[\"objType\",\"proxyId\",\"id\","
3723 "\"field\",\"processNum\",\"tags\","
3724 "\"value\"]"
3725 "}"
3726 "},"
3727 "{"
3728 "\"title\":\"Error\","
3729 "\"type\":\"object\","
3730 "\"properties\":{"
3731 "\"errorStr\":{"
3732 "\"type\":\"string\""
3733 "},"
3734 "\"required\":[\"errorStr\"]"
3735 "}"
3736 "}"
3737 "],"
3738 "\"definitions\":{"
3739 "\"field\":{"
3740 "\"type\":\"object\","
3741 "\"pos\":{"
3742 "\"type\":\"integer\","
3743 "\"minimum\":0"
3744 "},"
3745 "\"name\":{"
3746 "\"type\":\"string\""
3747 "},"
3748 "\"required\":[\"pos\",\"name\"]"
3749 "},"
3750 "\"processNum\":{"
3751 "\"type\":\"integer\","
3752 "\"minimum\":1"
3753 "},"
3754 "\"tags\":{"
3755 "\"type\":\"object\","
3756 "\"origin\":{"
3757 "\"type\":\"string\","
3758 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3759 "\"Config\",\"Product\",\"Unknown\"]"
3760 "},"
3761 "\"nature\":{"
3762 "\"type\":\"string\","
3763 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3764 "\"Rate\",\"Counter\",\"Duration\","
3765 "\"Age\",\"Time\",\"Name\",\"Output\","
3766 "\"Avg\", \"Unknown\"]"
3767 "},"
3768 "\"scope\":{"
3769 "\"type\":\"string\","
3770 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3771 "\"System\",\"Unknown\"]"
3772 "},"
3773 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3774 "},"
3775 "\"typedValue\":{"
3776 "\"type\":\"object\","
3777 "\"oneOf\":["
3778 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3779 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3780 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3781 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3782 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3783 "],"
3784 "\"definitions\":{"
3785 "\"s32Value\":{"
3786 "\"properties\":{"
3787 "\"type\":{"
3788 "\"type\":\"string\","
3789 "\"enum\":[\"s32\"]"
3790 "},"
3791 "\"value\":{"
3792 "\"type\":\"integer\","
3793 "\"minimum\":-2147483648,"
3794 "\"maximum\":2147483647"
3795 "}"
3796 "},"
3797 "\"required\":[\"type\",\"value\"]"
3798 "},"
3799 "\"s64Value\":{"
3800 "\"properties\":{"
3801 "\"type\":{"
3802 "\"type\":\"string\","
3803 "\"enum\":[\"s64\"]"
3804 "},"
3805 "\"value\":{"
3806 "\"type\":\"integer\","
3807 "\"minimum\":-9007199254740991,"
3808 "\"maximum\":9007199254740991"
3809 "}"
3810 "},"
3811 "\"required\":[\"type\",\"value\"]"
3812 "},"
3813 "\"u32Value\":{"
3814 "\"properties\":{"
3815 "\"type\":{"
3816 "\"type\":\"string\","
3817 "\"enum\":[\"u32\"]"
3818 "},"
3819 "\"value\":{"
3820 "\"type\":\"integer\","
3821 "\"minimum\":0,"
3822 "\"maximum\":4294967295"
3823 "}"
3824 "},"
3825 "\"required\":[\"type\",\"value\"]"
3826 "},"
3827 "\"u64Value\":{"
3828 "\"properties\":{"
3829 "\"type\":{"
3830 "\"type\":\"string\","
3831 "\"enum\":[\"u64\"]"
3832 "},"
3833 "\"value\":{"
3834 "\"type\":\"integer\","
3835 "\"minimum\":0,"
3836 "\"maximum\":9007199254740991"
3837 "}"
3838 "},"
3839 "\"required\":[\"type\",\"value\"]"
3840 "},"
3841 "\"strValue\":{"
3842 "\"properties\":{"
3843 "\"type\":{"
3844 "\"type\":\"string\","
3845 "\"enum\":[\"str\"]"
3846 "},"
3847 "\"value\":{\"type\":\"string\"}"
3848 "},"
3849 "\"required\":[\"type\",\"value\"]"
3850 "},"
3851 "\"unknownValue\":{"
3852 "\"properties\":{"
3853 "\"type\":{"
3854 "\"type\":\"integer\","
3855 "\"minimum\":0"
3856 "},"
3857 "\"value\":{"
3858 "\"type\":\"string\","
3859 "\"enum\":[\"unknown\"]"
3860 "}"
3861 "},"
3862 "\"required\":[\"type\",\"value\"]"
3863 "}"
3864 "}"
3865 "}"
3866 "}"
3867 "}");
3868
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003869 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003870 chunk_reset(out);
3871 chunk_appendf(out,
3872 "{\"errorStr\":\"output buffer too short\"}");
3873 }
3874}
3875
3876/* This function dumps the schema onto the stream interface's read buffer.
3877 * It returns 0 as long as it does not complete, non-zero upon completion.
3878 * No state is used.
3879 */
3880static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3881{
3882 chunk_reset(&trash);
3883
3884 stats_dump_json_schema(&trash);
3885
Willy Tarreau06d80a92017-10-19 14:32:15 +02003886 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003887 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003888 return 0;
3889 }
3890
3891 return 1;
3892}
3893
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003894static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003895{
3896 struct proxy *px;
3897 struct server *sv;
3898 struct listener *li;
3899 int clrall = 0;
3900
3901 if (strcmp(args[2], "all") == 0)
3902 clrall = 1;
3903
3904 /* check permissions */
3905 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3906 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3907 return 1;
3908
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003909 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003910 if (clrall) {
3911 memset(&px->be_counters, 0, sizeof(px->be_counters));
3912 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3913 }
3914 else {
3915 px->be_counters.conn_max = 0;
3916 px->be_counters.p.http.rps_max = 0;
3917 px->be_counters.sps_max = 0;
3918 px->be_counters.cps_max = 0;
3919 px->be_counters.nbpend_max = 0;
3920
3921 px->fe_counters.conn_max = 0;
3922 px->fe_counters.p.http.rps_max = 0;
3923 px->fe_counters.sps_max = 0;
3924 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003925 }
3926
3927 for (sv = px->srv; sv; sv = sv->next)
3928 if (clrall)
3929 memset(&sv->counters, 0, sizeof(sv->counters));
3930 else {
3931 sv->counters.cur_sess_max = 0;
3932 sv->counters.nbpend_max = 0;
3933 sv->counters.sps_max = 0;
3934 }
3935
3936 list_for_each_entry(li, &px->conf.listeners, by_fe)
3937 if (li->counters) {
3938 if (clrall)
3939 memset(li->counters, 0, sizeof(*li->counters));
3940 else
3941 li->counters->conn_max = 0;
3942 }
3943 }
3944
3945 global.cps_max = 0;
3946 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003947 global.ssl_max = 0;
3948 global.ssl_fe_keys_max = 0;
3949 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003950
3951 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003952 return 1;
3953}
3954
3955
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003956static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003957{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003958 appctx->ctx.stats.scope_str = 0;
3959 appctx->ctx.stats.scope_len = 0;
3960 appctx->ctx.stats.flags = 0;
3961
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003962 if (strcmp(args[2], "typed") == 0)
3963 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003964 else if (strcmp(args[2], "json") == 0)
3965 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003966 return 0;
3967}
3968
3969
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003970static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003971{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003972 appctx->ctx.stats.scope_str = 0;
3973 appctx->ctx.stats.scope_len = 0;
3974 appctx->ctx.stats.flags = 0;
3975
Willy Tarreau2b812e22016-11-22 16:18:05 +01003976 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003977 struct proxy *px;
3978
3979 px = proxy_find_by_name(args[2], 0, 0);
3980 if (px)
3981 appctx->ctx.stats.iid = px->uuid;
3982 else
3983 appctx->ctx.stats.iid = atoi(args[2]);
3984
3985 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003986 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003987 appctx->ctx.cli.msg = "No such proxy.\n";
3988 appctx->st0 = CLI_ST_PRINT;
3989 return 1;
3990 }
3991
Willy Tarreau2b812e22016-11-22 16:18:05 +01003992 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003993 appctx->ctx.stats.type = atoi(args[3]);
3994 appctx->ctx.stats.sid = atoi(args[4]);
3995 if (strcmp(args[5], "typed") == 0)
3996 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003997 else if (strcmp(args[5], "json") == 0)
3998 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003999 }
4000 else if (strcmp(args[2], "typed") == 0)
4001 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004002 else if (strcmp(args[2], "json") == 0)
4003 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004004
Willy Tarreau2b812e22016-11-22 16:18:05 +01004005 return 0;
4006}
4007
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004008static int cli_io_handler_dump_info(struct appctx *appctx)
4009{
4010 return stats_dump_info_to_buffer(appctx->owner);
4011}
4012
Willy Tarreau2b812e22016-11-22 16:18:05 +01004013/* This I/O handler runs as an applet embedded in a stream interface. It is
4014 * used to send raw stats over a socket.
4015 */
4016static int cli_io_handler_dump_stat(struct appctx *appctx)
4017{
Christopher Fauletef779222018-10-31 08:47:01 +01004018 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004019}
4020
Simon Horman6f6bb382017-01-04 09:37:26 +01004021static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4022{
4023 return stats_dump_json_schema_to_buffer(appctx->owner);
4024}
4025
Willy Tarreau2b812e22016-11-22 16:18:05 +01004026/* register cli keywords */
4027static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004028 { { "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 +01004029 { { "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 +01004030 { { "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 +01004031 { { "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 +01004032 {{},}
4033}};
4034
Willy Tarreau0108d902018-11-25 19:14:37 +01004035INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4036
William Lallemand74c24fb2016-11-21 17:18:36 +01004037struct applet http_stats_applet = {
4038 .obj_type = OBJ_TYPE_APPLET,
4039 .name = "<STATS>", /* used for logging */
4040 .fct = http_stats_io_handler,
4041 .release = NULL,
4042};
4043
William Lallemand74c24fb2016-11-21 17:18:36 +01004044/*
4045 * Local variables:
4046 * c-indent-level: 8
4047 * c-basic-offset: 8
4048 * End:
4049 */