blob: 5e54dd5b2d8c2342b6a373de5e19ec5de81d7be6 [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;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100248 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100249 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100250 }
251 else {
252 if (ci_putchk(chn, chk) == -1)
253 return 0;
254 }
255 return 1;
256}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100257
William Lallemand74c24fb2016-11-21 17:18:36 +0100258/*
259 * http_stats_io_handler()
260 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
261 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100262 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100263 * -> stats_dump_html_head() // emits the HTML headers
264 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
265 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
266 * -> stats_dump_html_px_hdr()
267 * -> stats_dump_fe_stats()
268 * -> stats_dump_li_stats()
269 * -> stats_dump_sv_stats()
270 * -> stats_dump_be_stats()
271 * -> stats_dump_html_px_end()
272 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100273 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100274 */
275
276
277extern const char *stat_status_codes[];
278
279/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
280 * for clearing it if needed.
281 * NOTE: Some tools happen to rely on the field position instead of its name,
282 * so please only append new fields at the end, never in the middle.
283 */
284static void stats_dump_csv_header()
285{
286 int field;
287
288 chunk_appendf(&trash, "# ");
289 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
290 chunk_appendf(&trash, "%s,", stat_field_names[field]);
291
292 chunk_appendf(&trash, "\n");
293}
294
295
296/* Emits a stats field without any surrounding element and properly encoded to
297 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
298 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200299int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100300{
301 switch (field_format(f, 0)) {
302 case FF_EMPTY: return 1;
303 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
304 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
305 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
306 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
307 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
308 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
309 }
310}
311
312/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
313 * output is supposed to be used on its own line. Returns non-zero on success, 0
314 * if the buffer is full.
315 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200316int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100317{
318 switch (field_format(f, 0)) {
319 case FF_EMPTY: return 1;
320 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
321 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
322 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
323 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
324 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
325 default: return chunk_appendf(out, "%08x:?", f->type);
326 }
327}
328
Simon Horman05ee2132017-01-04 09:37:25 +0100329/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
330 * the recommendation for interoperable integers in section 6 of RFC 7159.
331 */
332#define JSON_INT_MAX ((1ULL << 53) - 1)
333#define JSON_INT_MIN (0 - JSON_INT_MAX)
334
335/* Emits a stats field value and its type in JSON.
336 * Returns non-zero on success, 0 on error.
337 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200338int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100339{
340 int old_len;
341 char buf[20];
342 const char *type, *value = buf, *quote = "";
343
344 switch (field_format(f, 0)) {
345 case FF_EMPTY: return 1;
346 case FF_S32: type = "\"s32\"";
347 snprintf(buf, sizeof(buf), "%d", f->u.s32);
348 break;
349 case FF_U32: type = "\"u32\"";
350 snprintf(buf, sizeof(buf), "%u", f->u.u32);
351 break;
352 case FF_S64: type = "\"s64\"";
353 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
354 return 0;
355 type = "\"s64\"";
356 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
357 break;
358 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
359 return 0;
360 type = "\"u64\"";
361 snprintf(buf, sizeof(buf), "%llu",
362 (unsigned long long) f->u.u64);
363 break;
364 case FF_STR: type = "\"str\"";
365 value = field_str(f, 0);
366 quote = "\"";
367 break;
368 default: snprintf(buf, sizeof(buf), "%u", f->type);
369 type = buf;
370 value = "unknown";
371 quote = "\"";
372 break;
373 }
374
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200375 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100376 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
377 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200378 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100379}
380
William Lallemand74c24fb2016-11-21 17:18:36 +0100381/* Emits an encoding of the field type on 3 characters followed by a delimiter.
382 * Returns non-zero on success, 0 if the buffer is full.
383 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200384int stats_emit_field_tags(struct buffer *out, const struct field *f,
385 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100386{
387 char origin, nature, scope;
388
389 switch (field_origin(f, 0)) {
390 case FO_METRIC: origin = 'M'; break;
391 case FO_STATUS: origin = 'S'; break;
392 case FO_KEY: origin = 'K'; break;
393 case FO_CONFIG: origin = 'C'; break;
394 case FO_PRODUCT: origin = 'P'; break;
395 default: origin = '?'; break;
396 }
397
398 switch (field_nature(f, 0)) {
399 case FN_GAUGE: nature = 'G'; break;
400 case FN_LIMIT: nature = 'L'; break;
401 case FN_MIN: nature = 'm'; break;
402 case FN_MAX: nature = 'M'; break;
403 case FN_RATE: nature = 'R'; break;
404 case FN_COUNTER: nature = 'C'; break;
405 case FN_DURATION: nature = 'D'; break;
406 case FN_AGE: nature = 'A'; break;
407 case FN_TIME: nature = 'T'; break;
408 case FN_NAME: nature = 'N'; break;
409 case FN_OUTPUT: nature = 'O'; break;
410 case FN_AVG: nature = 'a'; break;
411 default: nature = '?'; break;
412 }
413
414 switch (field_scope(f, 0)) {
415 case FS_PROCESS: scope = 'P'; break;
416 case FS_SERVICE: scope = 'S'; break;
417 case FS_SYSTEM: scope = 's'; break;
418 case FS_CLUSTER: scope = 'C'; break;
419 default: scope = '?'; break;
420 }
421
422 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
423}
424
Simon Horman05ee2132017-01-04 09:37:25 +0100425/* Emits an encoding of the field type as JSON.
426 * Returns non-zero on success, 0 if the buffer is full.
427 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200428int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100429{
430 const char *origin, *nature, *scope;
431 int old_len;
432
433 switch (field_origin(f, 0)) {
434 case FO_METRIC: origin = "Metric"; break;
435 case FO_STATUS: origin = "Status"; break;
436 case FO_KEY: origin = "Key"; break;
437 case FO_CONFIG: origin = "Config"; break;
438 case FO_PRODUCT: origin = "Product"; break;
439 default: origin = "Unknown"; break;
440 }
441
442 switch (field_nature(f, 0)) {
443 case FN_GAUGE: nature = "Gauge"; break;
444 case FN_LIMIT: nature = "Limit"; break;
445 case FN_MIN: nature = "Min"; break;
446 case FN_MAX: nature = "Max"; break;
447 case FN_RATE: nature = "Rate"; break;
448 case FN_COUNTER: nature = "Counter"; break;
449 case FN_DURATION: nature = "Duration"; break;
450 case FN_AGE: nature = "Age"; break;
451 case FN_TIME: nature = "Time"; break;
452 case FN_NAME: nature = "Name"; break;
453 case FN_OUTPUT: nature = "Output"; break;
454 case FN_AVG: nature = "Avg"; break;
455 default: nature = "Unknown"; break;
456 }
457
458 switch (field_scope(f, 0)) {
459 case FS_PROCESS: scope = "Process"; break;
460 case FS_SERVICE: scope = "Service"; break;
461 case FS_SYSTEM: scope = "System"; break;
462 case FS_CLUSTER: scope = "Cluster"; break;
463 default: scope = "Unknown"; break;
464 }
465
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200466 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100467 chunk_appendf(out, "\"tags\":{"
468 "\"origin\":\"%s\","
469 "\"nature\":\"%s\","
470 "\"scope\":\"%s\""
471 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200472 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100473}
William Lallemand74c24fb2016-11-21 17:18:36 +0100474
475/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200476static int stats_dump_fields_csv(struct buffer *out,
477 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100478{
479 int field;
480
481 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
482 if (!stats_emit_raw_data_field(out, &stats[field]))
483 return 0;
484 if (!chunk_strcat(out, ","))
485 return 0;
486 }
487 chunk_strcat(out, "\n");
488 return 1;
489}
490
491/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200492static int stats_dump_fields_typed(struct buffer *out,
493 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100494{
495 int field;
496
497 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
498 if (!stats[field].type)
499 continue;
500
501 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
502 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
503 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
504 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
505 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
506 '?',
507 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
508 field, stat_field_names[field], stats[ST_F_PID].u.u32);
509
510 if (!stats_emit_field_tags(out, &stats[field], ':'))
511 return 0;
512 if (!stats_emit_typed_data_field(out, &stats[field]))
513 return 0;
514 if (!chunk_strcat(out, "\n"))
515 return 0;
516 }
517 return 1;
518}
519
Simon Horman05ee2132017-01-04 09:37:25 +0100520/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200521static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100522 const struct field *info)
523{
524 int field;
525 int started = 0;
526
527 if (!chunk_strcat(out, "["))
528 return 0;
529
530 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
531 int old_len;
532
533 if (!field_format(info, field))
534 continue;
535
536 if (started && !chunk_strcat(out, ","))
537 goto err;
538 started = 1;
539
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200540 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100541 chunk_appendf(out,
542 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
543 "\"processNum\":%u,",
544 field, info_field_names[field],
545 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200546 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100547 goto err;
548
549 if (!stats_emit_json_field_tags(out, &info[field]))
550 goto err;
551
552 if (!stats_emit_json_data_field(out, &info[field]))
553 goto err;
554
555 if (!chunk_strcat(out, "}"))
556 goto err;
557 }
558
559 if (!chunk_strcat(out, "]"))
560 goto err;
561 return 1;
562
563err:
564 chunk_reset(out);
565 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
566 return 0;
567}
568
569/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200570static int stats_dump_fields_json(struct buffer *out,
571 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100572 int first_stat)
573{
574 int field;
575 int started = 0;
576
577 if (!first_stat && !chunk_strcat(out, ","))
578 return 0;
579 if (!chunk_strcat(out, "["))
580 return 0;
581
582 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
583 const char *obj_type;
584 int old_len;
585
586 if (!stats[field].type)
587 continue;
588
589 if (started && !chunk_strcat(out, ","))
590 goto err;
591 started = 1;
592
593 switch (stats[ST_F_TYPE].u.u32) {
594 case STATS_TYPE_FE: obj_type = "Frontend"; break;
595 case STATS_TYPE_BE: obj_type = "Backend"; break;
596 case STATS_TYPE_SO: obj_type = "Listener"; break;
597 case STATS_TYPE_SV: obj_type = "Server"; break;
598 default: obj_type = "Unknown"; break;
599 }
600
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200601 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100602 chunk_appendf(out,
603 "{"
604 "\"objType\":\"%s\","
605 "\"proxyId\":%d,"
606 "\"id\":%d,"
607 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
608 "\"processNum\":%u,",
609 obj_type, stats[ST_F_IID].u.u32,
610 stats[ST_F_SID].u.u32, field,
611 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200612 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100613 goto err;
614
615 if (!stats_emit_json_field_tags(out, &stats[field]))
616 goto err;
617
618 if (!stats_emit_json_data_field(out, &stats[field]))
619 goto err;
620
621 if (!chunk_strcat(out, "}"))
622 goto err;
623 }
624
625 if (!chunk_strcat(out, "]"))
626 goto err;
627
628 return 1;
629
630err:
631 chunk_reset(out);
632 if (!first_stat)
633 chunk_strcat(out, ",");
634 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
635 return 0;
636}
637
William Lallemand74c24fb2016-11-21 17:18:36 +0100638/* Dump all fields from <stats> into <out> using the HTML format. A column is
639 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
640 * are provided if ST_SHLGNDS is present in <flags>.
641 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200642static int stats_dump_fields_html(struct buffer *out,
643 const struct field *stats,
644 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100645{
Willy Tarreau83061a82018-07-13 11:56:34 +0200646 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100647
648 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
649 chunk_appendf(out,
650 /* name, queue */
651 "<tr class=\"frontend\">");
652
653 if (flags & ST_SHOWADMIN) {
654 /* Column sub-heading for Enable or Disable server */
655 chunk_appendf(out, "<td></td>");
656 }
657
658 chunk_appendf(out,
659 "<td class=ac>"
660 "<a name=\"%s/Frontend\"></a>"
661 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
662 "<td colspan=3></td>"
663 "",
664 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
665
666 chunk_appendf(out,
667 /* sessions rate : current */
668 "<td><u>%s<div class=tips><table class=det>"
669 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
670 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
671 "",
672 U2H(stats[ST_F_RATE].u.u32),
673 U2H(stats[ST_F_CONN_RATE].u.u32),
674 U2H(stats[ST_F_RATE].u.u32));
675
676 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
677 chunk_appendf(out,
678 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
679 U2H(stats[ST_F_REQ_RATE].u.u32));
680
681 chunk_appendf(out,
682 "</table></div></u></td>"
683 /* sessions rate : max */
684 "<td><u>%s<div class=tips><table class=det>"
685 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
686 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
687 "",
688 U2H(stats[ST_F_RATE_MAX].u.u32),
689 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
690 U2H(stats[ST_F_RATE_MAX].u.u32));
691
692 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
693 chunk_appendf(out,
694 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
695 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
696
697 chunk_appendf(out,
698 "</table></div></u></td>"
699 /* sessions rate : limit */
700 "<td>%s</td>",
701 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
702
703 chunk_appendf(out,
704 /* sessions: current, max, limit, total */
705 "<td>%s</td><td>%s</td><td>%s</td>"
706 "<td><u>%s<div class=tips><table class=det>"
707 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
708 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
709 "",
710 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
711 U2H(stats[ST_F_STOT].u.u64),
712 U2H(stats[ST_F_CONN_TOT].u.u64),
713 U2H(stats[ST_F_STOT].u.u64));
714
715 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
716 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
717 chunk_appendf(out,
718 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
719 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
720 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
721 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
722 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
723 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
724 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
725 "<tr><th>- other responses:</th><td>%s</td></tr>"
726 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100727 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
728 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200729 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100730 "",
731 U2H(stats[ST_F_REQ_TOT].u.u64),
732 U2H(stats[ST_F_HRSP_1XX].u.u64),
733 U2H(stats[ST_F_HRSP_2XX].u.u64),
734 U2H(stats[ST_F_COMP_RSP].u.u64),
735 stats[ST_F_HRSP_2XX].u.u64 ?
736 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
737 U2H(stats[ST_F_HRSP_3XX].u.u64),
738 U2H(stats[ST_F_HRSP_4XX].u.u64),
739 U2H(stats[ST_F_HRSP_5XX].u.u64),
740 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200741 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100742 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
743 U2H(stats[ST_F_CACHE_HITS].u.u64),
744 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
745 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200746 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100747 }
748
749 chunk_appendf(out,
750 "</table></div></u></td>"
751 /* sessions: lbtot, lastsess */
752 "<td></td><td></td>"
753 /* bytes : in */
754 "<td>%s</td>"
755 "",
756 U2H(stats[ST_F_BIN].u.u64));
757
758 chunk_appendf(out,
759 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
760 "<td>%s%s<div class=tips><table class=det>"
761 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
762 "<tr><th>Compression in:</th><td>%s</td></tr>"
763 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
764 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
765 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
766 "</table></div>%s</td>",
767 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
768 U2H(stats[ST_F_BOUT].u.u64),
769 U2H(stats[ST_F_BOUT].u.u64),
770 U2H(stats[ST_F_COMP_IN].u.u64),
771 U2H(stats[ST_F_COMP_OUT].u.u64),
772 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
773 U2H(stats[ST_F_COMP_BYP].u.u64),
774 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
775 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,
776 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
777
778 chunk_appendf(out,
779 /* denied: req, resp */
780 "<td>%s</td><td>%s</td>"
781 /* errors : request, connect, response */
782 "<td>%s</td><td></td><td></td>"
783 /* warnings: retries, redispatches */
784 "<td></td><td></td>"
785 /* server status : reflect frontend status */
786 "<td class=ac>%s</td>"
787 /* rest of server: nothing */
788 "<td class=ac colspan=8></td></tr>"
789 "",
790 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
791 U2H(stats[ST_F_EREQ].u.u64),
792 field_str(stats, ST_F_STATUS));
793 }
794 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
795 chunk_appendf(out, "<tr class=socket>");
796 if (flags & ST_SHOWADMIN) {
797 /* Column sub-heading for Enable or Disable server */
798 chunk_appendf(out, "<td></td>");
799 }
800
801 chunk_appendf(out,
802 /* frontend name, listener name */
803 "<td class=ac><a name=\"%s/+%s\"></a>%s"
804 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
805 "",
806 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
807 (flags & ST_SHLGNDS)?"<u>":"",
808 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
809
810 if (flags & ST_SHLGNDS) {
811 chunk_appendf(out, "<div class=tips>");
812
813 if (isdigit(*field_str(stats, ST_F_ADDR)))
814 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
815 else if (*field_str(stats, ST_F_ADDR) == '[')
816 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
817 else if (*field_str(stats, ST_F_ADDR))
818 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
819
820 /* id */
821 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
822 }
823
824 chunk_appendf(out,
825 /* queue */
826 "%s</td><td colspan=3></td>"
827 /* sessions rate: current, max, limit */
828 "<td colspan=3>&nbsp;</td>"
829 /* sessions: current, max, limit, total, lbtot, lastsess */
830 "<td>%s</td><td>%s</td><td>%s</td>"
831 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
832 /* bytes: in, out */
833 "<td>%s</td><td>%s</td>"
834 "",
835 (flags & ST_SHLGNDS)?"</u>":"",
836 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
837 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
838
839 chunk_appendf(out,
840 /* denied: req, resp */
841 "<td>%s</td><td>%s</td>"
842 /* errors: request, connect, response */
843 "<td>%s</td><td></td><td></td>"
844 /* warnings: retries, redispatches */
845 "<td></td><td></td>"
846 /* server status: reflect listener status */
847 "<td class=ac>%s</td>"
848 /* rest of server: nothing */
849 "<td class=ac colspan=8></td></tr>"
850 "",
851 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
852 U2H(stats[ST_F_EREQ].u.u64),
853 field_str(stats, ST_F_STATUS));
854 }
855 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
856 const char *style;
857
858 /* determine the style to use depending on the server's state,
859 * its health and weight. There isn't a 1-to-1 mapping between
860 * state and styles for the cases where the server is (still)
861 * up. The reason is that we don't want to report nolb and
862 * drain with the same color.
863 */
864
865 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
866 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
867 style = "down";
868 }
869 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
870 style = "going_up";
871 }
872 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
873 style = "going_down";
874 }
875 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
876 style = "nolb";
877 }
878 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
879 style = "no_check";
880 }
881 else if (!stats[ST_F_CHKFAIL].type ||
882 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
883 /* no check or max health = UP */
884 if (stats[ST_F_WEIGHT].u.u32)
885 style = "up";
886 else
887 style = "draining";
888 }
889 else {
890 style = "going_down";
891 }
892
893 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
894 chunk_appendf(out, "<tr class=\"maintain\">");
895 else
896 chunk_appendf(out,
897 "<tr class=\"%s_%s\">",
898 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
899
900
901 if (flags & ST_SHOWADMIN)
902 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000903 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
904 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100905 field_str(stats, ST_F_SVNAME));
906
907 chunk_appendf(out,
908 "<td class=ac><a name=\"%s/%s\"></a>%s"
909 "<a class=lfsb href=\"#%s/%s\">%s</a>"
910 "",
911 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
912 (flags & ST_SHLGNDS) ? "<u>" : "",
913 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
914
915 if (flags & ST_SHLGNDS) {
916 chunk_appendf(out, "<div class=tips>");
917
918 if (isdigit(*field_str(stats, ST_F_ADDR)))
919 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
920 else if (*field_str(stats, ST_F_ADDR) == '[')
921 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
922 else if (*field_str(stats, ST_F_ADDR))
923 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
924
925 /* id */
926 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
927
928 /* cookie */
929 if (stats[ST_F_COOKIE].type) {
930 chunk_appendf(out, ", cookie: '");
931 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
932 chunk_htmlencode(out, &src);
933 chunk_appendf(out, "'");
934 }
935
936 chunk_appendf(out, "</div>");
937 }
938
939 chunk_appendf(out,
940 /* queue : current, max, limit */
941 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
942 /* sessions rate : current, max, limit */
943 "<td>%s</td><td>%s</td><td></td>"
944 "",
945 (flags & ST_SHLGNDS) ? "</u>" : "",
946 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
947 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
948
949 chunk_appendf(out,
950 /* sessions: current, max, limit, total */
951 "<td>%s</td><td>%s</td><td>%s</td>"
952 "<td><u>%s<div class=tips><table class=det>"
953 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
954 "",
955 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
956 U2H(stats[ST_F_STOT].u.u64),
957 U2H(stats[ST_F_STOT].u.u64));
958
959 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
960 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
961 unsigned long long tot;
962
963 tot = stats[ST_F_HRSP_OTHER].u.u64;
964 tot += stats[ST_F_HRSP_1XX].u.u64;
965 tot += stats[ST_F_HRSP_2XX].u.u64;
966 tot += stats[ST_F_HRSP_3XX].u.u64;
967 tot += stats[ST_F_HRSP_4XX].u.u64;
968 tot += stats[ST_F_HRSP_5XX].u.u64;
969
970 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +0100971 "<tr><th>New connections:</th><td>%s</td></tr>"
972 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100973 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
974 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
975 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
976 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
977 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
978 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
979 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200980 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100981 "",
Willy Tarreauf1573842018-12-14 11:35:36 +0100982 U2H(stats[ST_F_CONNECT].u.u64),
983 U2H(stats[ST_F_REUSE].u.u64),
984 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
985 (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 +0100986 U2H(tot),
987 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
988 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
989 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
990 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
991 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 +0200992 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
993 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100994 }
995
996 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
997 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
998 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
999 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1000 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1001 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1002
1003 chunk_appendf(out,
1004 "</table></div></u></td>"
1005 /* sessions: lbtot, last */
1006 "<td>%s</td><td>%s</td>",
1007 U2H(stats[ST_F_LBTOT].u.u64),
1008 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1009
1010 chunk_appendf(out,
1011 /* bytes : in, out */
1012 "<td>%s</td><td>%s</td>"
1013 /* denied: req, resp */
1014 "<td></td><td>%s</td>"
1015 /* errors : request, connect */
1016 "<td></td><td>%s</td>"
1017 /* errors : response */
1018 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1019 /* warnings: retries, redispatches */
1020 "<td>%lld</td><td>%lld</td>"
1021 "",
1022 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1023 U2H(stats[ST_F_DRESP].u.u64),
1024 U2H(stats[ST_F_ECON].u.u64),
1025 U2H(stats[ST_F_ERESP].u.u64),
1026 (long long)stats[ST_F_CLI_ABRT].u.u64,
1027 (long long)stats[ST_F_SRV_ABRT].u.u64,
1028 (long long)stats[ST_F_WRETR].u.u64,
1029 (long long)stats[ST_F_WREDIS].u.u64);
1030
1031 /* status, last change */
1032 chunk_appendf(out, "<td class=ac>");
1033
1034 /* FIXME!!!!
1035 * LASTCHG should contain the last change for *this* server and must be computed
1036 * properly above, as was done below, ie: this server if maint, otherwise ref server
1037 * if tracking. Note that ref is either local or remote depending on tracking.
1038 */
1039
1040
1041 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1042 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1043 }
1044 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1045 chunk_strcat(out, "<i>no check</i>");
1046 }
1047 else {
1048 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1049 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1050 if (stats[ST_F_CHECK_HEALTH].u.u32)
1051 chunk_strcat(out, " &uarr;");
1052 }
1053 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1054 chunk_strcat(out, " &darr;");
1055 }
1056
1057 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1058 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1059 chunk_appendf(out,
1060 "</td><td class=ac><u> %s",
1061 field_str(stats, ST_F_AGENT_STATUS));
1062
1063 if (stats[ST_F_AGENT_CODE].type)
1064 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1065
1066 if (stats[ST_F_AGENT_DURATION].type)
1067 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1068
1069 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1070
1071 if (*field_str(stats, ST_F_LAST_AGT)) {
1072 chunk_appendf(out, ": ");
1073 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1074 chunk_htmlencode(out, &src);
1075 }
1076 chunk_appendf(out, "</div></u>");
1077 }
1078 else if (stats[ST_F_CHECK_STATUS].type) {
1079 chunk_appendf(out,
1080 "</td><td class=ac><u> %s",
1081 field_str(stats, ST_F_CHECK_STATUS));
1082
1083 if (stats[ST_F_CHECK_CODE].type)
1084 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1085
1086 if (stats[ST_F_CHECK_DURATION].type)
1087 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1088
1089 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1090
1091 if (*field_str(stats, ST_F_LAST_CHK)) {
1092 chunk_appendf(out, ": ");
1093 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1094 chunk_htmlencode(out, &src);
1095 }
1096 chunk_appendf(out, "</div></u>");
1097 }
1098 else
1099 chunk_appendf(out, "</td><td>");
1100
1101 chunk_appendf(out,
1102 /* weight */
1103 "</td><td class=ac>%d</td>"
1104 /* act, bck */
1105 "<td class=ac>%s</td><td class=ac>%s</td>"
1106 "",
1107 stats[ST_F_WEIGHT].u.u32,
1108 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1109 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1110
1111 /* check failures: unique, fatal, down time */
1112 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1113 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1114 }
1115 else if (stats[ST_F_CHKFAIL].type) {
1116 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1117
1118 if (stats[ST_F_HANAFAIL].type)
1119 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1120
1121 chunk_appendf(out,
1122 "<div class=tips>Failed Health Checks%s</div></u></td>"
1123 "<td>%lld</td><td>%s</td>"
1124 "",
1125 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1126 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1127 }
1128 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1129 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1130 chunk_appendf(out,
1131 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1132 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1133 }
1134 else
1135 chunk_appendf(out, "<td colspan=3></td>");
1136
1137 /* throttle */
1138 if (stats[ST_F_THROTTLE].type)
1139 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1140 else
1141 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1142 }
1143 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1144 chunk_appendf(out, "<tr class=\"backend\">");
1145 if (flags & ST_SHOWADMIN) {
1146 /* Column sub-heading for Enable or Disable server */
1147 chunk_appendf(out, "<td></td>");
1148 }
1149 chunk_appendf(out,
1150 "<td class=ac>"
1151 /* name */
1152 "%s<a name=\"%s/Backend\"></a>"
1153 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1154 "",
1155 (flags & ST_SHLGNDS)?"<u>":"",
1156 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1157
1158 if (flags & ST_SHLGNDS) {
1159 /* balancing */
1160 chunk_appendf(out, "<div class=tips>balancing: %s",
1161 field_str(stats, ST_F_ALGO));
1162
1163 /* cookie */
1164 if (stats[ST_F_COOKIE].type) {
1165 chunk_appendf(out, ", cookie: '");
1166 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1167 chunk_htmlencode(out, &src);
1168 chunk_appendf(out, "'");
1169 }
1170 chunk_appendf(out, "</div>");
1171 }
1172
1173 chunk_appendf(out,
1174 "%s</td>"
1175 /* queue : current, max */
1176 "<td>%s</td><td>%s</td><td></td>"
1177 /* sessions rate : current, max, limit */
1178 "<td>%s</td><td>%s</td><td></td>"
1179 "",
1180 (flags & ST_SHLGNDS)?"</u>":"",
1181 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1182 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1183
1184 chunk_appendf(out,
1185 /* sessions: current, max, limit, total */
1186 "<td>%s</td><td>%s</td><td>%s</td>"
1187 "<td><u>%s<div class=tips><table class=det>"
1188 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1189 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001190 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 +01001191 U2H(stats[ST_F_STOT].u.u64),
1192 U2H(stats[ST_F_STOT].u.u64));
1193
1194 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1195 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1196 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001197 "<tr><th>New connections:</th><td>%s</td></tr>"
1198 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001199 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1200 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1201 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1202 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1203 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1204 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1205 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1206 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001207 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1208 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001209 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001210 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1211 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001212 U2H(stats[ST_F_CONNECT].u.u64),
1213 U2H(stats[ST_F_REUSE].u.u64),
1214 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1215 (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 +01001216 U2H(stats[ST_F_REQ_TOT].u.u64),
1217 U2H(stats[ST_F_HRSP_1XX].u.u64),
1218 U2H(stats[ST_F_HRSP_2XX].u.u64),
1219 U2H(stats[ST_F_COMP_RSP].u.u64),
1220 stats[ST_F_HRSP_2XX].u.u64 ?
1221 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1222 U2H(stats[ST_F_HRSP_3XX].u.u64),
1223 U2H(stats[ST_F_HRSP_4XX].u.u64),
1224 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001225 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001226 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1227 U2H(stats[ST_F_CACHE_HITS].u.u64),
1228 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1229 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001230 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001231 }
1232
1233 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1234 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1235 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1236 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1237 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1238
1239 chunk_appendf(out,
1240 "</table></div></u></td>"
1241 /* sessions: lbtot, last */
1242 "<td>%s</td><td>%s</td>"
1243 /* bytes: in */
1244 "<td>%s</td>"
1245 "",
1246 U2H(stats[ST_F_LBTOT].u.u64),
1247 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1248 U2H(stats[ST_F_BIN].u.u64));
1249
1250 chunk_appendf(out,
1251 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1252 "<td>%s%s<div class=tips><table class=det>"
1253 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1254 "<tr><th>Compression in:</th><td>%s</td></tr>"
1255 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1256 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1257 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1258 "</table></div>%s</td>",
1259 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1260 U2H(stats[ST_F_BOUT].u.u64),
1261 U2H(stats[ST_F_BOUT].u.u64),
1262 U2H(stats[ST_F_COMP_IN].u.u64),
1263 U2H(stats[ST_F_COMP_OUT].u.u64),
1264 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1265 U2H(stats[ST_F_COMP_BYP].u.u64),
1266 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1267 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,
1268 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1269
1270 chunk_appendf(out,
1271 /* denied: req, resp */
1272 "<td>%s</td><td>%s</td>"
1273 /* errors : request, connect */
1274 "<td></td><td>%s</td>"
1275 /* errors : response */
1276 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1277 /* warnings: retries, redispatches */
1278 "<td>%lld</td><td>%lld</td>"
1279 /* backend status: reflect backend status (up/down): we display UP
1280 * if the backend has known working servers or if it has no server at
1281 * all (eg: for stats). Then we display the total weight, number of
1282 * active and backups. */
1283 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1284 "<td class=ac>%d</td><td class=ac>%d</td>"
1285 "",
1286 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1287 U2H(stats[ST_F_ECON].u.u64),
1288 U2H(stats[ST_F_ERESP].u.u64),
1289 (long long)stats[ST_F_CLI_ABRT].u.u64,
1290 (long long)stats[ST_F_SRV_ABRT].u.u64,
1291 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1292 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1293 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1294 stats[ST_F_WEIGHT].u.u32,
1295 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1296
1297 chunk_appendf(out,
1298 /* rest of backend: nothing, down transitions, total downtime, throttle */
1299 "<td class=ac>&nbsp;</td><td>%d</td>"
1300 "<td>%s</td>"
1301 "<td></td>"
1302 "</tr>",
1303 stats[ST_F_CHKDOWN].u.u32,
1304 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1305 }
1306 return 1;
1307}
1308
1309int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1310{
Simon Horman05ee2132017-01-04 09:37:25 +01001311 int ret;
1312
William Lallemand74c24fb2016-11-21 17:18:36 +01001313 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1314 flags |= ST_SHOWADMIN;
1315
1316 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001317 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001318 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001319 ret = stats_dump_fields_typed(&trash, stats);
1320 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1321 ret = stats_dump_fields_json(&trash, stats,
1322 !(appctx->ctx.stats.flags &
1323 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001324 else
Simon Horman05ee2132017-01-04 09:37:25 +01001325 ret = stats_dump_fields_csv(&trash, stats);
1326
1327 if (ret)
1328 appctx->ctx.stats.flags |= STAT_STARTED;
1329
1330 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001331}
1332
1333/* Fill <stats> with the frontend statistics. <stats> is
1334 * preallocated array of length <len>. The length of the array
1335 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1336 * this value, the function returns 0, otherwise, it returns 1.
1337 */
1338int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1339{
1340 if (len < ST_F_TOTAL_FIELDS)
1341 return 0;
1342
1343 memset(stats, 0, sizeof(*stats) * len);
1344
1345 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1346 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1347 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1348 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1349 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1350 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1351 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1352 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1353 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1354 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1355 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1356 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1357 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1358 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1359 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1360 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1361 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1362 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1363 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1364 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1365 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1366 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001367 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001368
1369 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1370 if (px->mode == PR_MODE_HTTP) {
1371 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1372 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1373 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1374 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1375 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1376 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1377 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001378 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1379 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001380 }
1381
1382 /* requests : req_rate, req_rate_max, req_tot, */
1383 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1384 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1385 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1386
1387 /* compression: in, out, bypassed, responses */
1388 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1389 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1390 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1391 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1392
1393 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1394 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1395 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1396 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1397
1398 return 1;
1399}
1400
1401/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1402 * the state from stream interface <si>. The caller is responsible for clearing
1403 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1404 */
1405static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1406{
1407 struct appctx *appctx = __objt_appctx(si->end);
1408
1409 if (!(px->cap & PR_CAP_FE))
1410 return 0;
1411
1412 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1413 return 0;
1414
1415 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1416 return 0;
1417
1418 return stats_dump_one_line(stats, 0, px, appctx);
1419}
1420
1421/* Fill <stats> with the listener statistics. <stats> is
1422 * preallocated array of length <len>. The length of the array
1423 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1424 * then this value, the function returns 0, otherwise, it
1425 * returns 1. <flags> can take the value ST_SHLGNDS.
1426 */
1427int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1428 struct field *stats, int len)
1429{
Willy Tarreau83061a82018-07-13 11:56:34 +02001430 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001431
1432 if (len < ST_F_TOTAL_FIELDS)
1433 return 0;
1434
1435 if (!l->counters)
1436 return 0;
1437
1438 chunk_reset(out);
1439 memset(stats, 0, sizeof(*stats) * len);
1440
1441 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1442 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1443 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1444 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1445 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1446 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1447 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1448 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1449 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1450 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1451 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1452 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1453 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1454 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1455 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
1456 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1457 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1458 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1459 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001460 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001461
1462 if (flags & ST_SHLGNDS) {
1463 char str[INET6_ADDRSTRLEN];
1464 int port;
1465
1466 port = get_host_port(&l->addr);
1467 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1468 case AF_INET:
1469 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1470 chunk_appendf(out, "%s:%d", str, port);
1471 break;
1472 case AF_INET6:
1473 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1474 chunk_appendf(out, "[%s]:%d", str, port);
1475 break;
1476 case AF_UNIX:
1477 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1478 break;
1479 case -1:
1480 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1481 chunk_strcat(out, strerror(errno));
1482 break;
1483 default: /* address family not supported */
1484 break;
1485 }
1486 }
1487
1488 return 1;
1489}
1490
1491/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1492 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1493 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1494 * otherwise.
1495 */
1496static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1497{
1498 struct appctx *appctx = __objt_appctx(si->end);
1499
1500 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1501 return 0;
1502
1503 return stats_dump_one_line(stats, flags, px, appctx);
1504}
1505
1506enum srv_stats_state {
1507 SRV_STATS_STATE_DOWN = 0,
1508 SRV_STATS_STATE_DOWN_AGENT,
1509 SRV_STATS_STATE_GOING_UP,
1510 SRV_STATS_STATE_UP_GOING_DOWN,
1511 SRV_STATS_STATE_UP,
1512 SRV_STATS_STATE_NOLB_GOING_DOWN,
1513 SRV_STATS_STATE_NOLB,
1514 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1515 SRV_STATS_STATE_DRAIN,
1516 SRV_STATS_STATE_DRAIN_AGENT,
1517 SRV_STATS_STATE_NO_CHECK,
1518
1519 SRV_STATS_STATE_COUNT, /* Must be last */
1520};
1521
1522static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1523 [SRV_STATS_STATE_DOWN] = "DOWN",
1524 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1525 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1526 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1527 [SRV_STATS_STATE_UP] = "UP",
1528 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1529 [SRV_STATS_STATE_NOLB] = "NOLB",
1530 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1531 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1532 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1533 [SRV_STATS_STATE_NO_CHECK] = "no check"
1534};
1535
1536/* Fill <stats> with the server statistics. <stats> is
1537 * preallocated array of length <len>. The length of the array
1538 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1539 * then this value, the function returns 0, otherwise, it
1540 * returns 1. <flags> can take the value ST_SHLGNDS.
1541 */
1542int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1543 struct field *stats, int len)
1544{
1545 struct server *via, *ref;
1546 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001547 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001548 enum srv_stats_state state;
1549 char *fld_status;
1550
1551 if (len < ST_F_TOTAL_FIELDS)
1552 return 0;
1553
1554 memset(stats, 0, sizeof(*stats) * len);
1555
1556 /* we have "via" which is the tracked server as described in the configuration,
1557 * and "ref" which is the checked server and the end of the chain.
1558 */
1559 via = sv->track ? sv->track : sv;
1560 ref = via;
1561 while (ref->track)
1562 ref = ref->track;
1563
Emeric Brun52a91d32017-08-31 14:41:55 +02001564 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001565 if ((ref->check.state & CHK_ST_ENABLED) &&
1566 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1567 state = SRV_STATS_STATE_UP_GOING_DOWN;
1568 } else {
1569 state = SRV_STATS_STATE_UP;
1570 }
1571
Emeric Brun52a91d32017-08-31 14:41:55 +02001572 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001573 if (ref->agent.state & CHK_ST_ENABLED)
1574 state = SRV_STATS_STATE_DRAIN_AGENT;
1575 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1576 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1577 else
1578 state = SRV_STATS_STATE_DRAIN;
1579 }
1580
1581 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1582 state = SRV_STATS_STATE_NO_CHECK;
1583 }
1584 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001585 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001586 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1587 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1588 state = SRV_STATS_STATE_NOLB;
1589 } else {
1590 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1591 }
1592 }
1593 else { /* stopped */
1594 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1595 state = SRV_STATS_STATE_DOWN_AGENT;
1596 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1597 state = SRV_STATS_STATE_DOWN; /* DOWN */
1598 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1599 state = SRV_STATS_STATE_GOING_UP;
1600 } else {
1601 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1602 }
1603 }
1604
1605 chunk_reset(out);
1606
1607 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1608 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1609 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1610 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1611 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1612 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1613 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1614
1615 if (sv->maxconn)
1616 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1617
1618 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1619 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1620 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1621 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1622 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1623 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1624 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1625 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001626 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001627 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1628 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001629
1630 /* status */
1631 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001632 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001633 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001634 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001635 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001636 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001637 chunk_appendf(out, "MAINT");
1638 else
1639 chunk_appendf(out,
1640 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001641 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1642 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001643
1644 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1645 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001646 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 +01001647 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1648 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1649
1650 /* check failures: unique, fatal; last change, total downtime */
1651 if (sv->check.state & CHK_ST_ENABLED) {
1652 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1653 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1654 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1655 }
1656
1657 if (sv->maxqueue)
1658 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1659
1660 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1661 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1662 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1663
Emeric Brun52a91d32017-08-31 14:41:55 +02001664 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001665 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1666
1667 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1668
1669 if (sv->track) {
1670 char *fld_track = chunk_newstr(out);
1671
1672 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1673 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1674 }
1675
1676 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1677 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1678 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1679
1680 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1681 const char *fld_chksts;
1682
1683 fld_chksts = chunk_newstr(out);
1684 chunk_strcat(out, "* "); // for check in progress
1685 chunk_strcat(out, get_check_status_info(sv->check.status));
1686 if (!(sv->check.state & CHK_ST_INPROGRESS))
1687 fld_chksts += 2; // skip "* "
1688 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1689
1690 if (sv->check.status >= HCHK_STATUS_L57DATA)
1691 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1692
1693 if (sv->check.status >= HCHK_STATUS_CHECKED)
1694 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1695
1696 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1697 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1698 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1699 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1700 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1701 }
1702
1703 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1704 const char *fld_chksts;
1705
1706 fld_chksts = chunk_newstr(out);
1707 chunk_strcat(out, "* "); // for check in progress
1708 chunk_strcat(out, get_check_status_info(sv->agent.status));
1709 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1710 fld_chksts += 2; // skip "* "
1711 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1712
1713 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1714 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1715
1716 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1717 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1718
1719 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1720 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1721 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1722 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1723 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1724 }
1725
1726 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1727 if (px->mode == PR_MODE_HTTP) {
1728 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1729 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1730 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1731 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1732 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1733 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1734 }
1735
1736 if (ref->observe)
1737 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1738
1739 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1740 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1741 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1742
1743 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1744 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1745 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1746 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1747
1748 if (flags & ST_SHLGNDS) {
1749 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1750 case AF_INET:
1751 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001752 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001753 break;
1754 case AF_INET6:
1755 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001756 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001757 break;
1758 case AF_UNIX:
1759 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1760 break;
1761 case -1:
1762 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1763 chunk_strcat(out, strerror(errno));
1764 break;
1765 default: /* address family not supported */
1766 break;
1767 }
1768
1769 if (sv->cookie)
1770 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1771 }
1772
1773 return 1;
1774}
1775
1776/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1777 * from stream interface <si>, stats flags <flags>, and server state <state>.
1778 * The caller is responsible for clearing the trash if needed. Returns non-zero
1779 * if it emits anything, zero otherwise.
1780 */
1781static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1782{
1783 struct appctx *appctx = __objt_appctx(si->end);
1784
1785 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1786 return 0;
1787
1788 return stats_dump_one_line(stats, flags, px, appctx);
1789}
1790
1791/* Fill <stats> with the backend statistics. <stats> is
1792 * preallocated array of length <len>. The length of the array
1793 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1794 * then this value, the function returns 0, otherwise, it
1795 * returns 1. <flags> can take the value ST_SHLGNDS.
1796 */
1797int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1798{
1799 if (len < ST_F_TOTAL_FIELDS)
1800 return 0;
1801
1802 memset(stats, 0, sizeof(*stats) * len);
1803
1804 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1805 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1806 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1807 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1808 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001809 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001810 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1811 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1812 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1813 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1814 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1815 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1816 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1817 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1818 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1819 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1820 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001821 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001822 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1823 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001824 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1825 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1826 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1827 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1828 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1829 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1830 if (px->srv)
1831 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1832
1833 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1834 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1835 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1836 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1837 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1838 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1839 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1840
1841 if (flags & ST_SHLGNDS) {
1842 if (px->cookie_name)
1843 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1844 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1845 }
1846
1847 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1848 if (px->mode == PR_MODE_HTTP) {
1849 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1850 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1851 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1852 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1853 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1854 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1855 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001856 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1857 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001858 }
1859
1860 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1861 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1862
1863 /* compression: in, out, bypassed, responses */
1864 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1865 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1866 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1867 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1868 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1869
1870 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1871 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1872 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1873 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1874
1875 return 1;
1876}
1877
1878/* Dumps a line for backend <px> to the trash for and uses the state from stream
1879 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1880 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1881 */
1882static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1883{
1884 struct appctx *appctx = __objt_appctx(si->end);
1885
1886 if (!(px->cap & PR_CAP_BE))
1887 return 0;
1888
1889 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1890 return 0;
1891
1892 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1893 return 0;
1894
1895 return stats_dump_one_line(stats, flags, px, appctx);
1896}
1897
1898/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1899 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1900 * for clearing the trash if needed.
1901 */
1902static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1903{
1904 struct appctx *appctx = __objt_appctx(si->end);
1905 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1906
1907 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1908 /* A form to enable/disable this proxy servers */
1909
1910 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1911 scope_txt[0] = 0;
1912 if (appctx->ctx.stats.scope_len) {
1913 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02001914 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 +01001915 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1916 }
1917
1918 chunk_appendf(&trash,
1919 "<form method=\"post\">");
1920 }
1921
1922 /* print a new table */
1923 chunk_appendf(&trash,
1924 "<table class=\"tbl\" width=\"100%%\">\n"
1925 "<tr class=\"titre\">"
1926 "<th class=\"pxname\" width=\"10%%\">");
1927
1928 chunk_appendf(&trash,
1929 "<a name=\"%s\"></a>%s"
1930 "<a class=px href=\"#%s\">%s</a>",
1931 px->id,
1932 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1933 px->id, px->id);
1934
1935 if (uri->flags & ST_SHLGNDS) {
1936 /* cap, mode, id */
1937 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1938 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1939 px->uuid);
1940 chunk_appendf(&trash, "</div>");
1941 }
1942
1943 chunk_appendf(&trash,
1944 "%s</th>"
1945 "<th class=\"%s\" width=\"90%%\">%s</th>"
1946 "</tr>\n"
1947 "</table>\n"
1948 "<table class=\"tbl\" width=\"100%%\">\n"
1949 "<tr class=\"titre\">",
1950 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1951 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1952
1953 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1954 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00001955 chunk_appendf(&trash,
1956 "<th rowspan=2 width=1><input type=\"checkbox\" \
1957 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
1958 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
1959 px->id,
1960 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01001961 }
1962
1963 chunk_appendf(&trash,
1964 "<th rowspan=2></th>"
1965 "<th colspan=3>Queue</th>"
1966 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
1967 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
1968 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
1969 "<th colspan=9>Server</th>"
1970 "</tr>\n"
1971 "<tr class=\"titre\">"
1972 "<th>Cur</th><th>Max</th><th>Limit</th>"
1973 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
1974 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
1975 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
1976 "<th>Resp</th><th>Retr</th><th>Redis</th>"
1977 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
1978 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1979 "<th>Thrtle</th>\n"
1980 "</tr>");
1981}
1982
1983/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
1984 * stream interface <si>. The caller is responsible for clearing the trash if needed.
1985 */
1986static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
1987{
1988 struct appctx *appctx = __objt_appctx(si->end);
1989 chunk_appendf(&trash, "</table>");
1990
1991 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1992 /* close the form used to enable/disable this proxy servers */
1993 chunk_appendf(&trash,
1994 "Choose the action to perform on the checked servers : "
1995 "<select name=action>"
1996 "<option value=\"\"></option>"
1997 "<option value=\"ready\">Set state to READY</option>"
1998 "<option value=\"drain\">Set state to DRAIN</option>"
1999 "<option value=\"maint\">Set state to MAINT</option>"
2000 "<option value=\"dhlth\">Health: disable checks</option>"
2001 "<option value=\"ehlth\">Health: enable checks</option>"
2002 "<option value=\"hrunn\">Health: force UP</option>"
2003 "<option value=\"hnolb\">Health: force NOLB</option>"
2004 "<option value=\"hdown\">Health: force DOWN</option>"
2005 "<option value=\"dagent\">Agent: disable checks</option>"
2006 "<option value=\"eagent\">Agent: enable checks</option>"
2007 "<option value=\"arunn\">Agent: force UP</option>"
2008 "<option value=\"adown\">Agent: force DOWN</option>"
2009 "<option value=\"shutdown\">Kill Sessions</option>"
2010 "</select>"
2011 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2012 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2013 "</form>",
2014 px->uuid);
2015 }
2016
2017 chunk_appendf(&trash, "<p>\n");
2018}
2019
2020/*
2021 * Dumps statistics for a proxy. The output is sent to the stream interface's
2022 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2023 * buffer space, or non-zero if everything completed. This function is used
2024 * both by the CLI and the HTTP entry points, and is able to dump the output
2025 * in HTML or CSV formats. If the later, <uri> must be NULL.
2026 */
Christopher Fauletef779222018-10-31 08:47:01 +01002027int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2028 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002029{
2030 struct appctx *appctx = __objt_appctx(si->end);
2031 struct stream *s = si_strm(si);
2032 struct channel *rep = si_ic(si);
2033 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2034 struct listener *l;
2035 unsigned int flags;
2036
2037 if (uri)
2038 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002039 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002040 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2041 else
2042 flags = ST_SHNODE | ST_SHDESC;
2043
2044 chunk_reset(&trash);
2045
2046 switch (appctx->ctx.stats.px_st) {
2047 case STAT_PX_ST_INIT:
2048 /* we are on a new proxy */
2049 if (uri && uri->scope) {
2050 /* we have a limited scope, we have to check the proxy name */
2051 struct stat_scope *scope;
2052 int len;
2053
2054 len = strlen(px->id);
2055 scope = uri->scope;
2056
2057 while (scope) {
2058 /* match exact proxy name */
2059 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2060 break;
2061
2062 /* match '.' which means 'self' proxy */
2063 if (!strcmp(scope->px_id, ".") && px == s->be)
2064 break;
2065 scope = scope->next;
2066 }
2067
2068 /* proxy name not found : don't dump anything */
2069 if (scope == NULL)
2070 return 1;
2071 }
2072
2073 /* if the user has requested a limited output and the proxy
2074 * name does not match, skip it.
2075 */
2076 if (appctx->ctx.stats.scope_len &&
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002077 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 +01002078 return 1;
2079
2080 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2081 (appctx->ctx.stats.iid != -1) &&
2082 (px->uuid != appctx->ctx.stats.iid))
2083 return 1;
2084
2085 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2086 /* fall through */
2087
2088 case STAT_PX_ST_TH:
2089 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2090 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002091 if (!stats_putchk(rep, htx, &trash))
2092 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002093 }
2094
2095 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2096 /* fall through */
2097
2098 case STAT_PX_ST_FE:
2099 /* print the frontend */
2100 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002101 if (!stats_putchk(rep, htx, &trash))
2102 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002103 }
2104
2105 appctx->ctx.stats.l = px->conf.listeners.n;
2106 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2107 /* fall through */
2108
2109 case STAT_PX_ST_LI:
2110 /* stats.l has been initialized above */
2111 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002112 if (htx) {
2113 if (htx_almost_full(htx))
2114 goto full;
2115 }
2116 else {
2117 if (buffer_almost_full(&rep->buf))
2118 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002119 }
2120
2121 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2122 if (!l->counters)
2123 continue;
2124
2125 if (appctx->ctx.stats.flags & STAT_BOUND) {
2126 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2127 break;
2128
2129 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2130 continue;
2131 }
2132
2133 /* print the frontend */
2134 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002135 if (!stats_putchk(rep, htx, &trash))
2136 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002137 }
2138 }
2139
2140 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2141 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2142 /* fall through */
2143
2144 case STAT_PX_ST_SV:
2145 /* stats.sv has been initialized above */
2146 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002147 if (htx) {
2148 if (htx_almost_full(htx))
2149 goto full;
2150 }
2151 else {
2152 if (buffer_almost_full(&rep->buf))
2153 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002154 }
2155
2156 sv = appctx->ctx.stats.sv;
2157
2158 if (appctx->ctx.stats.flags & STAT_BOUND) {
2159 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2160 break;
2161
2162 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2163 continue;
2164 }
2165
2166 svs = sv;
2167 while (svs->track)
2168 svs = svs->track;
2169
2170 /* do not report servers which are DOWN and not changing state */
2171 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002172 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2173 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002174 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2175 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2176 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2177 continue;
2178 }
2179
2180 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002181 if (!stats_putchk(rep, htx, &trash))
2182 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002183 }
2184 } /* for sv */
2185
2186 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2187 /* fall through */
2188
2189 case STAT_PX_ST_BE:
2190 /* print the backend */
2191 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002192 if (!stats_putchk(rep, htx, &trash))
2193 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002194 }
2195
2196 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2197 /* fall through */
2198
2199 case STAT_PX_ST_END:
2200 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2201 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002202 if (!stats_putchk(rep, htx, &trash))
2203 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002204 }
2205
2206 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2207 /* fall through */
2208
2209 case STAT_PX_ST_FIN:
2210 return 1;
2211
2212 default:
2213 /* unknown state, we should put an abort() here ! */
2214 return 1;
2215 }
Christopher Fauletef779222018-10-31 08:47:01 +01002216
2217 full:
2218 si_rx_room_blk(si);
2219 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002220}
2221
2222/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2223 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2224 */
2225static void stats_dump_html_head(struct uri_auth *uri)
2226{
2227 /* WARNING! This must fit in the first buffer !!! */
2228 chunk_appendf(&trash,
2229 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2230 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2231 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2232 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2233 "<style type=\"text/css\"><!--\n"
2234 "body {"
2235 " font-family: arial, helvetica, sans-serif;"
2236 " font-size: 12px;"
2237 " font-weight: normal;"
2238 " color: black;"
2239 " background: white;"
2240 "}\n"
2241 "th,td {"
2242 " font-size: 10px;"
2243 "}\n"
2244 "h1 {"
2245 " font-size: x-large;"
2246 " margin-bottom: 0.5em;"
2247 "}\n"
2248 "h2 {"
2249 " font-family: helvetica, arial;"
2250 " font-size: x-large;"
2251 " font-weight: bold;"
2252 " font-style: italic;"
2253 " color: #6020a0;"
2254 " margin-top: 0em;"
2255 " margin-bottom: 0em;"
2256 "}\n"
2257 "h3 {"
2258 " font-family: helvetica, arial;"
2259 " font-size: 16px;"
2260 " font-weight: bold;"
2261 " color: #b00040;"
2262 " background: #e8e8d0;"
2263 " margin-top: 0em;"
2264 " margin-bottom: 0em;"
2265 "}\n"
2266 "li {"
2267 " margin-top: 0.25em;"
2268 " margin-right: 2em;"
2269 "}\n"
2270 ".hr {margin-top: 0.25em;"
2271 " border-color: black;"
2272 " border-bottom-style: solid;"
2273 "}\n"
2274 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2275 ".total {background: #20D0D0;color: #ffff80;}\n"
2276 ".frontend {background: #e8e8d0;}\n"
2277 ".socket {background: #d0d0d0;}\n"
2278 ".backend {background: #e8e8d0;}\n"
2279 ".active_down {background: #ff9090;}\n"
2280 ".active_going_up {background: #ffd020;}\n"
2281 ".active_going_down {background: #ffffa0;}\n"
2282 ".active_up {background: #c0ffc0;}\n"
2283 ".active_nolb {background: #20a0ff;}\n"
2284 ".active_draining {background: #20a0FF;}\n"
2285 ".active_no_check {background: #e0e0e0;}\n"
2286 ".backup_down {background: #ff9090;}\n"
2287 ".backup_going_up {background: #ff80ff;}\n"
2288 ".backup_going_down {background: #c060ff;}\n"
2289 ".backup_up {background: #b0d0ff;}\n"
2290 ".backup_nolb {background: #90b0e0;}\n"
2291 ".backup_draining {background: #cc9900;}\n"
2292 ".backup_no_check {background: #e0e0e0;}\n"
2293 ".maintain {background: #c07820;}\n"
2294 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2295 "\n"
2296 "a.px:link {color: #ffff40; text-decoration: none;}"
2297 "a.px:visited {color: #ffff40; text-decoration: none;}"
2298 "a.px:hover {color: #ffffff; text-decoration: none;}"
2299 "a.lfsb:link {color: #000000; text-decoration: none;}"
2300 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2301 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2302 "\n"
2303 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2304 "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"
2305 "table.tbl td.ac { text-align: center;}\n"
2306 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2307 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2308 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2309 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2310 "\n"
2311 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2312 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2313 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2314 "table.det { border-collapse: collapse; border-style: none; }\n"
2315 "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"
2316 "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"
2317 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2318 "div.tips {\n"
2319 " display:block;\n"
2320 " visibility:hidden;\n"
2321 " z-index:2147483647;\n"
2322 " position:absolute;\n"
2323 " padding:2px 4px 3px;\n"
2324 " background:#f0f060; color:#000000;\n"
2325 " border:1px solid #7040c0;\n"
2326 " white-space:nowrap;\n"
2327 " font-style:normal;font-size:11px;font-weight:normal;\n"
2328 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2329 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2330 "}\n"
2331 "u:hover div.tips {visibility:visible;}\n"
2332 "-->\n"
2333 "</style></head>\n",
2334 (uri->flags & ST_SHNODE) ? " on " : "",
2335 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2336 );
2337}
2338
2339/* Dumps the HTML stats information block to the trash for and uses the state from
2340 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2341 * for clearing the trash if needed.
2342 */
2343static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2344{
2345 struct appctx *appctx = __objt_appctx(si->end);
2346 unsigned int up = (now.tv_sec - start_date.tv_sec);
2347 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2348
2349 /* WARNING! this has to fit the first packet too.
2350 * We are around 3.5 kB, add adding entries will
2351 * become tricky if we want to support 4kB buffers !
2352 */
2353 chunk_appendf(&trash,
2354 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2355 PRODUCT_NAME "%s</a></h1>\n"
2356 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2357 "<hr width=\"100%%\" class=\"hr\">\n"
2358 "<h3>&gt; General process information</h3>\n"
2359 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002360 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002361 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2362 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2363 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
2364 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
2365 "Running tasks: %d/%d; idle = %d %%<br>\n"
2366 "</td><td align=\"center\" nowrap>\n"
2367 "<table class=\"lgd\"><tr>\n"
2368 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2369 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2370 "</tr><tr>\n"
2371 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2372 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2373 "</tr><tr>\n"
2374 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2375 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2376 "</tr><tr>\n"
2377 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2378 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2379 "</tr><tr>\n"
2380 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2381 "</tr><tr>\n"
2382 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2383 "</tr></table>\n"
2384 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2385 "</td>"
2386 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2387 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2388 "",
Willy Tarreau909b9d82019-01-04 18:20:32 +01002389 (uri->flags & ST_HIDEVER) ? "" : (stats_version_string),
William Lallemand74c24fb2016-11-21 17:18:36 +01002390 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2391 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2392 (uri->flags & ST_SHDESC) ? ": " : "",
2393 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002394 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002395 up / 86400, (up % 86400) / 3600,
2396 (up % 3600) / 60, (up % 60),
2397 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2398 global.rlimit_memmax ? " MB" : "",
2399 global.rlimit_nofile,
2400 global.maxsock, global.maxconn, global.maxpipes,
2401 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002402 tasks_run_queue_cur, nb_tasks_cur, idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002403 );
2404
2405 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002406 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 +01002407 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2408
2409 chunk_appendf(&trash,
2410 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2411 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2412 STAT_SCOPE_TXT_MAXLEN);
2413
2414 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2415 scope_txt[0] = 0;
2416 if (appctx->ctx.stats.scope_len) {
2417 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002418 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 +01002419 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2420 }
2421
2422 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2423 chunk_appendf(&trash,
2424 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2425 uri->uri_prefix,
2426 "",
2427 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2428 scope_txt);
2429 else
2430 chunk_appendf(&trash,
2431 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2432 uri->uri_prefix,
2433 ";up",
2434 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2435 scope_txt);
2436
2437 if (uri->refresh > 0) {
2438 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2439 chunk_appendf(&trash,
2440 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2441 uri->uri_prefix,
2442 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2443 "",
2444 scope_txt);
2445 else
2446 chunk_appendf(&trash,
2447 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2448 uri->uri_prefix,
2449 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2450 ";norefresh",
2451 scope_txt);
2452 }
2453
2454 chunk_appendf(&trash,
2455 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2456 uri->uri_prefix,
2457 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2458 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2459 scope_txt);
2460
2461 chunk_appendf(&trash,
2462 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2463 uri->uri_prefix,
2464 (uri->refresh > 0) ? ";norefresh" : "",
2465 scope_txt);
2466
2467 chunk_appendf(&trash,
2468 "</ul></td>"
2469 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2470 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2471 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2472 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2473 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2474 "</ul>"
2475 "</td>"
2476 "</tr></table>\n"
2477 ""
2478 );
2479
2480 if (appctx->ctx.stats.st_code) {
2481 switch (appctx->ctx.stats.st_code) {
2482 case STAT_STATUS_DONE:
2483 chunk_appendf(&trash,
2484 "<p><div class=active_up>"
2485 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2486 "Action processed successfully."
2487 "</div>\n", uri->uri_prefix,
2488 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2489 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2490 scope_txt);
2491 break;
2492 case STAT_STATUS_NONE:
2493 chunk_appendf(&trash,
2494 "<p><div class=active_going_down>"
2495 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2496 "Nothing has changed."
2497 "</div>\n", uri->uri_prefix,
2498 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2499 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2500 scope_txt);
2501 break;
2502 case STAT_STATUS_PART:
2503 chunk_appendf(&trash,
2504 "<p><div class=active_going_down>"
2505 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2506 "Action partially processed.<br>"
2507 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2508 "</div>\n", uri->uri_prefix,
2509 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2510 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2511 scope_txt);
2512 break;
2513 case STAT_STATUS_ERRP:
2514 chunk_appendf(&trash,
2515 "<p><div class=active_down>"
2516 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2517 "Action not processed because of invalid parameters."
2518 "<ul>"
2519 "<li>The action is maybe unknown.</li>"
2520 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2521 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2522 "</ul>"
2523 "</div>\n", uri->uri_prefix,
2524 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2525 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2526 scope_txt);
2527 break;
2528 case STAT_STATUS_EXCD:
2529 chunk_appendf(&trash,
2530 "<p><div class=active_down>"
2531 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2532 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2533 "You should retry with less servers at a time.</b>"
2534 "</div>\n", uri->uri_prefix,
2535 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2536 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2537 scope_txt);
2538 break;
2539 case STAT_STATUS_DENY:
2540 chunk_appendf(&trash,
2541 "<p><div class=active_down>"
2542 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2543 "<b>Action denied.</b>"
2544 "</div>\n", uri->uri_prefix,
2545 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2546 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2547 scope_txt);
2548 break;
2549 default:
2550 chunk_appendf(&trash,
2551 "<p><div class=active_no_check>"
2552 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2553 "Unexpected result."
2554 "</div>\n", uri->uri_prefix,
2555 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2556 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2557 scope_txt);
2558 }
2559 chunk_appendf(&trash, "<p>\n");
2560 }
2561}
2562
2563/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2564 * for clearing the trash if needed.
2565 */
2566static void stats_dump_html_end()
2567{
2568 chunk_appendf(&trash, "</body></html>\n");
2569}
2570
Simon Horman05ee2132017-01-04 09:37:25 +01002571/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2572 * for clearing it if needed.
2573 */
2574static void stats_dump_json_header()
2575{
2576 chunk_strcat(&trash, "[");
2577}
2578
2579
2580/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2581 * for clearing the trash if needed.
2582 */
2583static void stats_dump_json_end()
2584{
2585 chunk_strcat(&trash, "]");
2586}
2587
William Lallemand74c24fb2016-11-21 17:18:36 +01002588/* This function dumps statistics onto the stream interface's read buffer in
2589 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2590 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2591 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2592 * and the stream must be closed, or -1 in case of any error. This function is
2593 * used by both the CLI and the HTTP handlers.
2594 */
Christopher Fauletef779222018-10-31 08:47:01 +01002595static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2596 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002597{
2598 struct appctx *appctx = __objt_appctx(si->end);
2599 struct channel *rep = si_ic(si);
2600 struct proxy *px;
2601
2602 chunk_reset(&trash);
2603
2604 switch (appctx->st2) {
2605 case STAT_ST_INIT:
2606 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2607 /* fall through */
2608
2609 case STAT_ST_HEAD:
2610 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2611 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002612 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002613 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002614 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2615 stats_dump_csv_header();
2616
Christopher Fauletef779222018-10-31 08:47:01 +01002617 if (!stats_putchk(rep, htx, &trash))
2618 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002619
2620 appctx->st2 = STAT_ST_INFO;
2621 /* fall through */
2622
2623 case STAT_ST_INFO:
2624 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2625 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002626 if (!stats_putchk(rep, htx, &trash))
2627 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002628 }
2629
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002630 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002631 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2632 appctx->st2 = STAT_ST_LIST;
2633 /* fall through */
2634
2635 case STAT_ST_LIST:
2636 /* dump proxies */
2637 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002638 if (htx) {
2639 if (htx_almost_full(htx))
2640 goto full;
2641 }
2642 else {
2643 if (buffer_almost_full(&rep->buf))
2644 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002645 }
2646
2647 px = appctx->ctx.stats.px;
2648 /* skip the disabled proxies, global frontend and non-networked ones */
2649 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002650 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002651 return 0;
2652
2653 appctx->ctx.stats.px = px->next;
2654 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2655 }
2656 /* here, we just have reached the last proxy */
2657
2658 appctx->st2 = STAT_ST_END;
2659 /* fall through */
2660
2661 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002662 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2663 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2664 stats_dump_html_end();
2665 else
2666 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002667 if (!stats_putchk(rep, htx, &trash))
2668 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002669 }
2670
2671 appctx->st2 = STAT_ST_FIN;
2672 /* fall through */
2673
2674 case STAT_ST_FIN:
2675 return 1;
2676
2677 default:
2678 /* unknown state ! */
2679 appctx->st2 = STAT_ST_FIN;
2680 return -1;
2681 }
Christopher Fauletef779222018-10-31 08:47:01 +01002682
2683 full:
2684 si_rx_room_blk(si);
2685 return 0;
2686
William Lallemand74c24fb2016-11-21 17:18:36 +01002687}
2688
2689/* We reached the stats page through a POST request. The appctx is
2690 * expected to have already been allocated by the caller.
2691 * Parse the posted data and enable/disable servers if necessary.
2692 * Returns 1 if request was parsed or zero if it needs more data.
2693 */
2694static int stats_process_http_post(struct stream_interface *si)
2695{
2696 struct stream *s = si_strm(si);
2697 struct appctx *appctx = objt_appctx(si->end);
2698
2699 struct proxy *px = NULL;
2700 struct server *sv = NULL;
2701
2702 char key[LINESIZE];
2703 int action = ST_ADM_ACTION_NONE;
2704 int reprocess = 0;
2705
2706 int total_servers = 0;
2707 int altered_servers = 0;
2708
2709 char *first_param, *cur_param, *next_param, *end_params;
2710 char *st_cur_param = NULL;
2711 char *st_next_param = NULL;
2712
Christopher Fauleta3618372018-12-13 21:59:56 +01002713 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002714
Christopher Fauleta3618372018-12-13 21:59:56 +01002715 if (IS_HTX_STRM(s)) {
2716 struct htx *htx = htxbuf(&s->req.buf);
2717 struct htx_blk *blk;
2718 size_t count = co_data(&s->req);
2719
2720 /* Remove the headers */
2721 blk = htx_get_head_blk(htx);
2722 while (count && blk) {
2723 enum htx_blk_type type = htx_get_blk_type(blk);
2724 uint32_t sz = htx_get_blksz(blk);
2725
2726 if (sz > count) {
2727 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2728 return 0;
2729 }
2730
2731 count -= sz;
2732 co_set_data(&s->req, co_data(&s->req) - sz);
2733 blk = htx_remove_blk(htx, blk);
2734
2735 if (type == HTX_BLK_EOH)
2736 break;
2737 }
2738
William Lallemand74c24fb2016-11-21 17:18:36 +01002739 /* too large request */
Christopher Fauleta3618372018-12-13 21:59:56 +01002740 if (htx->data + htx->extra > b_size(temp)) {
2741 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2742 goto out;
2743 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002744
William Lallemand74c24fb2016-11-21 17:18:36 +01002745 /* we need more data */
Christopher Fauleta3618372018-12-13 21:59:56 +01002746 if (htx->extra || htx->data > count) {
2747 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2748 return 0;
2749 }
2750
2751 while (count && blk) {
2752 enum htx_blk_type type = htx_get_blk_type(blk);
2753 uint32_t sz = htx_get_blksz(blk);
2754
2755 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2756 break;
2757 if (type == HTX_BLK_DATA) {
2758 struct ist v = htx_get_blk_value(htx, blk);
2759
2760 if (!chunk_memcat(temp, v.ptr, v.len)) {
2761 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2762 goto out;
2763 }
2764 }
2765
2766 count -= sz;
2767 co_set_data(&s->req, co_data(&s->req) - sz);
2768 blk = htx_remove_blk(htx, blk);
2769 }
2770 }
2771 else {
2772 int reql;
2773
2774 if (temp->size < s->txn->req.body_len) {
2775 /* too large request */
2776 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2777 goto out;
2778 }
2779
2780 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2781 s->txn->req.eoh + 2);
2782 if (reql <= 0) {
2783 /* we need more data */
2784 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2785 return 0;
2786 }
2787 temp->data = reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002788 }
2789
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002790 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002791 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002792 cur_param = next_param = end_params;
2793 *end_params = '\0';
2794
2795 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2796
2797 /*
2798 * Parse the parameters in reverse order to only store the last value.
2799 * From the html form, the backend and the action are at the end.
2800 */
2801 while (cur_param > first_param) {
2802 char *value;
2803 int poffset, plen;
2804
2805 cur_param--;
2806
2807 if ((*cur_param == '&') || (cur_param == first_param)) {
2808 reprocess_servers:
2809 /* Parse the key */
2810 poffset = (cur_param != first_param ? 1 : 0);
2811 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2812 if ((plen > 0) && (plen <= sizeof(key))) {
2813 strncpy(key, cur_param + poffset, plen);
2814 key[plen - 1] = '\0';
2815 } else {
2816 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2817 goto out;
2818 }
2819
2820 /* Parse the value */
2821 value = key;
2822 while (*value != '\0' && *value != '=') {
2823 value++;
2824 }
2825 if (*value == '=') {
2826 /* Ok, a value is found, we can mark the end of the key */
2827 *value++ = '\0';
2828 }
2829 if (url_decode(key) < 0 || url_decode(value) < 0)
2830 break;
2831
2832 /* Now we can check the key to see what to do */
2833 if (!px && (strcmp(key, "b") == 0)) {
2834 if ((px = proxy_be_by_name(value)) == NULL) {
2835 /* the backend name is unknown or ambiguous (duplicate names) */
2836 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2837 goto out;
2838 }
2839 }
2840 else if (!action && (strcmp(key, "action") == 0)) {
2841 if (strcmp(value, "ready") == 0) {
2842 action = ST_ADM_ACTION_READY;
2843 }
2844 else if (strcmp(value, "drain") == 0) {
2845 action = ST_ADM_ACTION_DRAIN;
2846 }
2847 else if (strcmp(value, "maint") == 0) {
2848 action = ST_ADM_ACTION_MAINT;
2849 }
2850 else if (strcmp(value, "shutdown") == 0) {
2851 action = ST_ADM_ACTION_SHUTDOWN;
2852 }
2853 else if (strcmp(value, "dhlth") == 0) {
2854 action = ST_ADM_ACTION_DHLTH;
2855 }
2856 else if (strcmp(value, "ehlth") == 0) {
2857 action = ST_ADM_ACTION_EHLTH;
2858 }
2859 else if (strcmp(value, "hrunn") == 0) {
2860 action = ST_ADM_ACTION_HRUNN;
2861 }
2862 else if (strcmp(value, "hnolb") == 0) {
2863 action = ST_ADM_ACTION_HNOLB;
2864 }
2865 else if (strcmp(value, "hdown") == 0) {
2866 action = ST_ADM_ACTION_HDOWN;
2867 }
2868 else if (strcmp(value, "dagent") == 0) {
2869 action = ST_ADM_ACTION_DAGENT;
2870 }
2871 else if (strcmp(value, "eagent") == 0) {
2872 action = ST_ADM_ACTION_EAGENT;
2873 }
2874 else if (strcmp(value, "arunn") == 0) {
2875 action = ST_ADM_ACTION_ARUNN;
2876 }
2877 else if (strcmp(value, "adown") == 0) {
2878 action = ST_ADM_ACTION_ADOWN;
2879 }
2880 /* else these are the old supported methods */
2881 else if (strcmp(value, "disable") == 0) {
2882 action = ST_ADM_ACTION_DISABLE;
2883 }
2884 else if (strcmp(value, "enable") == 0) {
2885 action = ST_ADM_ACTION_ENABLE;
2886 }
2887 else if (strcmp(value, "stop") == 0) {
2888 action = ST_ADM_ACTION_STOP;
2889 }
2890 else if (strcmp(value, "start") == 0) {
2891 action = ST_ADM_ACTION_START;
2892 }
2893 else {
2894 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2895 goto out;
2896 }
2897 }
2898 else if (strcmp(key, "s") == 0) {
2899 if (!(px && action)) {
2900 /*
2901 * Indicates that we'll need to reprocess the parameters
2902 * as soon as backend and action are known
2903 */
2904 if (!reprocess) {
2905 st_cur_param = cur_param;
2906 st_next_param = next_param;
2907 }
2908 reprocess = 1;
2909 }
2910 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002911 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002912 switch (action) {
2913 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002914 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002915 altered_servers++;
2916 total_servers++;
2917 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2918 }
2919 break;
2920 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002921 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002922 altered_servers++;
2923 total_servers++;
2924 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2925 }
2926 break;
2927 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002928 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002929 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2930 altered_servers++;
2931 total_servers++;
2932 }
2933 break;
2934 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002935 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002936 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2937 altered_servers++;
2938 total_servers++;
2939 }
2940 break;
2941 case ST_ADM_ACTION_DHLTH:
2942 if (sv->check.state & CHK_ST_CONFIGURED) {
2943 sv->check.state &= ~CHK_ST_ENABLED;
2944 altered_servers++;
2945 total_servers++;
2946 }
2947 break;
2948 case ST_ADM_ACTION_EHLTH:
2949 if (sv->check.state & CHK_ST_CONFIGURED) {
2950 sv->check.state |= CHK_ST_ENABLED;
2951 altered_servers++;
2952 total_servers++;
2953 }
2954 break;
2955 case ST_ADM_ACTION_HRUNN:
2956 if (!(sv->track)) {
2957 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002958 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002959 altered_servers++;
2960 total_servers++;
2961 }
2962 break;
2963 case ST_ADM_ACTION_HNOLB:
2964 if (!(sv->track)) {
2965 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002966 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002967 altered_servers++;
2968 total_servers++;
2969 }
2970 break;
2971 case ST_ADM_ACTION_HDOWN:
2972 if (!(sv->track)) {
2973 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002974 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002975 altered_servers++;
2976 total_servers++;
2977 }
2978 break;
2979 case ST_ADM_ACTION_DAGENT:
2980 if (sv->agent.state & CHK_ST_CONFIGURED) {
2981 sv->agent.state &= ~CHK_ST_ENABLED;
2982 altered_servers++;
2983 total_servers++;
2984 }
2985 break;
2986 case ST_ADM_ACTION_EAGENT:
2987 if (sv->agent.state & CHK_ST_CONFIGURED) {
2988 sv->agent.state |= CHK_ST_ENABLED;
2989 altered_servers++;
2990 total_servers++;
2991 }
2992 break;
2993 case ST_ADM_ACTION_ARUNN:
2994 if (sv->agent.state & CHK_ST_ENABLED) {
2995 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002996 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002997 altered_servers++;
2998 total_servers++;
2999 }
3000 break;
3001 case ST_ADM_ACTION_ADOWN:
3002 if (sv->agent.state & CHK_ST_ENABLED) {
3003 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003004 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003005 altered_servers++;
3006 total_servers++;
3007 }
3008 break;
3009 case ST_ADM_ACTION_READY:
3010 srv_adm_set_ready(sv);
3011 altered_servers++;
3012 total_servers++;
3013 break;
3014 case ST_ADM_ACTION_DRAIN:
3015 srv_adm_set_drain(sv);
3016 altered_servers++;
3017 total_servers++;
3018 break;
3019 case ST_ADM_ACTION_MAINT:
3020 srv_adm_set_maint(sv);
3021 altered_servers++;
3022 total_servers++;
3023 break;
3024 case ST_ADM_ACTION_SHUTDOWN:
3025 if (px->state != PR_STSTOPPED) {
3026 struct stream *sess, *sess_bck;
3027
3028 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3029 if (sess->srv_conn == sv)
3030 stream_shutdown(sess, SF_ERR_KILLED);
3031
3032 altered_servers++;
3033 total_servers++;
3034 }
3035 break;
3036 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003037 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003038 } else {
3039 /* the server name is unknown or ambiguous (duplicate names) */
3040 total_servers++;
3041 }
3042 }
3043 if (reprocess && px && action) {
3044 /* Now, we know the backend and the action chosen by the user.
3045 * We can safely restart from the first server parameter
3046 * to reprocess them
3047 */
3048 cur_param = st_cur_param;
3049 next_param = st_next_param;
3050 reprocess = 0;
3051 goto reprocess_servers;
3052 }
3053
3054 next_param = cur_param;
3055 }
3056 }
3057
3058 if (total_servers == 0) {
3059 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3060 }
3061 else if (altered_servers == 0) {
3062 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3063 }
3064 else if (altered_servers == total_servers) {
3065 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3066 }
3067 else {
3068 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3069 }
3070 out:
3071 return 1;
Christopher Fauletef779222018-10-31 08:47:01 +01003072}
3073
3074
3075static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
3076{
3077 struct stream *s = si_strm(si);
3078 struct uri_auth *uri = s->be->uri_auth;
3079 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003080 struct htx_sl *sl;
3081 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003082
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003083 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);
3084 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3085 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003086 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003087 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003088
3089 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
3090 !htx_add_header(htx, ist("Connection"), ist("close")))
3091 goto full;
3092 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3093 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3094 goto full;
3095 }
3096 else {
3097 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3098 goto full;
3099 }
3100
3101 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3102 const char *refresh = U2A(uri->refresh);
3103 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3104 goto full;
3105 }
3106
3107 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3108 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3109 goto full;
3110 }
3111
3112 if (!htx_add_endof(htx, HTX_BLK_EOH))
3113 goto full;
3114
3115 return 1;
3116
3117 full:
3118 htx_reset(htx);
3119 si_rx_room_blk(si);
3120 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003121}
3122
Christopher Fauletef779222018-10-31 08:47:01 +01003123
3124static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx)
3125{
3126 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3127 struct stream *s = si_strm(si);
3128 struct uri_auth *uri = s->be->uri_auth;
3129 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003130 struct htx_sl *sl;
3131 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003132
3133 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3134 scope_txt[0] = 0;
3135 if (appctx->ctx.stats.scope_len) {
3136 strcpy(scope_txt, STAT_SCOPE_PATTERN);
3137 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), co_head(si_oc(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3138 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3139 }
3140
3141 /* We don't want to land on the posted stats page because a refresh will
3142 * repost the data. We don't want this to happen on accident so we redirect
3143 * the browse to the stats page with a GET.
3144 */
3145 chunk_printf(&trash, "%s;st=%s%s%s%s",
3146 uri->uri_prefix,
3147 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3148 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3149 stat_status_codes[appctx->ctx.stats.st_code]) ?
3150 stat_status_codes[appctx->ctx.stats.st_code] :
3151 stat_status_codes[STAT_STATUS_UNKN],
3152 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3153 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3154 scope_txt);
3155
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003156 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3157 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3158 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003159 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003160 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003161
3162 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
3163 !htx_add_header(htx, ist("Connection"), ist("close")) ||
3164 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3165 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3166 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3167 goto full;
3168
3169 if (!htx_add_endof(htx, HTX_BLK_EOH))
3170 goto full;
3171
3172 return 1;
3173
3174full:
3175 htx_reset(htx);
3176 si_rx_room_blk(si);
3177 return 0;
3178}
William Lallemand74c24fb2016-11-21 17:18:36 +01003179
3180static int stats_send_http_headers(struct stream_interface *si)
3181{
3182 struct stream *s = si_strm(si);
3183 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003184 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003185
3186 chunk_printf(&trash,
3187 "HTTP/1.1 200 OK\r\n"
3188 "Cache-Control: no-cache\r\n"
3189 "Connection: close\r\n"
3190 "Content-Type: %s\r\n",
3191 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
3192
3193 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
3194 chunk_appendf(&trash, "Refresh: %d\r\n",
3195 uri->refresh);
3196
3197 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
3198
3199 if (appctx->ctx.stats.flags & STAT_CHUNKED)
3200 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
3201 else
3202 chunk_appendf(&trash, "\r\n");
3203
Willy Tarreau06d80a92017-10-19 14:32:15 +02003204 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003205 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003206 return 0;
3207 }
3208
3209 return 1;
3210}
3211
3212static int stats_send_http_redirect(struct stream_interface *si)
3213{
3214 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3215 struct stream *s = si_strm(si);
3216 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003217 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003218
3219 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3220 scope_txt[0] = 0;
3221 if (appctx->ctx.stats.scope_len) {
3222 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02003223 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 +01003224 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3225 }
3226
3227 /* We don't want to land on the posted stats page because a refresh will
3228 * repost the data. We don't want this to happen on accident so we redirect
3229 * the browse to the stats page with a GET.
3230 */
3231 chunk_printf(&trash,
3232 "HTTP/1.1 303 See Other\r\n"
3233 "Cache-Control: no-cache\r\n"
3234 "Content-Type: text/plain\r\n"
3235 "Connection: close\r\n"
3236 "Location: %s;st=%s%s%s%s\r\n"
3237 "Content-length: 0\r\n"
3238 "\r\n",
3239 uri->uri_prefix,
3240 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3241 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3242 stat_status_codes[appctx->ctx.stats.st_code]) ?
3243 stat_status_codes[appctx->ctx.stats.st_code] :
3244 stat_status_codes[STAT_STATUS_UNKN],
3245 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3246 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3247 scope_txt);
3248
Willy Tarreau06d80a92017-10-19 14:32:15 +02003249 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003250 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003251 return 0;
3252 }
3253
3254 return 1;
3255}
3256
Simon Horman05ee2132017-01-04 09:37:25 +01003257
William Lallemand74c24fb2016-11-21 17:18:36 +01003258/* This I/O handler runs as an applet embedded in a stream interface. It is
3259 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3260 * appctx->st0 contains the operation in progress (dump, done). The handler
3261 * automatically unregisters itself once transfer is complete.
3262 */
Christopher Fauletef779222018-10-31 08:47:01 +01003263static void htx_stats_io_handler(struct appctx *appctx)
3264{
3265 struct stream_interface *si = appctx->owner;
3266 struct stream *s = si_strm(si);
3267 struct channel *req = si_oc(si);
3268 struct channel *res = si_ic(si);
3269 struct htx *req_htx, *res_htx;
3270
3271 res_htx = htx_from_buf(&res->buf);
3272
3273 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3274 goto out;
3275
3276 /* Check if the input buffer is avalaible. */
3277 if (!b_size(&res->buf)) {
3278 si_rx_room_blk(si);
3279 goto out;
3280 }
3281
3282 /* check that the output is not closed */
3283 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3284 appctx->st0 = STAT_HTTP_DONE;
3285
3286 /* all states are processed in sequence */
3287 if (appctx->st0 == STAT_HTTP_HEAD) {
3288 if (stats_send_htx_headers(si, res_htx)) {
3289 if (s->txn->meth == HTTP_METH_HEAD)
3290 appctx->st0 = STAT_HTTP_DONE;
3291 else
3292 appctx->st0 = STAT_HTTP_DUMP;
3293 }
3294 }
3295
3296 if (appctx->st0 == STAT_HTTP_DUMP) {
3297 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3298 appctx->st0 = STAT_HTTP_DONE;
3299 }
3300
3301 if (appctx->st0 == STAT_HTTP_POST) {
3302 if (stats_process_http_post(si))
3303 appctx->st0 = STAT_HTTP_LAST;
3304 else if (si_oc(si)->flags & CF_SHUTR)
3305 appctx->st0 = STAT_HTTP_DONE;
3306 }
3307
3308 if (appctx->st0 == STAT_HTTP_LAST) {
3309 if (stats_send_htx_redirect(si, res_htx))
3310 appctx->st0 = STAT_HTTP_DONE;
3311 }
3312
3313 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Fauletb1b08212018-11-21 13:51:07 +01003314 /* Don't add EOD and TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003315 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3316 si_rx_room_blk(si);
3317 goto out;
3318 }
3319
3320 /* eat the whole request */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003321 req_htx = htxbuf(&req->buf);
3322 htx_reset(req_htx);
3323 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9b95d312018-11-19 21:58:10 +01003324 co_set_data(req, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01003325 res->flags |= CF_READ_NULL;
3326 si_shutr(si);
3327 }
3328
3329 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3330 si_shutw(si);
3331
3332 if (appctx->st0 == STAT_HTTP_DONE) {
3333 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3334 si_shutr(si);
3335 res->flags |= CF_READ_NULL;
3336 }
3337 }
3338 out:
3339 /* we have left the request in the buffer for the case where we
3340 * process a POST, and this automatically re-enables activity on
3341 * read. It's better to indicate that we want to stop reading when
3342 * we're sending, so that we know there's at most one direction
3343 * deciding to wake the applet up. It saves it from looping when
3344 * emitting large blocks into small TCP windows.
3345 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003346 htx_to_buf(res_htx, &res->buf);
3347 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003348 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003349}
3350
3351
3352/* This I/O handler runs as an applet embedded in a stream interface. It is
3353 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3354 * appctx->st0 contains the operation in progress (dump, done). The handler
3355 * automatically unregisters itself once transfer is complete.
3356 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003357static void http_stats_io_handler(struct appctx *appctx)
3358{
3359 struct stream_interface *si = appctx->owner;
3360 struct stream *s = si_strm(si);
3361 struct channel *req = si_oc(si);
3362 struct channel *res = si_ic(si);
3363
Christopher Fauletef779222018-10-31 08:47:01 +01003364 if (IS_HTX_STRM(s))
3365 return htx_stats_io_handler(appctx);
3366
William Lallemand74c24fb2016-11-21 17:18:36 +01003367 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3368 goto out;
3369
Joseph Herlant29023ec2018-11-15 13:39:46 -08003370 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003371 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003372 /* already subscribed, we'll be called later once the buffer is
3373 * available.
3374 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003375 goto out;
3376 }
3377
William Lallemand74c24fb2016-11-21 17:18:36 +01003378 /* check that the output is not closed */
3379 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3380 appctx->st0 = STAT_HTTP_DONE;
3381
3382 /* all states are processed in sequence */
3383 if (appctx->st0 == STAT_HTTP_HEAD) {
3384 if (stats_send_http_headers(si)) {
3385 if (s->txn->meth == HTTP_METH_HEAD)
3386 appctx->st0 = STAT_HTTP_DONE;
3387 else
3388 appctx->st0 = STAT_HTTP_DUMP;
3389 }
3390 }
3391
3392 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003393 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003394 unsigned int data_len;
3395 unsigned int last_len;
3396 unsigned int last_fwd = 0;
3397
3398 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3399 /* One difficulty we're facing is that we must prevent
3400 * the input data from being automatically forwarded to
3401 * the output area. For this, we temporarily disable
3402 * forwarding on the channel.
3403 */
3404 last_fwd = si_ic(si)->to_forward;
3405 si_ic(si)->to_forward = 0;
3406 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003407 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003408 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003409 si_ic(si)->to_forward = last_fwd;
3410 goto out;
3411 }
3412 }
3413
Willy Tarreau97f538b2018-06-15 19:41:31 +02003414 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003415 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003416 appctx->st0 = STAT_HTTP_DONE;
3417
Willy Tarreau97f538b2018-06-15 19:41:31 +02003418 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003419
3420 /* Now we must either adjust or remove the chunk size. This is
3421 * not easy because the chunk size might wrap at the end of the
3422 * buffer, so we pretend we have nothing in the buffer, we write
3423 * the size, then restore the buffer's contents. Note that we can
3424 * only do that because no forwarding is scheduled on the stats
3425 * applet.
3426 */
3427 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3428 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003429 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003430
3431 if (last_len != data_len) {
3432 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003433 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003434 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003435
3436 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003437 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003438 }
3439 /* now re-enable forwarding */
3440 channel_forward(si_ic(si), last_fwd);
3441 }
3442 }
3443
3444 if (appctx->st0 == STAT_HTTP_POST) {
3445 if (stats_process_http_post(si))
3446 appctx->st0 = STAT_HTTP_LAST;
3447 else if (si_oc(si)->flags & CF_SHUTR)
3448 appctx->st0 = STAT_HTTP_DONE;
3449 }
3450
3451 if (appctx->st0 == STAT_HTTP_LAST) {
3452 if (stats_send_http_redirect(si))
3453 appctx->st0 = STAT_HTTP_DONE;
3454 }
3455
3456 if (appctx->st0 == STAT_HTTP_DONE) {
3457 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3458 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003459 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003460 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003461 goto out;
3462 }
3463 }
3464 /* eat the whole request */
Willy Tarreau97f538b2018-06-15 19:41:31 +02003465 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003466 res->flags |= CF_READ_NULL;
3467 si_shutr(si);
3468 }
3469
3470 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3471 si_shutw(si);
3472
3473 if (appctx->st0 == STAT_HTTP_DONE) {
3474 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3475 si_shutr(si);
3476 res->flags |= CF_READ_NULL;
3477 }
3478 }
3479 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003480 /* we have left the request in the buffer for the case where we
3481 * process a POST, and this automatically re-enables activity on
3482 * read. It's better to indicate that we want to stop reading when
3483 * we're sending, so that we know there's at most one direction
3484 * deciding to wake the applet up. It saves it from looping when
3485 * emitting large blocks into small TCP windows.
3486 */
3487 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003488 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003489}
3490
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003491/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003492static int stats_dump_info_fields(struct buffer *out,
3493 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003494{
3495 int field;
3496
3497 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3498 if (!field_format(info, field))
3499 continue;
3500
3501 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3502 return 0;
3503 if (!stats_emit_raw_data_field(out, &info[field]))
3504 return 0;
3505 if (!chunk_strcat(out, "\n"))
3506 return 0;
3507 }
3508 return 1;
3509}
3510
3511/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003512static int stats_dump_typed_info_fields(struct buffer *out,
3513 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003514{
3515 int field;
3516
3517 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3518 if (!field_format(info, field))
3519 continue;
3520
3521 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3522 return 0;
3523 if (!stats_emit_field_tags(out, &info[field], ':'))
3524 return 0;
3525 if (!stats_emit_typed_data_field(out, &info[field]))
3526 return 0;
3527 if (!chunk_strcat(out, "\n"))
3528 return 0;
3529 }
3530 return 1;
3531}
3532
3533/* Fill <info> with HAProxy global info. <info> is preallocated
3534 * array of length <len>. The length of the aray must be
3535 * INF_TOTAL_FIELDS. If this length is less then this value, the
3536 * function returns 0, otherwise, it returns 1.
3537 */
3538int stats_fill_info(struct field *info, int len)
3539{
3540 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003541 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003542
3543#ifdef USE_OPENSSL
3544 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3545 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3546 int ssl_reuse = 0;
3547
3548 if (ssl_key_rate < ssl_sess_rate) {
3549 /* count the ssl reuse ratio and avoid overflows in both directions */
3550 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3551 }
3552#endif
3553
3554 if (len < INF_TOTAL_FIELDS)
3555 return 0;
3556
3557 chunk_reset(out);
3558 memset(info, 0, sizeof(*info) * len);
3559
3560 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003561 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3562 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003563
Yves Lafon95317282018-02-26 11:10:37 +01003564 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003565 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3566 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3567 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3568
3569 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3570 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3571
3572 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3573 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3574 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3575 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3576 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3577 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3578 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3579 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3580 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3581 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3582 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3583 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3584#ifdef USE_OPENSSL
3585 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3586 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3587 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3588#endif
3589 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3590 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3591 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3592 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3593 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3594 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3595 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3596 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3597 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3598
3599#ifdef USE_OPENSSL
3600 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3601 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3602 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3603 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3604 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3605 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3606 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3607 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3608 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3609 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3610#endif
3611 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3612 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3613 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3614#ifdef USE_ZLIB
3615 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3616 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3617#endif
3618 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003619 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003620 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3621 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3622 if (global.desc)
3623 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003624 info[INF_STOPPING] = mkf_u32(0, stopping);
3625 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003626 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003627 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003628 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003629 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003630 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003631 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003632
3633 return 1;
3634}
3635
3636/* This function dumps information onto the stream interface's read buffer.
3637 * It returns 0 as long as it does not complete, non-zero upon completion.
3638 * No state is used.
3639 */
3640static int stats_dump_info_to_buffer(struct stream_interface *si)
3641{
3642 struct appctx *appctx = __objt_appctx(si->end);
3643
3644 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3645 return 0;
3646
3647 chunk_reset(&trash);
3648
3649 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3650 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003651 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3652 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003653 else
3654 stats_dump_info_fields(&trash, info);
3655
Willy Tarreau06d80a92017-10-19 14:32:15 +02003656 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003657 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003658 return 0;
3659 }
3660
3661 return 1;
3662}
3663
Simon Horman6f6bb382017-01-04 09:37:26 +01003664/* This function dumps the schema onto the stream interface's read buffer.
3665 * It returns 0 as long as it does not complete, non-zero upon completion.
3666 * No state is used.
3667 *
3668 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3669 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3670 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003671static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003672{
3673
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003674 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003675
3676 chunk_strcat(out,
3677 "{"
3678 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3679 "\"oneOf\":["
3680 "{"
3681 "\"title\":\"Info\","
3682 "\"type\":\"array\","
3683 "\"items\":{"
3684 "\"properties\":{"
3685 "\"title\":\"InfoItem\","
3686 "\"type\":\"object\","
3687 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3688 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3689 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3690 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3691 "},"
3692 "\"required\":[\"field\",\"processNum\",\"tags\","
3693 "\"value\"]"
3694 "}"
3695 "},"
3696 "{"
3697 "\"title\":\"Stat\","
3698 "\"type\":\"array\","
3699 "\"items\":{"
3700 "\"title\":\"InfoItem\","
3701 "\"type\":\"object\","
3702 "\"properties\":{"
3703 "\"objType\":{"
3704 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3705 "\"Server\",\"Unknown\"]"
3706 "},"
3707 "\"proxyId\":{"
3708 "\"type\":\"integer\","
3709 "\"minimum\":0"
3710 "},"
3711 "\"id\":{"
3712 "\"type\":\"integer\","
3713 "\"minimum\":0"
3714 "},"
3715 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3716 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3717 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3718 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3719 "},"
3720 "\"required\":[\"objType\",\"proxyId\",\"id\","
3721 "\"field\",\"processNum\",\"tags\","
3722 "\"value\"]"
3723 "}"
3724 "},"
3725 "{"
3726 "\"title\":\"Error\","
3727 "\"type\":\"object\","
3728 "\"properties\":{"
3729 "\"errorStr\":{"
3730 "\"type\":\"string\""
3731 "},"
3732 "\"required\":[\"errorStr\"]"
3733 "}"
3734 "}"
3735 "],"
3736 "\"definitions\":{"
3737 "\"field\":{"
3738 "\"type\":\"object\","
3739 "\"pos\":{"
3740 "\"type\":\"integer\","
3741 "\"minimum\":0"
3742 "},"
3743 "\"name\":{"
3744 "\"type\":\"string\""
3745 "},"
3746 "\"required\":[\"pos\",\"name\"]"
3747 "},"
3748 "\"processNum\":{"
3749 "\"type\":\"integer\","
3750 "\"minimum\":1"
3751 "},"
3752 "\"tags\":{"
3753 "\"type\":\"object\","
3754 "\"origin\":{"
3755 "\"type\":\"string\","
3756 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3757 "\"Config\",\"Product\",\"Unknown\"]"
3758 "},"
3759 "\"nature\":{"
3760 "\"type\":\"string\","
3761 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3762 "\"Rate\",\"Counter\",\"Duration\","
3763 "\"Age\",\"Time\",\"Name\",\"Output\","
3764 "\"Avg\", \"Unknown\"]"
3765 "},"
3766 "\"scope\":{"
3767 "\"type\":\"string\","
3768 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3769 "\"System\",\"Unknown\"]"
3770 "},"
3771 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3772 "},"
3773 "\"typedValue\":{"
3774 "\"type\":\"object\","
3775 "\"oneOf\":["
3776 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3777 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3778 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3779 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3780 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3781 "],"
3782 "\"definitions\":{"
3783 "\"s32Value\":{"
3784 "\"properties\":{"
3785 "\"type\":{"
3786 "\"type\":\"string\","
3787 "\"enum\":[\"s32\"]"
3788 "},"
3789 "\"value\":{"
3790 "\"type\":\"integer\","
3791 "\"minimum\":-2147483648,"
3792 "\"maximum\":2147483647"
3793 "}"
3794 "},"
3795 "\"required\":[\"type\",\"value\"]"
3796 "},"
3797 "\"s64Value\":{"
3798 "\"properties\":{"
3799 "\"type\":{"
3800 "\"type\":\"string\","
3801 "\"enum\":[\"s64\"]"
3802 "},"
3803 "\"value\":{"
3804 "\"type\":\"integer\","
3805 "\"minimum\":-9007199254740991,"
3806 "\"maximum\":9007199254740991"
3807 "}"
3808 "},"
3809 "\"required\":[\"type\",\"value\"]"
3810 "},"
3811 "\"u32Value\":{"
3812 "\"properties\":{"
3813 "\"type\":{"
3814 "\"type\":\"string\","
3815 "\"enum\":[\"u32\"]"
3816 "},"
3817 "\"value\":{"
3818 "\"type\":\"integer\","
3819 "\"minimum\":0,"
3820 "\"maximum\":4294967295"
3821 "}"
3822 "},"
3823 "\"required\":[\"type\",\"value\"]"
3824 "},"
3825 "\"u64Value\":{"
3826 "\"properties\":{"
3827 "\"type\":{"
3828 "\"type\":\"string\","
3829 "\"enum\":[\"u64\"]"
3830 "},"
3831 "\"value\":{"
3832 "\"type\":\"integer\","
3833 "\"minimum\":0,"
3834 "\"maximum\":9007199254740991"
3835 "}"
3836 "},"
3837 "\"required\":[\"type\",\"value\"]"
3838 "},"
3839 "\"strValue\":{"
3840 "\"properties\":{"
3841 "\"type\":{"
3842 "\"type\":\"string\","
3843 "\"enum\":[\"str\"]"
3844 "},"
3845 "\"value\":{\"type\":\"string\"}"
3846 "},"
3847 "\"required\":[\"type\",\"value\"]"
3848 "},"
3849 "\"unknownValue\":{"
3850 "\"properties\":{"
3851 "\"type\":{"
3852 "\"type\":\"integer\","
3853 "\"minimum\":0"
3854 "},"
3855 "\"value\":{"
3856 "\"type\":\"string\","
3857 "\"enum\":[\"unknown\"]"
3858 "}"
3859 "},"
3860 "\"required\":[\"type\",\"value\"]"
3861 "}"
3862 "}"
3863 "}"
3864 "}"
3865 "}");
3866
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003867 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003868 chunk_reset(out);
3869 chunk_appendf(out,
3870 "{\"errorStr\":\"output buffer too short\"}");
3871 }
3872}
3873
3874/* This function dumps the schema onto the stream interface's read buffer.
3875 * It returns 0 as long as it does not complete, non-zero upon completion.
3876 * No state is used.
3877 */
3878static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3879{
3880 chunk_reset(&trash);
3881
3882 stats_dump_json_schema(&trash);
3883
Willy Tarreau06d80a92017-10-19 14:32:15 +02003884 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003885 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003886 return 0;
3887 }
3888
3889 return 1;
3890}
3891
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003892static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003893{
3894 struct proxy *px;
3895 struct server *sv;
3896 struct listener *li;
3897 int clrall = 0;
3898
3899 if (strcmp(args[2], "all") == 0)
3900 clrall = 1;
3901
3902 /* check permissions */
3903 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3904 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3905 return 1;
3906
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003907 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003908 if (clrall) {
3909 memset(&px->be_counters, 0, sizeof(px->be_counters));
3910 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3911 }
3912 else {
3913 px->be_counters.conn_max = 0;
3914 px->be_counters.p.http.rps_max = 0;
3915 px->be_counters.sps_max = 0;
3916 px->be_counters.cps_max = 0;
3917 px->be_counters.nbpend_max = 0;
3918
3919 px->fe_counters.conn_max = 0;
3920 px->fe_counters.p.http.rps_max = 0;
3921 px->fe_counters.sps_max = 0;
3922 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003923 }
3924
3925 for (sv = px->srv; sv; sv = sv->next)
3926 if (clrall)
3927 memset(&sv->counters, 0, sizeof(sv->counters));
3928 else {
3929 sv->counters.cur_sess_max = 0;
3930 sv->counters.nbpend_max = 0;
3931 sv->counters.sps_max = 0;
3932 }
3933
3934 list_for_each_entry(li, &px->conf.listeners, by_fe)
3935 if (li->counters) {
3936 if (clrall)
3937 memset(li->counters, 0, sizeof(*li->counters));
3938 else
3939 li->counters->conn_max = 0;
3940 }
3941 }
3942
3943 global.cps_max = 0;
3944 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003945 global.ssl_max = 0;
3946 global.ssl_fe_keys_max = 0;
3947 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003948
3949 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003950 return 1;
3951}
3952
3953
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003954static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003955{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003956 appctx->ctx.stats.scope_str = 0;
3957 appctx->ctx.stats.scope_len = 0;
3958 appctx->ctx.stats.flags = 0;
3959
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003960 if (strcmp(args[2], "typed") == 0)
3961 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003962 else if (strcmp(args[2], "json") == 0)
3963 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003964 return 0;
3965}
3966
3967
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003968static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003969{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003970 appctx->ctx.stats.scope_str = 0;
3971 appctx->ctx.stats.scope_len = 0;
3972 appctx->ctx.stats.flags = 0;
3973
Willy Tarreau2b812e22016-11-22 16:18:05 +01003974 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003975 struct proxy *px;
3976
3977 px = proxy_find_by_name(args[2], 0, 0);
3978 if (px)
3979 appctx->ctx.stats.iid = px->uuid;
3980 else
3981 appctx->ctx.stats.iid = atoi(args[2]);
3982
3983 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003984 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003985 appctx->ctx.cli.msg = "No such proxy.\n";
3986 appctx->st0 = CLI_ST_PRINT;
3987 return 1;
3988 }
3989
Willy Tarreau2b812e22016-11-22 16:18:05 +01003990 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003991 appctx->ctx.stats.type = atoi(args[3]);
3992 appctx->ctx.stats.sid = atoi(args[4]);
3993 if (strcmp(args[5], "typed") == 0)
3994 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003995 else if (strcmp(args[5], "json") == 0)
3996 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003997 }
3998 else if (strcmp(args[2], "typed") == 0)
3999 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004000 else if (strcmp(args[2], "json") == 0)
4001 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004002
Willy Tarreau2b812e22016-11-22 16:18:05 +01004003 return 0;
4004}
4005
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004006static int cli_io_handler_dump_info(struct appctx *appctx)
4007{
4008 return stats_dump_info_to_buffer(appctx->owner);
4009}
4010
Willy Tarreau2b812e22016-11-22 16:18:05 +01004011/* This I/O handler runs as an applet embedded in a stream interface. It is
4012 * used to send raw stats over a socket.
4013 */
4014static int cli_io_handler_dump_stat(struct appctx *appctx)
4015{
Christopher Fauletef779222018-10-31 08:47:01 +01004016 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004017}
4018
Simon Horman6f6bb382017-01-04 09:37:26 +01004019static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4020{
4021 return stats_dump_json_schema_to_buffer(appctx->owner);
4022}
4023
Willy Tarreau2b812e22016-11-22 16:18:05 +01004024/* register cli keywords */
4025static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004026 { { "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 +01004027 { { "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 +01004028 { { "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 +01004029 { { "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 +01004030 {{},}
4031}};
4032
Willy Tarreau0108d902018-11-25 19:14:37 +01004033INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4034
William Lallemand74c24fb2016-11-21 17:18:36 +01004035struct applet http_stats_applet = {
4036 .obj_type = OBJ_TYPE_APPLET,
4037 .name = "<STATS>", /* used for logging */
4038 .fct = http_stats_io_handler,
4039 .release = NULL,
4040};
4041
William Lallemand74c24fb2016-11-21 17:18:36 +01004042/*
4043 * Local variables:
4044 * c-indent-level: 8
4045 * c-basic-offset: 8
4046 * End:
4047 */