blob: 4a87d5ed56deefc804207718a4e0e1f87ba65e63 [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>
Baptiste Assmann333939c2019-01-21 08:34:50 +010054#include <proto/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010055#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010056#include <proto/fd.h>
57#include <proto/freq_ctr.h>
58#include <proto/frontend.h>
Christopher Fauleted7a0662019-01-14 11:07:34 +010059#include <proto/http_htx.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010060#include <proto/log.h>
61#include <proto/pattern.h>
62#include <proto/pipe.h>
63#include <proto/listener.h>
64#include <proto/map.h>
Christopher Fauletef779222018-10-31 08:47:01 +010065#include <proto/proto_http.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066#include <proto/proxy.h>
67#include <proto/sample.h>
68#include <proto/session.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020069#include <proto/ssl_sock.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010070#include <proto/stream.h>
71#include <proto/server.h>
72#include <proto/raw_sock.h>
73#include <proto/stream_interface.h>
74#include <proto/task.h>
75
William Lallemand74c24fb2016-11-21 17:18:36 +010076
Christopher Faulet2b9b6782019-02-27 16:42:58 +010077/* status codes available for the stats admin page (strictly 4 chars length) */
78const char *stat_status_codes[STAT_STATUS_SIZE] = {
79 [STAT_STATUS_DENY] = "DENY",
80 [STAT_STATUS_DONE] = "DONE",
81 [STAT_STATUS_ERRP] = "ERRP",
82 [STAT_STATUS_EXCD] = "EXCD",
83 [STAT_STATUS_NONE] = "NONE",
84 [STAT_STATUS_PART] = "PART",
85 [STAT_STATUS_UNKN] = "UNKN",
86 [STAT_STATUS_IVAL] = "IVAL",
87};
88
Willy Tarreau0baac8c2016-11-22 16:36:53 +010089/* These are the field names for each INF_* field position. Please pay attention
90 * to always use the exact same name except that the strings for new names must
91 * be lower case or CamelCase while the enum entries must be upper case.
92 */
93const char *info_field_names[INF_TOTAL_FIELDS] = {
94 [INF_NAME] = "Name",
95 [INF_VERSION] = "Version",
96 [INF_RELEASE_DATE] = "Release_date",
Yves Lafon95317282018-02-26 11:10:37 +010097 [INF_NBTHREAD] = "Nbthread",
Willy Tarreau0baac8c2016-11-22 16:36:53 +010098 [INF_NBPROC] = "Nbproc",
99 [INF_PROCESS_NUM] = "Process_num",
100 [INF_PID] = "Pid",
101 [INF_UPTIME] = "Uptime",
102 [INF_UPTIME_SEC] = "Uptime_sec",
103 [INF_MEMMAX_MB] = "Memmax_MB",
104 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
105 [INF_POOL_USED_MB] = "PoolUsed_MB",
106 [INF_POOL_FAILED] = "PoolFailed",
107 [INF_ULIMIT_N] = "Ulimit-n",
108 [INF_MAXSOCK] = "Maxsock",
109 [INF_MAXCONN] = "Maxconn",
110 [INF_HARD_MAXCONN] = "Hard_maxconn",
111 [INF_CURR_CONN] = "CurrConns",
112 [INF_CUM_CONN] = "CumConns",
113 [INF_CUM_REQ] = "CumReq",
114 [INF_MAX_SSL_CONNS] = "MaxSslConns",
115 [INF_CURR_SSL_CONNS] = "CurrSslConns",
116 [INF_CUM_SSL_CONNS] = "CumSslConns",
117 [INF_MAXPIPES] = "Maxpipes",
118 [INF_PIPES_USED] = "PipesUsed",
119 [INF_PIPES_FREE] = "PipesFree",
120 [INF_CONN_RATE] = "ConnRate",
121 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
122 [INF_MAX_CONN_RATE] = "MaxConnRate",
123 [INF_SESS_RATE] = "SessRate",
124 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
125 [INF_MAX_SESS_RATE] = "MaxSessRate",
126 [INF_SSL_RATE] = "SslRate",
127 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
128 [INF_MAX_SSL_RATE] = "MaxSslRate",
129 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
130 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
131 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
132 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
133 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
134 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
135 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
136 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
137 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
138 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
139 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
140 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
141 [INF_TASKS] = "Tasks",
142 [INF_RUN_QUEUE] = "Run_queue",
143 [INF_IDLE_PCT] = "Idle_pct",
144 [INF_NODE] = "node",
145 [INF_DESCRIPTION] = "description",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100146 [INF_STOPPING] = "Stopping",
147 [INF_JOBS] = "Jobs",
William Lallemanda7199262018-11-16 16:57:20 +0100148 [INF_UNSTOPPABLE_JOBS] = "Unstoppable Jobs",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100149 [INF_LISTENERS] = "Listeners",
Willy Tarreau199ad242018-11-05 16:31:22 +0100150 [INF_ACTIVE_PEERS] = "ActivePeers",
Willy Tarreau2d372c22018-11-05 17:12:27 +0100151 [INF_CONNECTED_PEERS] = "ConnectedPeers",
Willy Tarreau13ef7732018-11-12 07:25:28 +0100152 [INF_DROPPED_LOGS] = "DroppedLogs",
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100153 [INF_BUSY_POLLING] = "BusyPolling",
Baptiste Assmann333939c2019-01-21 08:34:50 +0100154 [INF_FAILED_RESOLUTIONS] = "FailedResolutions",
Willy Tarreau7cf0e452019-05-23 11:39:14 +0200155 [INF_TOTAL_BYTES_OUT] = "TotalBytesOut",
156 [INF_BYTES_OUT_RATE] = "BytesOutRate",
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100157};
158
William Lallemand74c24fb2016-11-21 17:18:36 +0100159const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
160 [ST_F_PXNAME] = "pxname",
161 [ST_F_SVNAME] = "svname",
162 [ST_F_QCUR] = "qcur",
163 [ST_F_QMAX] = "qmax",
164 [ST_F_SCUR] = "scur",
165 [ST_F_SMAX] = "smax",
166 [ST_F_SLIM] = "slim",
167 [ST_F_STOT] = "stot",
168 [ST_F_BIN] = "bin",
169 [ST_F_BOUT] = "bout",
170 [ST_F_DREQ] = "dreq",
171 [ST_F_DRESP] = "dresp",
172 [ST_F_EREQ] = "ereq",
173 [ST_F_ECON] = "econ",
174 [ST_F_ERESP] = "eresp",
175 [ST_F_WRETR] = "wretr",
176 [ST_F_WREDIS] = "wredis",
177 [ST_F_STATUS] = "status",
178 [ST_F_WEIGHT] = "weight",
179 [ST_F_ACT] = "act",
180 [ST_F_BCK] = "bck",
181 [ST_F_CHKFAIL] = "chkfail",
182 [ST_F_CHKDOWN] = "chkdown",
183 [ST_F_LASTCHG] = "lastchg",
184 [ST_F_DOWNTIME] = "downtime",
185 [ST_F_QLIMIT] = "qlimit",
186 [ST_F_PID] = "pid",
187 [ST_F_IID] = "iid",
188 [ST_F_SID] = "sid",
189 [ST_F_THROTTLE] = "throttle",
190 [ST_F_LBTOT] = "lbtot",
191 [ST_F_TRACKED] = "tracked",
192 [ST_F_TYPE] = "type",
193 [ST_F_RATE] = "rate",
194 [ST_F_RATE_LIM] = "rate_lim",
195 [ST_F_RATE_MAX] = "rate_max",
196 [ST_F_CHECK_STATUS] = "check_status",
197 [ST_F_CHECK_CODE] = "check_code",
198 [ST_F_CHECK_DURATION] = "check_duration",
199 [ST_F_HRSP_1XX] = "hrsp_1xx",
200 [ST_F_HRSP_2XX] = "hrsp_2xx",
201 [ST_F_HRSP_3XX] = "hrsp_3xx",
202 [ST_F_HRSP_4XX] = "hrsp_4xx",
203 [ST_F_HRSP_5XX] = "hrsp_5xx",
204 [ST_F_HRSP_OTHER] = "hrsp_other",
205 [ST_F_HANAFAIL] = "hanafail",
206 [ST_F_REQ_RATE] = "req_rate",
207 [ST_F_REQ_RATE_MAX] = "req_rate_max",
208 [ST_F_REQ_TOT] = "req_tot",
209 [ST_F_CLI_ABRT] = "cli_abrt",
210 [ST_F_SRV_ABRT] = "srv_abrt",
211 [ST_F_COMP_IN] = "comp_in",
212 [ST_F_COMP_OUT] = "comp_out",
213 [ST_F_COMP_BYP] = "comp_byp",
214 [ST_F_COMP_RSP] = "comp_rsp",
215 [ST_F_LASTSESS] = "lastsess",
216 [ST_F_LAST_CHK] = "last_chk",
217 [ST_F_LAST_AGT] = "last_agt",
218 [ST_F_QTIME] = "qtime",
219 [ST_F_CTIME] = "ctime",
220 [ST_F_RTIME] = "rtime",
221 [ST_F_TTIME] = "ttime",
222 [ST_F_AGENT_STATUS] = "agent_status",
223 [ST_F_AGENT_CODE] = "agent_code",
224 [ST_F_AGENT_DURATION] = "agent_duration",
225 [ST_F_CHECK_DESC] = "check_desc",
226 [ST_F_AGENT_DESC] = "agent_desc",
227 [ST_F_CHECK_RISE] = "check_rise",
228 [ST_F_CHECK_FALL] = "check_fall",
229 [ST_F_CHECK_HEALTH] = "check_health",
230 [ST_F_AGENT_RISE] = "agent_rise",
231 [ST_F_AGENT_FALL] = "agent_fall",
232 [ST_F_AGENT_HEALTH] = "agent_health",
233 [ST_F_ADDR] = "addr",
234 [ST_F_COOKIE] = "cookie",
235 [ST_F_MODE] = "mode",
236 [ST_F_ALGO] = "algo",
237 [ST_F_CONN_RATE] = "conn_rate",
238 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
239 [ST_F_CONN_TOT] = "conn_tot",
240 [ST_F_INTERCEPTED] = "intercepted",
241 [ST_F_DCON] = "dcon",
242 [ST_F_DSES] = "dses",
Tim Duesterhus3fd19732018-05-27 20:35:08 +0200243 [ST_F_WREW] = "wrew",
Willy Tarreauf1573842018-12-14 11:35:36 +0100244 [ST_F_CONNECT] = "connect",
245 [ST_F_REUSE] = "reuse",
Willy Tarreaua1214a52018-12-14 14:00:25 +0100246 [ST_F_CACHE_LOOKUPS] = "cache_lookups",
247 [ST_F_CACHE_HITS] = "cache_hits",
Adis Neziroviceeea5702019-09-13 11:43:03 +0200248 [ST_F_SRV_ICUR] = "srv_icur",
Christopher Fauletb3fdd522019-11-08 14:59:51 +0100249 [ST_F_SRV_ILIM] = "src_ilim",
250 [ST_F_QT_MAX] = "qtime_max",
251 [ST_F_CT_MAX] = "ctime_max",
252 [ST_F_RT_MAX] = "rtime_max",
253 [ST_F_TT_MAX] = "ttime_max",
William Lallemand74c24fb2016-11-21 17:18:36 +0100254};
255
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100256/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100257static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100258/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100259static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100260
261
Christopher Fauletef779222018-10-31 08:47:01 +0100262static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
263{
264 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100265 if (chk->data >= channel_htx_recv_max(chn, htx))
266 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200267 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100268 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100269 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100270 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100271 }
272 else {
273 if (ci_putchk(chn, chk) == -1)
274 return 0;
275 }
276 return 1;
277}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100278
Christopher Fauleted7a0662019-01-14 11:07:34 +0100279static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
280{
281 const char *p;
282
283 if (IS_HTX_STRM(si_strm(si))) {
284 struct channel *req = si_oc(si);
285 struct htx *htx = htxbuf(&req->buf);
Christopher Faulet8fa60e42019-05-23 11:04:05 +0200286 struct htx_blk *blk;
287 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100288
Christopher Faulet8fa60e42019-05-23 11:04:05 +0200289 blk = htx_get_head_blk(htx);
Christopher Faulet43a686d2019-11-18 15:50:25 +0100290 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Faulet8fa60e42019-05-23 11:04:05 +0200291 ALREADY_CHECKED(blk);
292 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Christopher Fauleted7a0662019-01-14 11:07:34 +0100293 p = uri.ptr;
294 }
295 else
296 p = co_head(si_oc(si));
297
298 return p + appctx->ctx.stats.scope_str;
299}
300
William Lallemand74c24fb2016-11-21 17:18:36 +0100301/*
302 * http_stats_io_handler()
303 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
304 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100305 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100306 * -> stats_dump_html_head() // emits the HTML headers
307 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
308 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
309 * -> stats_dump_html_px_hdr()
310 * -> stats_dump_fe_stats()
311 * -> stats_dump_li_stats()
312 * -> stats_dump_sv_stats()
313 * -> stats_dump_be_stats()
314 * -> stats_dump_html_px_end()
315 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100316 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100317 */
318
319
William Lallemand74c24fb2016-11-21 17:18:36 +0100320/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
321 * for clearing it if needed.
322 * NOTE: Some tools happen to rely on the field position instead of its name,
323 * so please only append new fields at the end, never in the middle.
324 */
325static void stats_dump_csv_header()
326{
327 int field;
328
329 chunk_appendf(&trash, "# ");
330 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
331 chunk_appendf(&trash, "%s,", stat_field_names[field]);
332
333 chunk_appendf(&trash, "\n");
334}
335
336
337/* Emits a stats field without any surrounding element and properly encoded to
338 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
339 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200340int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100341{
342 switch (field_format(f, 0)) {
343 case FF_EMPTY: return 1;
344 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
345 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
346 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
347 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet87d74c12019-09-24 16:35:10 +0200348 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100349 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
350 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
351 }
352}
353
354/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
355 * output is supposed to be used on its own line. Returns non-zero on success, 0
356 * if the buffer is full.
357 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200358int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100359{
360 switch (field_format(f, 0)) {
361 case FF_EMPTY: return 1;
362 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
363 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
364 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
365 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet87d74c12019-09-24 16:35:10 +0200366 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100367 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
368 default: return chunk_appendf(out, "%08x:?", f->type);
369 }
370}
371
Simon Horman05ee2132017-01-04 09:37:25 +0100372/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
373 * the recommendation for interoperable integers in section 6 of RFC 7159.
374 */
375#define JSON_INT_MAX ((1ULL << 53) - 1)
376#define JSON_INT_MIN (0 - JSON_INT_MAX)
377
378/* Emits a stats field value and its type in JSON.
379 * Returns non-zero on success, 0 on error.
380 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200381int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100382{
383 int old_len;
384 char buf[20];
385 const char *type, *value = buf, *quote = "";
386
387 switch (field_format(f, 0)) {
388 case FF_EMPTY: return 1;
389 case FF_S32: type = "\"s32\"";
390 snprintf(buf, sizeof(buf), "%d", f->u.s32);
391 break;
392 case FF_U32: type = "\"u32\"";
393 snprintf(buf, sizeof(buf), "%u", f->u.u32);
394 break;
395 case FF_S64: type = "\"s64\"";
396 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
397 return 0;
398 type = "\"s64\"";
399 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
400 break;
401 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
402 return 0;
403 type = "\"u64\"";
404 snprintf(buf, sizeof(buf), "%llu",
405 (unsigned long long) f->u.u64);
Christopher Fauletc0104fc2019-09-28 10:37:31 +0200406 break;
Christopher Faulet87d74c12019-09-24 16:35:10 +0200407 case FF_FLT: type = "\"flt\"";
408 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100409 break;
410 case FF_STR: type = "\"str\"";
411 value = field_str(f, 0);
412 quote = "\"";
413 break;
414 default: snprintf(buf, sizeof(buf), "%u", f->type);
415 type = buf;
416 value = "unknown";
417 quote = "\"";
418 break;
419 }
420
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200421 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100422 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
423 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200424 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100425}
426
William Lallemand74c24fb2016-11-21 17:18:36 +0100427/* Emits an encoding of the field type on 3 characters followed by a delimiter.
428 * Returns non-zero on success, 0 if the buffer is full.
429 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200430int stats_emit_field_tags(struct buffer *out, const struct field *f,
431 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100432{
433 char origin, nature, scope;
434
435 switch (field_origin(f, 0)) {
436 case FO_METRIC: origin = 'M'; break;
437 case FO_STATUS: origin = 'S'; break;
438 case FO_KEY: origin = 'K'; break;
439 case FO_CONFIG: origin = 'C'; break;
440 case FO_PRODUCT: origin = 'P'; break;
441 default: origin = '?'; break;
442 }
443
444 switch (field_nature(f, 0)) {
445 case FN_GAUGE: nature = 'G'; break;
446 case FN_LIMIT: nature = 'L'; break;
447 case FN_MIN: nature = 'm'; break;
448 case FN_MAX: nature = 'M'; break;
449 case FN_RATE: nature = 'R'; break;
450 case FN_COUNTER: nature = 'C'; break;
451 case FN_DURATION: nature = 'D'; break;
452 case FN_AGE: nature = 'A'; break;
453 case FN_TIME: nature = 'T'; break;
454 case FN_NAME: nature = 'N'; break;
455 case FN_OUTPUT: nature = 'O'; break;
456 case FN_AVG: nature = 'a'; break;
457 default: nature = '?'; break;
458 }
459
460 switch (field_scope(f, 0)) {
461 case FS_PROCESS: scope = 'P'; break;
462 case FS_SERVICE: scope = 'S'; break;
463 case FS_SYSTEM: scope = 's'; break;
464 case FS_CLUSTER: scope = 'C'; break;
465 default: scope = '?'; break;
466 }
467
468 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
469}
470
Simon Horman05ee2132017-01-04 09:37:25 +0100471/* Emits an encoding of the field type as JSON.
472 * Returns non-zero on success, 0 if the buffer is full.
473 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200474int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100475{
476 const char *origin, *nature, *scope;
477 int old_len;
478
479 switch (field_origin(f, 0)) {
480 case FO_METRIC: origin = "Metric"; break;
481 case FO_STATUS: origin = "Status"; break;
482 case FO_KEY: origin = "Key"; break;
483 case FO_CONFIG: origin = "Config"; break;
484 case FO_PRODUCT: origin = "Product"; break;
485 default: origin = "Unknown"; break;
486 }
487
488 switch (field_nature(f, 0)) {
489 case FN_GAUGE: nature = "Gauge"; break;
490 case FN_LIMIT: nature = "Limit"; break;
491 case FN_MIN: nature = "Min"; break;
492 case FN_MAX: nature = "Max"; break;
493 case FN_RATE: nature = "Rate"; break;
494 case FN_COUNTER: nature = "Counter"; break;
495 case FN_DURATION: nature = "Duration"; break;
496 case FN_AGE: nature = "Age"; break;
497 case FN_TIME: nature = "Time"; break;
498 case FN_NAME: nature = "Name"; break;
499 case FN_OUTPUT: nature = "Output"; break;
500 case FN_AVG: nature = "Avg"; break;
501 default: nature = "Unknown"; break;
502 }
503
504 switch (field_scope(f, 0)) {
505 case FS_PROCESS: scope = "Process"; break;
506 case FS_SERVICE: scope = "Service"; break;
507 case FS_SYSTEM: scope = "System"; break;
508 case FS_CLUSTER: scope = "Cluster"; break;
509 default: scope = "Unknown"; break;
510 }
511
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200512 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100513 chunk_appendf(out, "\"tags\":{"
514 "\"origin\":\"%s\","
515 "\"nature\":\"%s\","
516 "\"scope\":\"%s\""
517 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200518 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100519}
William Lallemand74c24fb2016-11-21 17:18:36 +0100520
521/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200522static int stats_dump_fields_csv(struct buffer *out,
523 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100524{
525 int field;
526
527 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
528 if (!stats_emit_raw_data_field(out, &stats[field]))
529 return 0;
530 if (!chunk_strcat(out, ","))
531 return 0;
532 }
533 chunk_strcat(out, "\n");
534 return 1;
535}
536
537/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200538static int stats_dump_fields_typed(struct buffer *out,
539 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100540{
541 int field;
542
543 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
544 if (!stats[field].type)
545 continue;
546
547 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
548 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
549 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
550 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
551 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
552 '?',
553 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
554 field, stat_field_names[field], stats[ST_F_PID].u.u32);
555
556 if (!stats_emit_field_tags(out, &stats[field], ':'))
557 return 0;
558 if (!stats_emit_typed_data_field(out, &stats[field]))
559 return 0;
560 if (!chunk_strcat(out, "\n"))
561 return 0;
562 }
563 return 1;
564}
565
Simon Horman05ee2132017-01-04 09:37:25 +0100566/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200567static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100568 const struct field *info)
569{
570 int field;
571 int started = 0;
572
573 if (!chunk_strcat(out, "["))
574 return 0;
575
576 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
577 int old_len;
578
579 if (!field_format(info, field))
580 continue;
581
582 if (started && !chunk_strcat(out, ","))
583 goto err;
584 started = 1;
585
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200586 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100587 chunk_appendf(out,
588 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
589 "\"processNum\":%u,",
590 field, info_field_names[field],
591 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200592 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100593 goto err;
594
595 if (!stats_emit_json_field_tags(out, &info[field]))
596 goto err;
597
598 if (!stats_emit_json_data_field(out, &info[field]))
599 goto err;
600
601 if (!chunk_strcat(out, "}"))
602 goto err;
603 }
604
605 if (!chunk_strcat(out, "]"))
606 goto err;
607 return 1;
608
609err:
610 chunk_reset(out);
611 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
612 return 0;
613}
614
615/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200616static int stats_dump_fields_json(struct buffer *out,
617 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100618 int first_stat)
619{
620 int field;
621 int started = 0;
622
623 if (!first_stat && !chunk_strcat(out, ","))
624 return 0;
625 if (!chunk_strcat(out, "["))
626 return 0;
627
628 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
629 const char *obj_type;
630 int old_len;
631
632 if (!stats[field].type)
633 continue;
634
635 if (started && !chunk_strcat(out, ","))
636 goto err;
637 started = 1;
638
639 switch (stats[ST_F_TYPE].u.u32) {
640 case STATS_TYPE_FE: obj_type = "Frontend"; break;
641 case STATS_TYPE_BE: obj_type = "Backend"; break;
642 case STATS_TYPE_SO: obj_type = "Listener"; break;
643 case STATS_TYPE_SV: obj_type = "Server"; break;
644 default: obj_type = "Unknown"; break;
645 }
646
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200647 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100648 chunk_appendf(out,
649 "{"
650 "\"objType\":\"%s\","
651 "\"proxyId\":%d,"
652 "\"id\":%d,"
653 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
654 "\"processNum\":%u,",
655 obj_type, stats[ST_F_IID].u.u32,
656 stats[ST_F_SID].u.u32, field,
657 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200658 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100659 goto err;
660
661 if (!stats_emit_json_field_tags(out, &stats[field]))
662 goto err;
663
664 if (!stats_emit_json_data_field(out, &stats[field]))
665 goto err;
666
667 if (!chunk_strcat(out, "}"))
668 goto err;
669 }
670
671 if (!chunk_strcat(out, "]"))
672 goto err;
673
674 return 1;
675
676err:
677 chunk_reset(out);
678 if (!first_stat)
679 chunk_strcat(out, ",");
680 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
681 return 0;
682}
683
William Lallemand74c24fb2016-11-21 17:18:36 +0100684/* Dump all fields from <stats> into <out> using the HTML format. A column is
685 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
686 * are provided if ST_SHLGNDS is present in <flags>.
687 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200688static int stats_dump_fields_html(struct buffer *out,
689 const struct field *stats,
690 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100691{
Willy Tarreau83061a82018-07-13 11:56:34 +0200692 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100693
694 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
695 chunk_appendf(out,
696 /* name, queue */
697 "<tr class=\"frontend\">");
698
699 if (flags & ST_SHOWADMIN) {
700 /* Column sub-heading for Enable or Disable server */
701 chunk_appendf(out, "<td></td>");
702 }
703
704 chunk_appendf(out,
705 "<td class=ac>"
706 "<a name=\"%s/Frontend\"></a>"
707 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
708 "<td colspan=3></td>"
709 "",
710 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
711
712 chunk_appendf(out,
713 /* sessions rate : current */
714 "<td><u>%s<div class=tips><table class=det>"
715 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
716 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
717 "",
718 U2H(stats[ST_F_RATE].u.u32),
719 U2H(stats[ST_F_CONN_RATE].u.u32),
720 U2H(stats[ST_F_RATE].u.u32));
721
722 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
723 chunk_appendf(out,
724 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
725 U2H(stats[ST_F_REQ_RATE].u.u32));
726
727 chunk_appendf(out,
728 "</table></div></u></td>"
729 /* sessions rate : max */
730 "<td><u>%s<div class=tips><table class=det>"
731 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
732 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
733 "",
734 U2H(stats[ST_F_RATE_MAX].u.u32),
735 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
736 U2H(stats[ST_F_RATE_MAX].u.u32));
737
738 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
739 chunk_appendf(out,
740 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
741 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
742
743 chunk_appendf(out,
744 "</table></div></u></td>"
745 /* sessions rate : limit */
746 "<td>%s</td>",
747 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
748
749 chunk_appendf(out,
750 /* sessions: current, max, limit, total */
751 "<td>%s</td><td>%s</td><td>%s</td>"
752 "<td><u>%s<div class=tips><table class=det>"
753 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
754 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
755 "",
756 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
757 U2H(stats[ST_F_STOT].u.u64),
758 U2H(stats[ST_F_CONN_TOT].u.u64),
759 U2H(stats[ST_F_STOT].u.u64));
760
761 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
762 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
763 chunk_appendf(out,
764 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
765 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
766 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
767 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
768 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
769 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
770 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
771 "<tr><th>- other responses:</th><td>%s</td></tr>"
772 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100773 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
774 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200775 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100776 "",
777 U2H(stats[ST_F_REQ_TOT].u.u64),
778 U2H(stats[ST_F_HRSP_1XX].u.u64),
779 U2H(stats[ST_F_HRSP_2XX].u.u64),
780 U2H(stats[ST_F_COMP_RSP].u.u64),
781 stats[ST_F_HRSP_2XX].u.u64 ?
782 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
783 U2H(stats[ST_F_HRSP_3XX].u.u64),
784 U2H(stats[ST_F_HRSP_4XX].u.u64),
785 U2H(stats[ST_F_HRSP_5XX].u.u64),
786 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200787 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100788 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
789 U2H(stats[ST_F_CACHE_HITS].u.u64),
790 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
791 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200792 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100793 }
794
795 chunk_appendf(out,
796 "</table></div></u></td>"
797 /* sessions: lbtot, lastsess */
798 "<td></td><td></td>"
799 /* bytes : in */
800 "<td>%s</td>"
801 "",
802 U2H(stats[ST_F_BIN].u.u64));
803
804 chunk_appendf(out,
805 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
806 "<td>%s%s<div class=tips><table class=det>"
807 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
808 "<tr><th>Compression in:</th><td>%s</td></tr>"
809 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
810 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
811 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
812 "</table></div>%s</td>",
813 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
814 U2H(stats[ST_F_BOUT].u.u64),
815 U2H(stats[ST_F_BOUT].u.u64),
816 U2H(stats[ST_F_COMP_IN].u.u64),
817 U2H(stats[ST_F_COMP_OUT].u.u64),
818 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
819 U2H(stats[ST_F_COMP_BYP].u.u64),
820 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
821 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,
822 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
823
824 chunk_appendf(out,
825 /* denied: req, resp */
826 "<td>%s</td><td>%s</td>"
827 /* errors : request, connect, response */
828 "<td>%s</td><td></td><td></td>"
829 /* warnings: retries, redispatches */
830 "<td></td><td></td>"
831 /* server status : reflect frontend status */
832 "<td class=ac>%s</td>"
833 /* rest of server: nothing */
834 "<td class=ac colspan=8></td></tr>"
835 "",
836 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
837 U2H(stats[ST_F_EREQ].u.u64),
838 field_str(stats, ST_F_STATUS));
839 }
840 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
841 chunk_appendf(out, "<tr class=socket>");
842 if (flags & ST_SHOWADMIN) {
843 /* Column sub-heading for Enable or Disable server */
844 chunk_appendf(out, "<td></td>");
845 }
846
847 chunk_appendf(out,
848 /* frontend name, listener name */
849 "<td class=ac><a name=\"%s/+%s\"></a>%s"
850 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
851 "",
852 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
853 (flags & ST_SHLGNDS)?"<u>":"",
854 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
855
856 if (flags & ST_SHLGNDS) {
857 chunk_appendf(out, "<div class=tips>");
858
859 if (isdigit(*field_str(stats, ST_F_ADDR)))
860 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
861 else if (*field_str(stats, ST_F_ADDR) == '[')
862 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
863 else if (*field_str(stats, ST_F_ADDR))
864 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
865
866 /* id */
867 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
868 }
869
870 chunk_appendf(out,
871 /* queue */
872 "%s</td><td colspan=3></td>"
873 /* sessions rate: current, max, limit */
874 "<td colspan=3>&nbsp;</td>"
875 /* sessions: current, max, limit, total, lbtot, lastsess */
876 "<td>%s</td><td>%s</td><td>%s</td>"
877 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
878 /* bytes: in, out */
879 "<td>%s</td><td>%s</td>"
880 "",
881 (flags & ST_SHLGNDS)?"</u>":"",
882 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
883 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
884
885 chunk_appendf(out,
886 /* denied: req, resp */
887 "<td>%s</td><td>%s</td>"
888 /* errors: request, connect, response */
889 "<td>%s</td><td></td><td></td>"
890 /* warnings: retries, redispatches */
891 "<td></td><td></td>"
892 /* server status: reflect listener status */
893 "<td class=ac>%s</td>"
894 /* rest of server: nothing */
895 "<td class=ac colspan=8></td></tr>"
896 "",
897 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
898 U2H(stats[ST_F_EREQ].u.u64),
899 field_str(stats, ST_F_STATUS));
900 }
901 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
902 const char *style;
903
904 /* determine the style to use depending on the server's state,
905 * its health and weight. There isn't a 1-to-1 mapping between
906 * state and styles for the cases where the server is (still)
907 * up. The reason is that we don't want to report nolb and
908 * drain with the same color.
909 */
910
911 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
912 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
913 style = "down";
914 }
915 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
916 style = "going_up";
917 }
Daniel Corbett90e2d312020-03-28 12:35:50 -0400918 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
919 style = "draining";
920 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100921 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
922 style = "going_down";
923 }
924 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
925 style = "nolb";
926 }
927 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
928 style = "no_check";
929 }
930 else if (!stats[ST_F_CHKFAIL].type ||
931 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
932 /* no check or max health = UP */
933 if (stats[ST_F_WEIGHT].u.u32)
934 style = "up";
935 else
936 style = "draining";
937 }
938 else {
939 style = "going_down";
940 }
941
Willy Tarreauf3109612020-08-11 10:26:36 +0200942 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +0100943 chunk_appendf(out, "<tr class=\"maintain\">");
944 else
945 chunk_appendf(out,
946 "<tr class=\"%s_%s\">",
947 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
948
949
950 if (flags & ST_SHOWADMIN)
951 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000952 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
953 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100954 field_str(stats, ST_F_SVNAME));
955
956 chunk_appendf(out,
957 "<td class=ac><a name=\"%s/%s\"></a>%s"
958 "<a class=lfsb href=\"#%s/%s\">%s</a>"
959 "",
960 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
961 (flags & ST_SHLGNDS) ? "<u>" : "",
962 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
963
964 if (flags & ST_SHLGNDS) {
965 chunk_appendf(out, "<div class=tips>");
966
967 if (isdigit(*field_str(stats, ST_F_ADDR)))
968 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
969 else if (*field_str(stats, ST_F_ADDR) == '[')
970 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
971 else if (*field_str(stats, ST_F_ADDR))
972 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
973
974 /* id */
975 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
976
977 /* cookie */
978 if (stats[ST_F_COOKIE].type) {
979 chunk_appendf(out, ", cookie: '");
980 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
981 chunk_htmlencode(out, &src);
982 chunk_appendf(out, "'");
983 }
984
985 chunk_appendf(out, "</div>");
986 }
987
988 chunk_appendf(out,
989 /* queue : current, max, limit */
990 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
991 /* sessions rate : current, max, limit */
992 "<td>%s</td><td>%s</td><td></td>"
993 "",
994 (flags & ST_SHLGNDS) ? "</u>" : "",
995 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
996 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
997
998 chunk_appendf(out,
999 /* sessions: current, max, limit, total */
Willy Tarreau0ff699e2019-09-08 09:24:56 +02001000 "<td><u>%s<div class=tips>"
1001 "<table class=det>"
1002 "<tr><th>Current active connections:</th><td>%s</td></tr>"
1003 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
1004 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1005 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1006 "</table></div></u>"
1007 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001008 "<td><u>%s<div class=tips><table class=det>"
1009 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1010 "",
Willy Tarreau0ff699e2019-09-08 09:24:56 +02001011 U2H(stats[ST_F_SCUR].u.u32),
1012 U2H(stats[ST_F_SCUR].u.u32),
1013 U2H(stats[ST_F_SRV_ICUR].u.u32),
1014 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1015 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1016 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001017 U2H(stats[ST_F_STOT].u.u64),
1018 U2H(stats[ST_F_STOT].u.u64));
1019
1020 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1021 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1022 unsigned long long tot;
1023
1024 tot = stats[ST_F_HRSP_OTHER].u.u64;
1025 tot += stats[ST_F_HRSP_1XX].u.u64;
1026 tot += stats[ST_F_HRSP_2XX].u.u64;
1027 tot += stats[ST_F_HRSP_3XX].u.u64;
1028 tot += stats[ST_F_HRSP_4XX].u.u64;
1029 tot += stats[ST_F_HRSP_5XX].u.u64;
1030
1031 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001032 "<tr><th>New connections:</th><td>%s</td></tr>"
1033 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001034 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1035 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1036 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1037 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1038 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1039 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1040 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001041 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001042 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001043 U2H(stats[ST_F_CONNECT].u.u64),
1044 U2H(stats[ST_F_REUSE].u.u64),
1045 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1046 (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 +01001047 U2H(tot),
1048 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1049 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1050 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1051 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1052 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 +02001053 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1054 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001055 }
1056
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001057 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1058 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1059 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1060 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1061 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001062 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001063 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1064 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1065 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1066 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001067
1068 chunk_appendf(out,
1069 "</table></div></u></td>"
1070 /* sessions: lbtot, last */
1071 "<td>%s</td><td>%s</td>",
1072 U2H(stats[ST_F_LBTOT].u.u64),
1073 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1074
1075 chunk_appendf(out,
1076 /* bytes : in, out */
1077 "<td>%s</td><td>%s</td>"
1078 /* denied: req, resp */
1079 "<td></td><td>%s</td>"
1080 /* errors : request, connect */
1081 "<td></td><td>%s</td>"
1082 /* errors : response */
1083 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1084 /* warnings: retries, redispatches */
1085 "<td>%lld</td><td>%lld</td>"
1086 "",
1087 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1088 U2H(stats[ST_F_DRESP].u.u64),
1089 U2H(stats[ST_F_ECON].u.u64),
1090 U2H(stats[ST_F_ERESP].u.u64),
1091 (long long)stats[ST_F_CLI_ABRT].u.u64,
1092 (long long)stats[ST_F_SRV_ABRT].u.u64,
1093 (long long)stats[ST_F_WRETR].u.u64,
1094 (long long)stats[ST_F_WREDIS].u.u64);
1095
1096 /* status, last change */
1097 chunk_appendf(out, "<td class=ac>");
1098
1099 /* FIXME!!!!
1100 * LASTCHG should contain the last change for *this* server and must be computed
1101 * properly above, as was done below, ie: this server if maint, otherwise ref server
1102 * if tracking. Note that ref is either local or remote depending on tracking.
1103 */
1104
1105
Willy Tarreauf3109612020-08-11 10:26:36 +02001106 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001107 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1108 }
Willy Tarreauf3109612020-08-11 10:26:36 +02001109 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001110 chunk_strcat(out, "<i>no check</i>");
1111 }
1112 else {
1113 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
Willy Tarreauf3109612020-08-11 10:26:36 +02001114 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001115 if (stats[ST_F_CHECK_HEALTH].u.u32)
1116 chunk_strcat(out, " &uarr;");
1117 }
1118 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1119 chunk_strcat(out, " &darr;");
1120 }
1121
Willy Tarreauf3109612020-08-11 10:26:36 +02001122 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001123 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1124 chunk_appendf(out,
1125 "</td><td class=ac><u> %s",
1126 field_str(stats, ST_F_AGENT_STATUS));
1127
1128 if (stats[ST_F_AGENT_CODE].type)
1129 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1130
1131 if (stats[ST_F_AGENT_DURATION].type)
1132 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1133
1134 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1135
1136 if (*field_str(stats, ST_F_LAST_AGT)) {
1137 chunk_appendf(out, ": ");
1138 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1139 chunk_htmlencode(out, &src);
1140 }
1141 chunk_appendf(out, "</div></u>");
1142 }
1143 else if (stats[ST_F_CHECK_STATUS].type) {
1144 chunk_appendf(out,
1145 "</td><td class=ac><u> %s",
1146 field_str(stats, ST_F_CHECK_STATUS));
1147
1148 if (stats[ST_F_CHECK_CODE].type)
1149 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1150
1151 if (stats[ST_F_CHECK_DURATION].type)
1152 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1153
1154 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1155
1156 if (*field_str(stats, ST_F_LAST_CHK)) {
1157 chunk_appendf(out, ": ");
1158 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1159 chunk_htmlencode(out, &src);
1160 }
1161 chunk_appendf(out, "</div></u>");
1162 }
1163 else
1164 chunk_appendf(out, "</td><td>");
1165
1166 chunk_appendf(out,
1167 /* weight */
1168 "</td><td class=ac>%d</td>"
1169 /* act, bck */
1170 "<td class=ac>%s</td><td class=ac>%s</td>"
1171 "",
1172 stats[ST_F_WEIGHT].u.u32,
1173 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1174 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1175
1176 /* check failures: unique, fatal, down time */
1177 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1178 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1179 }
1180 else if (stats[ST_F_CHKFAIL].type) {
1181 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1182
1183 if (stats[ST_F_HANAFAIL].type)
1184 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1185
1186 chunk_appendf(out,
1187 "<div class=tips>Failed Health Checks%s</div></u></td>"
1188 "<td>%lld</td><td>%s</td>"
1189 "",
1190 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1191 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1192 }
1193 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1194 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1195 chunk_appendf(out,
1196 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1197 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1198 }
1199 else
1200 chunk_appendf(out, "<td colspan=3></td>");
1201
1202 /* throttle */
1203 if (stats[ST_F_THROTTLE].type)
1204 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1205 else
1206 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1207 }
1208 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1209 chunk_appendf(out, "<tr class=\"backend\">");
1210 if (flags & ST_SHOWADMIN) {
1211 /* Column sub-heading for Enable or Disable server */
1212 chunk_appendf(out, "<td></td>");
1213 }
1214 chunk_appendf(out,
1215 "<td class=ac>"
1216 /* name */
1217 "%s<a name=\"%s/Backend\"></a>"
1218 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1219 "",
1220 (flags & ST_SHLGNDS)?"<u>":"",
1221 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1222
1223 if (flags & ST_SHLGNDS) {
1224 /* balancing */
1225 chunk_appendf(out, "<div class=tips>balancing: %s",
1226 field_str(stats, ST_F_ALGO));
1227
1228 /* cookie */
1229 if (stats[ST_F_COOKIE].type) {
1230 chunk_appendf(out, ", cookie: '");
1231 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1232 chunk_htmlencode(out, &src);
1233 chunk_appendf(out, "'");
1234 }
1235 chunk_appendf(out, "</div>");
1236 }
1237
1238 chunk_appendf(out,
1239 "%s</td>"
1240 /* queue : current, max */
1241 "<td>%s</td><td>%s</td><td></td>"
1242 /* sessions rate : current, max, limit */
1243 "<td>%s</td><td>%s</td><td></td>"
1244 "",
1245 (flags & ST_SHLGNDS)?"</u>":"",
1246 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1247 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1248
1249 chunk_appendf(out,
1250 /* sessions: current, max, limit, total */
1251 "<td>%s</td><td>%s</td><td>%s</td>"
1252 "<td><u>%s<div class=tips><table class=det>"
1253 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1254 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001255 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 +01001256 U2H(stats[ST_F_STOT].u.u64),
1257 U2H(stats[ST_F_STOT].u.u64));
1258
1259 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1260 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1261 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001262 "<tr><th>New connections:</th><td>%s</td></tr>"
1263 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001264 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1265 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1266 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1267 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1268 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1269 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1270 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1271 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001272 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1273 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001274 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001275 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001276 U2H(stats[ST_F_CONNECT].u.u64),
1277 U2H(stats[ST_F_REUSE].u.u64),
1278 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1279 (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 +01001280 U2H(stats[ST_F_REQ_TOT].u.u64),
1281 U2H(stats[ST_F_HRSP_1XX].u.u64),
1282 U2H(stats[ST_F_HRSP_2XX].u.u64),
1283 U2H(stats[ST_F_COMP_RSP].u.u64),
1284 stats[ST_F_HRSP_2XX].u.u64 ?
1285 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1286 U2H(stats[ST_F_HRSP_3XX].u.u64),
1287 U2H(stats[ST_F_HRSP_4XX].u.u64),
1288 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001289 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001290 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1291 U2H(stats[ST_F_CACHE_HITS].u.u64),
1292 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1293 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001294 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001295 }
1296
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001297 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1298 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1299 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1300 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1301 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001302 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001303 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1304 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1305 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1306 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001307
1308 chunk_appendf(out,
1309 "</table></div></u></td>"
1310 /* sessions: lbtot, last */
1311 "<td>%s</td><td>%s</td>"
1312 /* bytes: in */
1313 "<td>%s</td>"
1314 "",
1315 U2H(stats[ST_F_LBTOT].u.u64),
1316 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1317 U2H(stats[ST_F_BIN].u.u64));
1318
1319 chunk_appendf(out,
1320 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1321 "<td>%s%s<div class=tips><table class=det>"
1322 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1323 "<tr><th>Compression in:</th><td>%s</td></tr>"
1324 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1325 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1326 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1327 "</table></div>%s</td>",
1328 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1329 U2H(stats[ST_F_BOUT].u.u64),
1330 U2H(stats[ST_F_BOUT].u.u64),
1331 U2H(stats[ST_F_COMP_IN].u.u64),
1332 U2H(stats[ST_F_COMP_OUT].u.u64),
1333 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1334 U2H(stats[ST_F_COMP_BYP].u.u64),
1335 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1336 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,
1337 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1338
1339 chunk_appendf(out,
1340 /* denied: req, resp */
1341 "<td>%s</td><td>%s</td>"
1342 /* errors : request, connect */
1343 "<td></td><td>%s</td>"
1344 /* errors : response */
1345 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1346 /* warnings: retries, redispatches */
1347 "<td>%lld</td><td>%lld</td>"
1348 /* backend status: reflect backend status (up/down): we display UP
1349 * if the backend has known working servers or if it has no server at
1350 * all (eg: for stats). Then we display the total weight, number of
1351 * active and backups. */
1352 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1353 "<td class=ac>%d</td><td class=ac>%d</td>"
1354 "",
1355 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1356 U2H(stats[ST_F_ECON].u.u64),
1357 U2H(stats[ST_F_ERESP].u.u64),
1358 (long long)stats[ST_F_CLI_ABRT].u.u64,
1359 (long long)stats[ST_F_SRV_ABRT].u.u64,
1360 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1361 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1362 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1363 stats[ST_F_WEIGHT].u.u32,
1364 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1365
1366 chunk_appendf(out,
1367 /* rest of backend: nothing, down transitions, total downtime, throttle */
1368 "<td class=ac>&nbsp;</td><td>%d</td>"
1369 "<td>%s</td>"
1370 "<td></td>"
1371 "</tr>",
1372 stats[ST_F_CHKDOWN].u.u32,
1373 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1374 }
1375 return 1;
1376}
1377
1378int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1379{
Simon Horman05ee2132017-01-04 09:37:25 +01001380 int ret;
1381
William Lallemand74c24fb2016-11-21 17:18:36 +01001382 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1383 flags |= ST_SHOWADMIN;
1384
1385 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001386 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001387 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001388 ret = stats_dump_fields_typed(&trash, stats);
1389 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1390 ret = stats_dump_fields_json(&trash, stats,
1391 !(appctx->ctx.stats.flags &
1392 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001393 else
Simon Horman05ee2132017-01-04 09:37:25 +01001394 ret = stats_dump_fields_csv(&trash, stats);
1395
1396 if (ret)
1397 appctx->ctx.stats.flags |= STAT_STARTED;
1398
1399 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001400}
1401
1402/* Fill <stats> with the frontend statistics. <stats> is
1403 * preallocated array of length <len>. The length of the array
1404 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1405 * this value, the function returns 0, otherwise, it returns 1.
1406 */
1407int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1408{
1409 if (len < ST_F_TOTAL_FIELDS)
1410 return 0;
1411
1412 memset(stats, 0, sizeof(*stats) * len);
1413
1414 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1415 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1416 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1417 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1418 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1419 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1420 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1421 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1422 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1423 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1424 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1425 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1426 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1427 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1428 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1429 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1430 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1431 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1432 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1433 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1434 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1435 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001436 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001437
1438 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1439 if (px->mode == PR_MODE_HTTP) {
1440 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1441 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1442 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1443 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1444 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1445 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1446 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001447 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1448 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001449 }
1450
1451 /* requests : req_rate, req_rate_max, req_tot, */
1452 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1453 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1454 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1455
1456 /* compression: in, out, bypassed, responses */
1457 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1458 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1459 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1460 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1461
1462 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1463 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1464 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1465 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1466
1467 return 1;
1468}
1469
1470/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1471 * the state from stream interface <si>. The caller is responsible for clearing
1472 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1473 */
1474static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1475{
1476 struct appctx *appctx = __objt_appctx(si->end);
1477
1478 if (!(px->cap & PR_CAP_FE))
1479 return 0;
1480
1481 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1482 return 0;
1483
1484 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1485 return 0;
1486
1487 return stats_dump_one_line(stats, 0, px, appctx);
1488}
1489
1490/* Fill <stats> with the listener statistics. <stats> is
1491 * preallocated array of length <len>. The length of the array
1492 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1493 * then this value, the function returns 0, otherwise, it
1494 * returns 1. <flags> can take the value ST_SHLGNDS.
1495 */
1496int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1497 struct field *stats, int len)
1498{
Willy Tarreau83061a82018-07-13 11:56:34 +02001499 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001500
1501 if (len < ST_F_TOTAL_FIELDS)
1502 return 0;
1503
1504 if (!l->counters)
1505 return 0;
1506
1507 chunk_reset(out);
1508 memset(stats, 0, sizeof(*stats) * len);
1509
1510 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1511 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1512 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1513 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1514 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1515 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1516 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1517 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1518 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1519 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1520 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1521 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1522 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1523 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001524 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (!l->maxconn || l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
William Lallemand74c24fb2016-11-21 17:18:36 +01001525 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1526 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1527 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1528 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001529 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001530
1531 if (flags & ST_SHLGNDS) {
1532 char str[INET6_ADDRSTRLEN];
1533 int port;
1534
1535 port = get_host_port(&l->addr);
1536 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1537 case AF_INET:
1538 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1539 chunk_appendf(out, "%s:%d", str, port);
1540 break;
1541 case AF_INET6:
1542 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1543 chunk_appendf(out, "[%s]:%d", str, port);
1544 break;
1545 case AF_UNIX:
1546 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1547 break;
1548 case -1:
1549 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1550 chunk_strcat(out, strerror(errno));
1551 break;
1552 default: /* address family not supported */
1553 break;
1554 }
1555 }
1556
1557 return 1;
1558}
1559
1560/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1561 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1562 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1563 * otherwise.
1564 */
1565static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1566{
1567 struct appctx *appctx = __objt_appctx(si->end);
1568
1569 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1570 return 0;
1571
1572 return stats_dump_one_line(stats, flags, px, appctx);
1573}
1574
1575enum srv_stats_state {
1576 SRV_STATS_STATE_DOWN = 0,
1577 SRV_STATS_STATE_DOWN_AGENT,
1578 SRV_STATS_STATE_GOING_UP,
1579 SRV_STATS_STATE_UP_GOING_DOWN,
1580 SRV_STATS_STATE_UP,
1581 SRV_STATS_STATE_NOLB_GOING_DOWN,
1582 SRV_STATS_STATE_NOLB,
1583 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1584 SRV_STATS_STATE_DRAIN,
1585 SRV_STATS_STATE_DRAIN_AGENT,
1586 SRV_STATS_STATE_NO_CHECK,
1587
1588 SRV_STATS_STATE_COUNT, /* Must be last */
1589};
1590
1591static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1592 [SRV_STATS_STATE_DOWN] = "DOWN",
1593 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1594 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1595 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1596 [SRV_STATS_STATE_UP] = "UP",
1597 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1598 [SRV_STATS_STATE_NOLB] = "NOLB",
1599 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1600 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1601 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1602 [SRV_STATS_STATE_NO_CHECK] = "no check"
1603};
1604
1605/* Fill <stats> with the server statistics. <stats> is
1606 * preallocated array of length <len>. The length of the array
1607 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1608 * then this value, the function returns 0, otherwise, it
1609 * returns 1. <flags> can take the value ST_SHLGNDS.
1610 */
1611int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1612 struct field *stats, int len)
1613{
1614 struct server *via, *ref;
1615 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001616 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001617 enum srv_stats_state state;
1618 char *fld_status;
1619
1620 if (len < ST_F_TOTAL_FIELDS)
1621 return 0;
1622
1623 memset(stats, 0, sizeof(*stats) * len);
1624
1625 /* we have "via" which is the tracked server as described in the configuration,
1626 * and "ref" which is the checked server and the end of the chain.
1627 */
1628 via = sv->track ? sv->track : sv;
1629 ref = via;
1630 while (ref->track)
1631 ref = ref->track;
1632
Emeric Brun52a91d32017-08-31 14:41:55 +02001633 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001634 if ((ref->check.state & CHK_ST_ENABLED) &&
1635 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1636 state = SRV_STATS_STATE_UP_GOING_DOWN;
1637 } else {
1638 state = SRV_STATS_STATE_UP;
1639 }
1640
Emeric Brun52a91d32017-08-31 14:41:55 +02001641 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001642 if (ref->agent.state & CHK_ST_ENABLED)
1643 state = SRV_STATS_STATE_DRAIN_AGENT;
1644 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1645 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1646 else
1647 state = SRV_STATS_STATE_DRAIN;
1648 }
1649
1650 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1651 state = SRV_STATS_STATE_NO_CHECK;
1652 }
1653 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001654 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001655 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1656 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1657 state = SRV_STATS_STATE_NOLB;
1658 } else {
1659 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1660 }
1661 }
1662 else { /* stopped */
1663 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1664 state = SRV_STATS_STATE_DOWN_AGENT;
1665 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1666 state = SRV_STATS_STATE_DOWN; /* DOWN */
1667 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1668 state = SRV_STATS_STATE_GOING_UP;
1669 } else {
1670 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1671 }
1672 }
1673
1674 chunk_reset(out);
1675
1676 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1677 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1678 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1679 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1680 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1681 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1682 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1683
1684 if (sv->maxconn)
1685 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1686
Willy Tarreau0ff699e2019-09-08 09:24:56 +02001687 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1688 if (sv->max_idle_conns != -1)
1689 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1690
William Lallemand74c24fb2016-11-21 17:18:36 +01001691 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1692 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1693 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1694 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1695 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1696 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1697 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1698 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001699 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001700 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1701 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001702
1703 /* status */
1704 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001705 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001706 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001707 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001708 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001709 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001710 chunk_appendf(out, "MAINT");
1711 else
1712 chunk_appendf(out,
1713 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001714 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1715 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001716
1717 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1718 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001719 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 +01001720 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1721 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1722
1723 /* check failures: unique, fatal; last change, total downtime */
1724 if (sv->check.state & CHK_ST_ENABLED) {
1725 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1726 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1727 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1728 }
1729
1730 if (sv->maxqueue)
1731 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1732
1733 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1734 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1735 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1736
Emeric Brun52a91d32017-08-31 14:41:55 +02001737 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001738 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1739
1740 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1741
1742 if (sv->track) {
1743 char *fld_track = chunk_newstr(out);
1744
1745 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1746 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1747 }
1748
1749 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1750 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1751 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1752
1753 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1754 const char *fld_chksts;
1755
1756 fld_chksts = chunk_newstr(out);
1757 chunk_strcat(out, "* "); // for check in progress
1758 chunk_strcat(out, get_check_status_info(sv->check.status));
1759 if (!(sv->check.state & CHK_ST_INPROGRESS))
1760 fld_chksts += 2; // skip "* "
1761 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1762
1763 if (sv->check.status >= HCHK_STATUS_L57DATA)
1764 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1765
1766 if (sv->check.status >= HCHK_STATUS_CHECKED)
1767 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1768
1769 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1770 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1771 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1772 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1773 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1774 }
1775
1776 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1777 const char *fld_chksts;
1778
1779 fld_chksts = chunk_newstr(out);
1780 chunk_strcat(out, "* "); // for check in progress
1781 chunk_strcat(out, get_check_status_info(sv->agent.status));
1782 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1783 fld_chksts += 2; // skip "* "
1784 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1785
1786 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1787 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1788
1789 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1790 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1791
1792 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1793 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1794 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1795 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1796 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1797 }
1798
1799 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1800 if (px->mode == PR_MODE_HTTP) {
1801 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1802 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1803 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1804 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1805 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1806 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1807 }
1808
1809 if (ref->observe)
1810 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1811
1812 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1813 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1814 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1815
1816 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1817 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1818 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1819 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1820
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001821 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1822 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1823 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1824 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1825
William Lallemand74c24fb2016-11-21 17:18:36 +01001826 if (flags & ST_SHLGNDS) {
1827 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1828 case AF_INET:
1829 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001830 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001831 break;
1832 case AF_INET6:
1833 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001834 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001835 break;
1836 case AF_UNIX:
1837 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1838 break;
1839 case -1:
1840 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1841 chunk_strcat(out, strerror(errno));
1842 break;
1843 default: /* address family not supported */
1844 break;
1845 }
1846
1847 if (sv->cookie)
1848 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1849 }
1850
1851 return 1;
1852}
1853
1854/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1855 * from stream interface <si>, stats flags <flags>, and server state <state>.
1856 * The caller is responsible for clearing the trash if needed. Returns non-zero
1857 * if it emits anything, zero otherwise.
1858 */
1859static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1860{
1861 struct appctx *appctx = __objt_appctx(si->end);
1862
1863 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1864 return 0;
1865
1866 return stats_dump_one_line(stats, flags, px, appctx);
1867}
1868
1869/* Fill <stats> with the backend statistics. <stats> is
1870 * preallocated array of length <len>. The length of the array
1871 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1872 * then this value, the function returns 0, otherwise, it
1873 * returns 1. <flags> can take the value ST_SHLGNDS.
1874 */
1875int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1876{
1877 if (len < ST_F_TOTAL_FIELDS)
1878 return 0;
1879
1880 memset(stats, 0, sizeof(*stats) * len);
1881
1882 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1883 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1884 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1885 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1886 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001887 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001888 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1889 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1890 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1891 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1892 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1893 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1894 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1895 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1896 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1897 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1898 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001899 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001900 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1901 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001902 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1903 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1904 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1905 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1906 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1907 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1908 if (px->srv)
1909 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1910
1911 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1912 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1913 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1914 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1915 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1916 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1917 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1918
1919 if (flags & ST_SHLGNDS) {
1920 if (px->cookie_name)
1921 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1922 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1923 }
1924
1925 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1926 if (px->mode == PR_MODE_HTTP) {
1927 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1928 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1929 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1930 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1931 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1932 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1933 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001934 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1935 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001936 }
1937
1938 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1939 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1940
1941 /* compression: in, out, bypassed, responses */
1942 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1943 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1944 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1945 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1946 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1947
1948 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1949 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1950 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1951 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1952
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001953 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1954 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1955 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1956 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1957
William Lallemand74c24fb2016-11-21 17:18:36 +01001958 return 1;
1959}
1960
1961/* Dumps a line for backend <px> to the trash for and uses the state from stream
1962 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1963 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1964 */
1965static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1966{
1967 struct appctx *appctx = __objt_appctx(si->end);
1968
1969 if (!(px->cap & PR_CAP_BE))
1970 return 0;
1971
1972 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1973 return 0;
1974
1975 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1976 return 0;
1977
1978 return stats_dump_one_line(stats, flags, px, appctx);
1979}
1980
1981/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1982 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1983 * for clearing the trash if needed.
1984 */
1985static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1986{
1987 struct appctx *appctx = __objt_appctx(si->end);
1988 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1989
1990 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1991 /* A form to enable/disable this proxy servers */
1992
1993 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1994 scope_txt[0] = 0;
1995 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001996 const char *scope_ptr = stats_scope_ptr(appctx, si);
1997
William Lallemand74c24fb2016-11-21 17:18:36 +01001998 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001999 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002000 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2001 }
2002
2003 chunk_appendf(&trash,
2004 "<form method=\"post\">");
2005 }
2006
2007 /* print a new table */
2008 chunk_appendf(&trash,
2009 "<table class=\"tbl\" width=\"100%%\">\n"
2010 "<tr class=\"titre\">"
2011 "<th class=\"pxname\" width=\"10%%\">");
2012
2013 chunk_appendf(&trash,
2014 "<a name=\"%s\"></a>%s"
2015 "<a class=px href=\"#%s\">%s</a>",
2016 px->id,
2017 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2018 px->id, px->id);
2019
2020 if (uri->flags & ST_SHLGNDS) {
2021 /* cap, mode, id */
2022 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2023 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2024 px->uuid);
2025 chunk_appendf(&trash, "</div>");
2026 }
2027
2028 chunk_appendf(&trash,
2029 "%s</th>"
2030 "<th class=\"%s\" width=\"90%%\">%s</th>"
2031 "</tr>\n"
2032 "</table>\n"
2033 "<table class=\"tbl\" width=\"100%%\">\n"
2034 "<tr class=\"titre\">",
2035 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2036 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2037
2038 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2039 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00002040 chunk_appendf(&trash,
2041 "<th rowspan=2 width=1><input type=\"checkbox\" \
2042 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
2043 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2044 px->id,
2045 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01002046 }
2047
2048 chunk_appendf(&trash,
2049 "<th rowspan=2></th>"
2050 "<th colspan=3>Queue</th>"
2051 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2052 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2053 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2054 "<th colspan=9>Server</th>"
2055 "</tr>\n"
2056 "<tr class=\"titre\">"
2057 "<th>Cur</th><th>Max</th><th>Limit</th>"
2058 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2059 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2060 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2061 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2062 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2063 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2064 "<th>Thrtle</th>\n"
2065 "</tr>");
2066}
2067
2068/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2069 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2070 */
2071static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2072{
2073 struct appctx *appctx = __objt_appctx(si->end);
2074 chunk_appendf(&trash, "</table>");
2075
2076 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2077 /* close the form used to enable/disable this proxy servers */
2078 chunk_appendf(&trash,
2079 "Choose the action to perform on the checked servers : "
2080 "<select name=action>"
2081 "<option value=\"\"></option>"
2082 "<option value=\"ready\">Set state to READY</option>"
2083 "<option value=\"drain\">Set state to DRAIN</option>"
2084 "<option value=\"maint\">Set state to MAINT</option>"
2085 "<option value=\"dhlth\">Health: disable checks</option>"
2086 "<option value=\"ehlth\">Health: enable checks</option>"
2087 "<option value=\"hrunn\">Health: force UP</option>"
2088 "<option value=\"hnolb\">Health: force NOLB</option>"
2089 "<option value=\"hdown\">Health: force DOWN</option>"
2090 "<option value=\"dagent\">Agent: disable checks</option>"
2091 "<option value=\"eagent\">Agent: enable checks</option>"
2092 "<option value=\"arunn\">Agent: force UP</option>"
2093 "<option value=\"adown\">Agent: force DOWN</option>"
2094 "<option value=\"shutdown\">Kill Sessions</option>"
2095 "</select>"
2096 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2097 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2098 "</form>",
2099 px->uuid);
2100 }
2101
2102 chunk_appendf(&trash, "<p>\n");
2103}
2104
2105/*
2106 * Dumps statistics for a proxy. The output is sent to the stream interface's
2107 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2108 * buffer space, or non-zero if everything completed. This function is used
2109 * both by the CLI and the HTTP entry points, and is able to dump the output
2110 * in HTML or CSV formats. If the later, <uri> must be NULL.
2111 */
Christopher Fauletef779222018-10-31 08:47:01 +01002112int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2113 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002114{
2115 struct appctx *appctx = __objt_appctx(si->end);
2116 struct stream *s = si_strm(si);
2117 struct channel *rep = si_ic(si);
2118 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2119 struct listener *l;
2120 unsigned int flags;
2121
2122 if (uri)
2123 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002124 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002125 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2126 else
2127 flags = ST_SHNODE | ST_SHDESC;
2128
2129 chunk_reset(&trash);
2130
2131 switch (appctx->ctx.stats.px_st) {
2132 case STAT_PX_ST_INIT:
2133 /* we are on a new proxy */
2134 if (uri && uri->scope) {
2135 /* we have a limited scope, we have to check the proxy name */
2136 struct stat_scope *scope;
2137 int len;
2138
2139 len = strlen(px->id);
2140 scope = uri->scope;
2141
2142 while (scope) {
2143 /* match exact proxy name */
2144 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2145 break;
2146
2147 /* match '.' which means 'self' proxy */
2148 if (!strcmp(scope->px_id, ".") && px == s->be)
2149 break;
2150 scope = scope->next;
2151 }
2152
2153 /* proxy name not found : don't dump anything */
2154 if (scope == NULL)
2155 return 1;
2156 }
2157
2158 /* if the user has requested a limited output and the proxy
2159 * name does not match, skip it.
2160 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002161 if (appctx->ctx.stats.scope_len) {
2162 const char *scope_ptr = stats_scope_ptr(appctx, si);
2163
2164 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2165 return 1;
2166 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002167
2168 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2169 (appctx->ctx.stats.iid != -1) &&
2170 (px->uuid != appctx->ctx.stats.iid))
2171 return 1;
2172
2173 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2174 /* fall through */
2175
2176 case STAT_PX_ST_TH:
2177 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2178 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002179 if (!stats_putchk(rep, htx, &trash))
2180 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002181 }
2182
2183 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2184 /* fall through */
2185
2186 case STAT_PX_ST_FE:
2187 /* print the frontend */
2188 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002189 if (!stats_putchk(rep, htx, &trash))
2190 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002191 }
2192
2193 appctx->ctx.stats.l = px->conf.listeners.n;
2194 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2195 /* fall through */
2196
2197 case STAT_PX_ST_LI:
2198 /* stats.l has been initialized above */
2199 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002200 if (htx) {
2201 if (htx_almost_full(htx))
2202 goto full;
2203 }
2204 else {
2205 if (buffer_almost_full(&rep->buf))
2206 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002207 }
2208
2209 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2210 if (!l->counters)
2211 continue;
2212
2213 if (appctx->ctx.stats.flags & STAT_BOUND) {
2214 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2215 break;
2216
2217 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2218 continue;
2219 }
2220
2221 /* print the frontend */
2222 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002223 if (!stats_putchk(rep, htx, &trash))
2224 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002225 }
2226 }
2227
2228 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2229 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2230 /* fall through */
2231
2232 case STAT_PX_ST_SV:
2233 /* stats.sv has been initialized above */
2234 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002235 if (htx) {
2236 if (htx_almost_full(htx))
2237 goto full;
2238 }
2239 else {
2240 if (buffer_almost_full(&rep->buf))
2241 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002242 }
2243
2244 sv = appctx->ctx.stats.sv;
2245
2246 if (appctx->ctx.stats.flags & STAT_BOUND) {
2247 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2248 break;
2249
2250 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2251 continue;
2252 }
2253
2254 svs = sv;
2255 while (svs->track)
2256 svs = svs->track;
2257
2258 /* do not report servers which are DOWN and not changing state */
2259 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002260 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2261 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002262 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2263 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2264 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2265 continue;
2266 }
2267
2268 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002269 if (!stats_putchk(rep, htx, &trash))
2270 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002271 }
2272 } /* for sv */
2273
2274 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2275 /* fall through */
2276
2277 case STAT_PX_ST_BE:
2278 /* print the backend */
2279 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002280 if (!stats_putchk(rep, htx, &trash))
2281 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002282 }
2283
2284 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2285 /* fall through */
2286
2287 case STAT_PX_ST_END:
2288 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2289 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002290 if (!stats_putchk(rep, htx, &trash))
2291 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002292 }
2293
2294 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2295 /* fall through */
2296
2297 case STAT_PX_ST_FIN:
2298 return 1;
2299
2300 default:
2301 /* unknown state, we should put an abort() here ! */
2302 return 1;
2303 }
Christopher Fauletef779222018-10-31 08:47:01 +01002304
2305 full:
2306 si_rx_room_blk(si);
2307 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002308}
2309
2310/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2311 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2312 */
2313static void stats_dump_html_head(struct uri_auth *uri)
2314{
2315 /* WARNING! This must fit in the first buffer !!! */
2316 chunk_appendf(&trash,
2317 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2318 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2319 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2320 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2321 "<style type=\"text/css\"><!--\n"
2322 "body {"
2323 " font-family: arial, helvetica, sans-serif;"
2324 " font-size: 12px;"
2325 " font-weight: normal;"
2326 " color: black;"
2327 " background: white;"
2328 "}\n"
2329 "th,td {"
2330 " font-size: 10px;"
2331 "}\n"
2332 "h1 {"
2333 " font-size: x-large;"
2334 " margin-bottom: 0.5em;"
2335 "}\n"
2336 "h2 {"
2337 " font-family: helvetica, arial;"
2338 " font-size: x-large;"
2339 " font-weight: bold;"
2340 " font-style: italic;"
2341 " color: #6020a0;"
2342 " margin-top: 0em;"
2343 " margin-bottom: 0em;"
2344 "}\n"
2345 "h3 {"
2346 " font-family: helvetica, arial;"
2347 " font-size: 16px;"
2348 " font-weight: bold;"
2349 " color: #b00040;"
2350 " background: #e8e8d0;"
2351 " margin-top: 0em;"
2352 " margin-bottom: 0em;"
2353 "}\n"
2354 "li {"
2355 " margin-top: 0.25em;"
2356 " margin-right: 2em;"
2357 "}\n"
2358 ".hr {margin-top: 0.25em;"
2359 " border-color: black;"
2360 " border-bottom-style: solid;"
2361 "}\n"
2362 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2363 ".total {background: #20D0D0;color: #ffff80;}\n"
2364 ".frontend {background: #e8e8d0;}\n"
2365 ".socket {background: #d0d0d0;}\n"
2366 ".backend {background: #e8e8d0;}\n"
2367 ".active_down {background: #ff9090;}\n"
2368 ".active_going_up {background: #ffd020;}\n"
2369 ".active_going_down {background: #ffffa0;}\n"
2370 ".active_up {background: #c0ffc0;}\n"
2371 ".active_nolb {background: #20a0ff;}\n"
2372 ".active_draining {background: #20a0FF;}\n"
2373 ".active_no_check {background: #e0e0e0;}\n"
2374 ".backup_down {background: #ff9090;}\n"
2375 ".backup_going_up {background: #ff80ff;}\n"
2376 ".backup_going_down {background: #c060ff;}\n"
2377 ".backup_up {background: #b0d0ff;}\n"
2378 ".backup_nolb {background: #90b0e0;}\n"
2379 ".backup_draining {background: #cc9900;}\n"
2380 ".backup_no_check {background: #e0e0e0;}\n"
2381 ".maintain {background: #c07820;}\n"
2382 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2383 "\n"
2384 "a.px:link {color: #ffff40; text-decoration: none;}"
2385 "a.px:visited {color: #ffff40; text-decoration: none;}"
2386 "a.px:hover {color: #ffffff; text-decoration: none;}"
2387 "a.lfsb:link {color: #000000; text-decoration: none;}"
2388 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2389 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2390 "\n"
2391 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2392 "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"
2393 "table.tbl td.ac { text-align: center;}\n"
2394 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2395 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2396 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2397 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2398 "\n"
2399 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2400 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2401 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2402 "table.det { border-collapse: collapse; border-style: none; }\n"
2403 "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"
2404 "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"
2405 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2406 "div.tips {\n"
2407 " display:block;\n"
2408 " visibility:hidden;\n"
2409 " z-index:2147483647;\n"
2410 " position:absolute;\n"
2411 " padding:2px 4px 3px;\n"
2412 " background:#f0f060; color:#000000;\n"
2413 " border:1px solid #7040c0;\n"
2414 " white-space:nowrap;\n"
2415 " font-style:normal;font-size:11px;font-weight:normal;\n"
2416 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2417 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2418 "}\n"
2419 "u:hover div.tips {visibility:visible;}\n"
2420 "-->\n"
2421 "</style></head>\n",
2422 (uri->flags & ST_SHNODE) ? " on " : "",
2423 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2424 );
2425}
2426
2427/* Dumps the HTML stats information block to the trash for and uses the state from
2428 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2429 * for clearing the trash if needed.
2430 */
2431static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2432{
2433 struct appctx *appctx = __objt_appctx(si->end);
2434 unsigned int up = (now.tv_sec - start_date.tv_sec);
2435 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002436 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002437 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2438
2439 /* Turn the bytes per second to bits per second and take care of the
2440 * usual ethernet overhead in order to help figure how far we are from
2441 * interface saturation since it's the only case which usually matters.
2442 * For this we count the total size of an Ethernet frame on the wire
2443 * including preamble and IFG (1538) for the largest TCP segment it
2444 * transports (1448 with TCP timestamps). This is not valid for smaller
2445 * packets (under-estimated), but it gives a reasonably accurate
2446 * estimation of how far we are from uplink saturation.
2447 */
2448 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002449
2450 /* WARNING! this has to fit the first packet too.
2451 * We are around 3.5 kB, add adding entries will
2452 * become tricky if we want to support 4kB buffers !
2453 */
2454 chunk_appendf(&trash,
2455 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2456 PRODUCT_NAME "%s</a></h1>\n"
2457 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2458 "<hr width=\"100%%\" class=\"hr\">\n"
2459 "<h3>&gt; General process information</h3>\n"
2460 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002461 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002462 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2463 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2464 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002465 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002466 "Running tasks: %d/%d; idle = %d %%<br>\n"
2467 "</td><td align=\"center\" nowrap>\n"
2468 "<table class=\"lgd\"><tr>\n"
2469 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2470 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2471 "</tr><tr>\n"
2472 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2473 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2474 "</tr><tr>\n"
2475 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2476 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2477 "</tr><tr>\n"
2478 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2479 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2480 "</tr><tr>\n"
2481 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2482 "</tr><tr>\n"
2483 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2484 "</tr></table>\n"
2485 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2486 "</td>"
2487 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2488 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2489 "",
Willy Tarreau909b9d82019-01-04 18:20:32 +01002490 (uri->flags & ST_HIDEVER) ? "" : (stats_version_string),
William Lallemand74c24fb2016-11-21 17:18:36 +01002491 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2492 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2493 (uri->flags & ST_SHDESC) ? ": " : "",
2494 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002495 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002496 up / 86400, (up % 86400) / 3600,
2497 (up % 3600) / 60, (up % 60),
2498 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2499 global.rlimit_memmax ? " MB" : "",
2500 global.rlimit_nofile,
2501 global.maxsock, global.maxconn, global.maxpipes,
2502 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002503 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2504 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002505 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002506 );
2507
2508 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002509 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002510 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2511
2512 chunk_appendf(&trash,
2513 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2514 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2515 STAT_SCOPE_TXT_MAXLEN);
2516
2517 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2518 scope_txt[0] = 0;
2519 if (appctx->ctx.stats.scope_len) {
2520 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002521 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002522 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2523 }
2524
2525 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2526 chunk_appendf(&trash,
2527 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2528 uri->uri_prefix,
2529 "",
2530 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2531 scope_txt);
2532 else
2533 chunk_appendf(&trash,
2534 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2535 uri->uri_prefix,
2536 ";up",
2537 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2538 scope_txt);
2539
2540 if (uri->refresh > 0) {
2541 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2542 chunk_appendf(&trash,
2543 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2544 uri->uri_prefix,
2545 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2546 "",
2547 scope_txt);
2548 else
2549 chunk_appendf(&trash,
2550 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2551 uri->uri_prefix,
2552 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2553 ";norefresh",
2554 scope_txt);
2555 }
2556
2557 chunk_appendf(&trash,
2558 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2559 uri->uri_prefix,
2560 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2561 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2562 scope_txt);
2563
2564 chunk_appendf(&trash,
2565 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2566 uri->uri_prefix,
2567 (uri->refresh > 0) ? ";norefresh" : "",
2568 scope_txt);
2569
2570 chunk_appendf(&trash,
2571 "</ul></td>"
2572 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2573 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2574 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2575 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2576 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2577 "</ul>"
2578 "</td>"
2579 "</tr></table>\n"
2580 ""
2581 );
2582
2583 if (appctx->ctx.stats.st_code) {
2584 switch (appctx->ctx.stats.st_code) {
2585 case STAT_STATUS_DONE:
2586 chunk_appendf(&trash,
2587 "<p><div class=active_up>"
2588 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2589 "Action processed successfully."
2590 "</div>\n", uri->uri_prefix,
2591 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2592 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2593 scope_txt);
2594 break;
2595 case STAT_STATUS_NONE:
2596 chunk_appendf(&trash,
2597 "<p><div class=active_going_down>"
2598 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2599 "Nothing has changed."
2600 "</div>\n", uri->uri_prefix,
2601 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2602 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2603 scope_txt);
2604 break;
2605 case STAT_STATUS_PART:
2606 chunk_appendf(&trash,
2607 "<p><div class=active_going_down>"
2608 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2609 "Action partially processed.<br>"
2610 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2611 "</div>\n", uri->uri_prefix,
2612 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2613 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2614 scope_txt);
2615 break;
2616 case STAT_STATUS_ERRP:
2617 chunk_appendf(&trash,
2618 "<p><div class=active_down>"
2619 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2620 "Action not processed because of invalid parameters."
2621 "<ul>"
2622 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002623 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002624 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2625 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2626 "</ul>"
2627 "</div>\n", uri->uri_prefix,
2628 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2629 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2630 scope_txt);
2631 break;
2632 case STAT_STATUS_EXCD:
2633 chunk_appendf(&trash,
2634 "<p><div class=active_down>"
2635 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2636 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2637 "You should retry with less servers at a time.</b>"
2638 "</div>\n", uri->uri_prefix,
2639 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2640 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2641 scope_txt);
2642 break;
2643 case STAT_STATUS_DENY:
2644 chunk_appendf(&trash,
2645 "<p><div class=active_down>"
2646 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2647 "<b>Action denied.</b>"
2648 "</div>\n", uri->uri_prefix,
2649 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2650 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2651 scope_txt);
2652 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002653 case STAT_STATUS_IVAL:
2654 chunk_appendf(&trash,
2655 "<p><div class=active_down>"
2656 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2657 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2658 "</div>\n", uri->uri_prefix,
2659 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2660 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2661 scope_txt);
2662 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002663 default:
2664 chunk_appendf(&trash,
2665 "<p><div class=active_no_check>"
2666 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2667 "Unexpected result."
2668 "</div>\n", uri->uri_prefix,
2669 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2670 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2671 scope_txt);
2672 }
2673 chunk_appendf(&trash, "<p>\n");
2674 }
2675}
2676
2677/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2678 * for clearing the trash if needed.
2679 */
2680static void stats_dump_html_end()
2681{
2682 chunk_appendf(&trash, "</body></html>\n");
2683}
2684
Simon Horman05ee2132017-01-04 09:37:25 +01002685/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2686 * for clearing it if needed.
2687 */
2688static void stats_dump_json_header()
2689{
2690 chunk_strcat(&trash, "[");
2691}
2692
2693
2694/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2695 * for clearing the trash if needed.
2696 */
2697static void stats_dump_json_end()
2698{
2699 chunk_strcat(&trash, "]");
2700}
2701
William Lallemand74c24fb2016-11-21 17:18:36 +01002702/* This function dumps statistics onto the stream interface's read buffer in
2703 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2704 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2705 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2706 * and the stream must be closed, or -1 in case of any error. This function is
2707 * used by both the CLI and the HTTP handlers.
2708 */
Christopher Fauletef779222018-10-31 08:47:01 +01002709static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2710 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002711{
2712 struct appctx *appctx = __objt_appctx(si->end);
2713 struct channel *rep = si_ic(si);
2714 struct proxy *px;
2715
2716 chunk_reset(&trash);
2717
2718 switch (appctx->st2) {
2719 case STAT_ST_INIT:
2720 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2721 /* fall through */
2722
2723 case STAT_ST_HEAD:
2724 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2725 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002726 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002727 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002728 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2729 stats_dump_csv_header();
2730
Christopher Fauletef779222018-10-31 08:47:01 +01002731 if (!stats_putchk(rep, htx, &trash))
2732 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002733
2734 appctx->st2 = STAT_ST_INFO;
2735 /* fall through */
2736
2737 case STAT_ST_INFO:
2738 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2739 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002740 if (!stats_putchk(rep, htx, &trash))
2741 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002742 }
2743
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002744 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002745 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2746 appctx->st2 = STAT_ST_LIST;
2747 /* fall through */
2748
2749 case STAT_ST_LIST:
2750 /* dump proxies */
2751 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002752 if (htx) {
2753 if (htx_almost_full(htx))
2754 goto full;
2755 }
2756 else {
2757 if (buffer_almost_full(&rep->buf))
2758 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002759 }
2760
2761 px = appctx->ctx.stats.px;
2762 /* skip the disabled proxies, global frontend and non-networked ones */
2763 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002764 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002765 return 0;
2766
2767 appctx->ctx.stats.px = px->next;
2768 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2769 }
2770 /* here, we just have reached the last proxy */
2771
2772 appctx->st2 = STAT_ST_END;
2773 /* fall through */
2774
2775 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002776 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2777 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2778 stats_dump_html_end();
2779 else
2780 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002781 if (!stats_putchk(rep, htx, &trash))
2782 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002783 }
2784
2785 appctx->st2 = STAT_ST_FIN;
2786 /* fall through */
2787
2788 case STAT_ST_FIN:
2789 return 1;
2790
2791 default:
2792 /* unknown state ! */
2793 appctx->st2 = STAT_ST_FIN;
2794 return -1;
2795 }
Christopher Fauletef779222018-10-31 08:47:01 +01002796
2797 full:
2798 si_rx_room_blk(si);
2799 return 0;
2800
William Lallemand74c24fb2016-11-21 17:18:36 +01002801}
2802
2803/* We reached the stats page through a POST request. The appctx is
2804 * expected to have already been allocated by the caller.
2805 * Parse the posted data and enable/disable servers if necessary.
2806 * Returns 1 if request was parsed or zero if it needs more data.
2807 */
2808static int stats_process_http_post(struct stream_interface *si)
2809{
2810 struct stream *s = si_strm(si);
2811 struct appctx *appctx = objt_appctx(si->end);
2812
2813 struct proxy *px = NULL;
2814 struct server *sv = NULL;
2815
2816 char key[LINESIZE];
2817 int action = ST_ADM_ACTION_NONE;
2818 int reprocess = 0;
2819
2820 int total_servers = 0;
2821 int altered_servers = 0;
2822
2823 char *first_param, *cur_param, *next_param, *end_params;
2824 char *st_cur_param = NULL;
2825 char *st_next_param = NULL;
2826
Christopher Fauleta3618372018-12-13 21:59:56 +01002827 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002828
Christopher Fauleta3618372018-12-13 21:59:56 +01002829 if (IS_HTX_STRM(s)) {
2830 struct htx *htx = htxbuf(&s->req.buf);
2831 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002832
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002833 /* we need more data */
2834 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2835 /* check if we can receive more */
2836 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2837 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2838 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002839 }
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002840 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002841 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002842
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002843 /* The request was fully received. Copy data */
Christopher Faulet8fa60e42019-05-23 11:04:05 +02002844 blk = htx_get_head_blk(htx);
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002845 while (blk) {
Christopher Fauleta3618372018-12-13 21:59:56 +01002846 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002847
Christopher Faulet54b5e212019-06-04 10:08:28 +02002848 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta3618372018-12-13 21:59:56 +01002849 break;
2850 if (type == HTX_BLK_DATA) {
2851 struct ist v = htx_get_blk_value(htx, blk);
2852
2853 if (!chunk_memcat(temp, v.ptr, v.len)) {
2854 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2855 goto out;
2856 }
2857 }
Christopher Fauleted7a0662019-01-14 11:07:34 +01002858 blk = htx_get_next_blk(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002859 }
2860 }
2861 else {
2862 int reql;
2863
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002864 /* we need more data */
2865 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2866 /* check if we can receive more */
2867 if (c_room(&s->req) <= global.tune.maxrewrite) {
2868 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2869 goto out;
2870 }
2871 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002872 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002873 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2874 s->txn->req.eoh + 2);
2875 if (reql <= 0) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002876 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2877 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002878 }
2879 temp->data = reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002880 }
2881
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002882 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002883 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002884 cur_param = next_param = end_params;
2885 *end_params = '\0';
2886
2887 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2888
2889 /*
2890 * Parse the parameters in reverse order to only store the last value.
2891 * From the html form, the backend and the action are at the end.
2892 */
2893 while (cur_param > first_param) {
2894 char *value;
2895 int poffset, plen;
2896
2897 cur_param--;
2898
2899 if ((*cur_param == '&') || (cur_param == first_param)) {
2900 reprocess_servers:
2901 /* Parse the key */
2902 poffset = (cur_param != first_param ? 1 : 0);
2903 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2904 if ((plen > 0) && (plen <= sizeof(key))) {
2905 strncpy(key, cur_param + poffset, plen);
2906 key[plen - 1] = '\0';
2907 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002908 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002909 goto out;
2910 }
2911
2912 /* Parse the value */
2913 value = key;
2914 while (*value != '\0' && *value != '=') {
2915 value++;
2916 }
2917 if (*value == '=') {
2918 /* Ok, a value is found, we can mark the end of the key */
2919 *value++ = '\0';
2920 }
Willy Tarreau7e913cb2020-04-23 17:54:47 +02002921 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002922 break;
2923
2924 /* Now we can check the key to see what to do */
2925 if (!px && (strcmp(key, "b") == 0)) {
2926 if ((px = proxy_be_by_name(value)) == NULL) {
2927 /* the backend name is unknown or ambiguous (duplicate names) */
2928 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2929 goto out;
2930 }
2931 }
2932 else if (!action && (strcmp(key, "action") == 0)) {
2933 if (strcmp(value, "ready") == 0) {
2934 action = ST_ADM_ACTION_READY;
2935 }
2936 else if (strcmp(value, "drain") == 0) {
2937 action = ST_ADM_ACTION_DRAIN;
2938 }
2939 else if (strcmp(value, "maint") == 0) {
2940 action = ST_ADM_ACTION_MAINT;
2941 }
2942 else if (strcmp(value, "shutdown") == 0) {
2943 action = ST_ADM_ACTION_SHUTDOWN;
2944 }
2945 else if (strcmp(value, "dhlth") == 0) {
2946 action = ST_ADM_ACTION_DHLTH;
2947 }
2948 else if (strcmp(value, "ehlth") == 0) {
2949 action = ST_ADM_ACTION_EHLTH;
2950 }
2951 else if (strcmp(value, "hrunn") == 0) {
2952 action = ST_ADM_ACTION_HRUNN;
2953 }
2954 else if (strcmp(value, "hnolb") == 0) {
2955 action = ST_ADM_ACTION_HNOLB;
2956 }
2957 else if (strcmp(value, "hdown") == 0) {
2958 action = ST_ADM_ACTION_HDOWN;
2959 }
2960 else if (strcmp(value, "dagent") == 0) {
2961 action = ST_ADM_ACTION_DAGENT;
2962 }
2963 else if (strcmp(value, "eagent") == 0) {
2964 action = ST_ADM_ACTION_EAGENT;
2965 }
2966 else if (strcmp(value, "arunn") == 0) {
2967 action = ST_ADM_ACTION_ARUNN;
2968 }
2969 else if (strcmp(value, "adown") == 0) {
2970 action = ST_ADM_ACTION_ADOWN;
2971 }
2972 /* else these are the old supported methods */
2973 else if (strcmp(value, "disable") == 0) {
2974 action = ST_ADM_ACTION_DISABLE;
2975 }
2976 else if (strcmp(value, "enable") == 0) {
2977 action = ST_ADM_ACTION_ENABLE;
2978 }
2979 else if (strcmp(value, "stop") == 0) {
2980 action = ST_ADM_ACTION_STOP;
2981 }
2982 else if (strcmp(value, "start") == 0) {
2983 action = ST_ADM_ACTION_START;
2984 }
2985 else {
2986 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2987 goto out;
2988 }
2989 }
2990 else if (strcmp(key, "s") == 0) {
2991 if (!(px && action)) {
2992 /*
2993 * Indicates that we'll need to reprocess the parameters
2994 * as soon as backend and action are known
2995 */
2996 if (!reprocess) {
2997 st_cur_param = cur_param;
2998 st_next_param = next_param;
2999 }
3000 reprocess = 1;
3001 }
3002 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003003 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003004 switch (action) {
3005 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003006 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003007 altered_servers++;
3008 total_servers++;
3009 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3010 }
3011 break;
3012 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003013 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003014 altered_servers++;
3015 total_servers++;
3016 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3017 }
3018 break;
3019 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003020 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003021 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3022 altered_servers++;
3023 total_servers++;
3024 }
3025 break;
3026 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003027 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003028 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3029 altered_servers++;
3030 total_servers++;
3031 }
3032 break;
3033 case ST_ADM_ACTION_DHLTH:
3034 if (sv->check.state & CHK_ST_CONFIGURED) {
3035 sv->check.state &= ~CHK_ST_ENABLED;
3036 altered_servers++;
3037 total_servers++;
3038 }
3039 break;
3040 case ST_ADM_ACTION_EHLTH:
3041 if (sv->check.state & CHK_ST_CONFIGURED) {
3042 sv->check.state |= CHK_ST_ENABLED;
3043 altered_servers++;
3044 total_servers++;
3045 }
3046 break;
3047 case ST_ADM_ACTION_HRUNN:
3048 if (!(sv->track)) {
3049 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003050 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003051 altered_servers++;
3052 total_servers++;
3053 }
3054 break;
3055 case ST_ADM_ACTION_HNOLB:
3056 if (!(sv->track)) {
3057 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003058 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003059 altered_servers++;
3060 total_servers++;
3061 }
3062 break;
3063 case ST_ADM_ACTION_HDOWN:
3064 if (!(sv->track)) {
3065 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003066 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003067 altered_servers++;
3068 total_servers++;
3069 }
3070 break;
3071 case ST_ADM_ACTION_DAGENT:
3072 if (sv->agent.state & CHK_ST_CONFIGURED) {
3073 sv->agent.state &= ~CHK_ST_ENABLED;
3074 altered_servers++;
3075 total_servers++;
3076 }
3077 break;
3078 case ST_ADM_ACTION_EAGENT:
3079 if (sv->agent.state & CHK_ST_CONFIGURED) {
3080 sv->agent.state |= CHK_ST_ENABLED;
3081 altered_servers++;
3082 total_servers++;
3083 }
3084 break;
3085 case ST_ADM_ACTION_ARUNN:
3086 if (sv->agent.state & CHK_ST_ENABLED) {
3087 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003088 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003089 altered_servers++;
3090 total_servers++;
3091 }
3092 break;
3093 case ST_ADM_ACTION_ADOWN:
3094 if (sv->agent.state & CHK_ST_ENABLED) {
3095 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003096 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003097 altered_servers++;
3098 total_servers++;
3099 }
3100 break;
3101 case ST_ADM_ACTION_READY:
3102 srv_adm_set_ready(sv);
3103 altered_servers++;
3104 total_servers++;
3105 break;
3106 case ST_ADM_ACTION_DRAIN:
3107 srv_adm_set_drain(sv);
3108 altered_servers++;
3109 total_servers++;
3110 break;
3111 case ST_ADM_ACTION_MAINT:
3112 srv_adm_set_maint(sv);
3113 altered_servers++;
3114 total_servers++;
3115 break;
3116 case ST_ADM_ACTION_SHUTDOWN:
3117 if (px->state != PR_STSTOPPED) {
3118 struct stream *sess, *sess_bck;
3119
3120 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3121 if (sess->srv_conn == sv)
3122 stream_shutdown(sess, SF_ERR_KILLED);
3123
3124 altered_servers++;
3125 total_servers++;
3126 }
3127 break;
3128 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003129 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003130 } else {
3131 /* the server name is unknown or ambiguous (duplicate names) */
3132 total_servers++;
3133 }
3134 }
3135 if (reprocess && px && action) {
3136 /* Now, we know the backend and the action chosen by the user.
3137 * We can safely restart from the first server parameter
3138 * to reprocess them
3139 */
3140 cur_param = st_cur_param;
3141 next_param = st_next_param;
3142 reprocess = 0;
3143 goto reprocess_servers;
3144 }
3145
3146 next_param = cur_param;
3147 }
3148 }
3149
3150 if (total_servers == 0) {
3151 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3152 }
3153 else if (altered_servers == 0) {
3154 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3155 }
3156 else if (altered_servers == total_servers) {
3157 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3158 }
3159 else {
3160 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3161 }
3162 out:
3163 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003164 wait:
3165 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3166 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003167}
3168
3169
3170static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
3171{
3172 struct stream *s = si_strm(si);
3173 struct uri_auth *uri = s->be->uri_auth;
3174 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003175 struct htx_sl *sl;
3176 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003177
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003178 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);
3179 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3180 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003181 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003182 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003183
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003184 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003185 goto full;
3186 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3187 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3188 goto full;
3189 }
3190 else {
3191 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3192 goto full;
3193 }
3194
3195 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3196 const char *refresh = U2A(uri->refresh);
3197 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3198 goto full;
3199 }
3200
3201 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3202 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3203 goto full;
3204 }
3205
3206 if (!htx_add_endof(htx, HTX_BLK_EOH))
3207 goto full;
3208
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003209 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003210 return 1;
3211
3212 full:
3213 htx_reset(htx);
3214 si_rx_room_blk(si);
3215 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003216}
3217
Christopher Fauletef779222018-10-31 08:47:01 +01003218
3219static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx)
3220{
3221 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3222 struct stream *s = si_strm(si);
3223 struct uri_auth *uri = s->be->uri_auth;
3224 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003225 struct htx_sl *sl;
3226 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003227
3228 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3229 scope_txt[0] = 0;
3230 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003231 const char *scope_ptr = stats_scope_ptr(appctx, si);
3232
Christopher Fauletef779222018-10-31 08:47:01 +01003233 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003234 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003235 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3236 }
3237
3238 /* We don't want to land on the posted stats page because a refresh will
3239 * repost the data. We don't want this to happen on accident so we redirect
3240 * the browse to the stats page with a GET.
3241 */
3242 chunk_printf(&trash, "%s;st=%s%s%s%s",
3243 uri->uri_prefix,
3244 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3245 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3246 stat_status_codes[appctx->ctx.stats.st_code]) ?
3247 stat_status_codes[appctx->ctx.stats.st_code] :
3248 stat_status_codes[STAT_STATUS_UNKN],
3249 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3250 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3251 scope_txt);
3252
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003253 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3254 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3255 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003256 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003257 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003258
3259 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003260 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3261 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3262 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3263 goto full;
3264
3265 if (!htx_add_endof(htx, HTX_BLK_EOH))
3266 goto full;
3267
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003268 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003269 return 1;
3270
3271full:
3272 htx_reset(htx);
3273 si_rx_room_blk(si);
3274 return 0;
3275}
William Lallemand74c24fb2016-11-21 17:18:36 +01003276
3277static int stats_send_http_headers(struct stream_interface *si)
3278{
3279 struct stream *s = si_strm(si);
3280 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003281 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003282
3283 chunk_printf(&trash,
3284 "HTTP/1.1 200 OK\r\n"
3285 "Cache-Control: no-cache\r\n"
3286 "Connection: close\r\n"
3287 "Content-Type: %s\r\n",
3288 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
3289
3290 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
3291 chunk_appendf(&trash, "Refresh: %d\r\n",
3292 uri->refresh);
3293
3294 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
3295
3296 if (appctx->ctx.stats.flags & STAT_CHUNKED)
3297 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
3298 else
3299 chunk_appendf(&trash, "\r\n");
3300
Willy Tarreau06d80a92017-10-19 14:32:15 +02003301 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003302 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003303 return 0;
3304 }
3305
3306 return 1;
3307}
3308
3309static int stats_send_http_redirect(struct stream_interface *si)
3310{
3311 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3312 struct stream *s = si_strm(si);
3313 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003314 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003315
3316 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3317 scope_txt[0] = 0;
3318 if (appctx->ctx.stats.scope_len) {
3319 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02003320 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 +01003321 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3322 }
3323
3324 /* We don't want to land on the posted stats page because a refresh will
3325 * repost the data. We don't want this to happen on accident so we redirect
3326 * the browse to the stats page with a GET.
3327 */
3328 chunk_printf(&trash,
3329 "HTTP/1.1 303 See Other\r\n"
3330 "Cache-Control: no-cache\r\n"
3331 "Content-Type: text/plain\r\n"
3332 "Connection: close\r\n"
3333 "Location: %s;st=%s%s%s%s\r\n"
3334 "Content-length: 0\r\n"
3335 "\r\n",
3336 uri->uri_prefix,
3337 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3338 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3339 stat_status_codes[appctx->ctx.stats.st_code]) ?
3340 stat_status_codes[appctx->ctx.stats.st_code] :
3341 stat_status_codes[STAT_STATUS_UNKN],
3342 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3343 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3344 scope_txt);
3345
Willy Tarreau06d80a92017-10-19 14:32:15 +02003346 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003347 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003348 return 0;
3349 }
3350
3351 return 1;
3352}
3353
Simon Horman05ee2132017-01-04 09:37:25 +01003354
William Lallemand74c24fb2016-11-21 17:18:36 +01003355/* This I/O handler runs as an applet embedded in a stream interface. It is
3356 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3357 * appctx->st0 contains the operation in progress (dump, done). The handler
3358 * automatically unregisters itself once transfer is complete.
3359 */
Christopher Fauletef779222018-10-31 08:47:01 +01003360static void htx_stats_io_handler(struct appctx *appctx)
3361{
3362 struct stream_interface *si = appctx->owner;
3363 struct stream *s = si_strm(si);
3364 struct channel *req = si_oc(si);
3365 struct channel *res = si_ic(si);
3366 struct htx *req_htx, *res_htx;
3367
3368 res_htx = htx_from_buf(&res->buf);
3369
3370 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3371 goto out;
3372
3373 /* Check if the input buffer is avalaible. */
3374 if (!b_size(&res->buf)) {
3375 si_rx_room_blk(si);
3376 goto out;
3377 }
3378
3379 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003380 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3381 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003382
3383 /* all states are processed in sequence */
3384 if (appctx->st0 == STAT_HTTP_HEAD) {
3385 if (stats_send_htx_headers(si, res_htx)) {
3386 if (s->txn->meth == HTTP_METH_HEAD)
3387 appctx->st0 = STAT_HTTP_DONE;
3388 else
3389 appctx->st0 = STAT_HTTP_DUMP;
3390 }
3391 }
3392
3393 if (appctx->st0 == STAT_HTTP_DUMP) {
3394 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3395 appctx->st0 = STAT_HTTP_DONE;
3396 }
3397
3398 if (appctx->st0 == STAT_HTTP_POST) {
3399 if (stats_process_http_post(si))
3400 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003401 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003402 appctx->st0 = STAT_HTTP_DONE;
3403 }
3404
3405 if (appctx->st0 == STAT_HTTP_LAST) {
3406 if (stats_send_htx_redirect(si, res_htx))
3407 appctx->st0 = STAT_HTTP_DONE;
3408 }
3409
3410 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003411 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003412 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3413 si_rx_room_blk(si);
3414 goto out;
3415 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003416 channel_add_input(&s->res, 1);
3417 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003418 }
3419
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003420 if (appctx->st0 == STAT_HTTP_END) {
3421 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003422 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003423 si_shutr(si);
3424 }
3425
3426 /* eat the whole request */
3427 if (co_data(req)) {
3428 req_htx = htx_from_buf(&req->buf);
3429 co_htx_skip(req, req_htx, co_data(req));
3430 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003431 }
3432 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003433
Christopher Fauletef779222018-10-31 08:47:01 +01003434 out:
3435 /* we have left the request in the buffer for the case where we
3436 * process a POST, and this automatically re-enables activity on
3437 * read. It's better to indicate that we want to stop reading when
3438 * we're sending, so that we know there's at most one direction
3439 * deciding to wake the applet up. It saves it from looping when
3440 * emitting large blocks into small TCP windows.
3441 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003442 htx_to_buf(res_htx, &res->buf);
3443 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003444 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003445}
3446
3447
3448/* This I/O handler runs as an applet embedded in a stream interface. It is
3449 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3450 * appctx->st0 contains the operation in progress (dump, done). The handler
3451 * automatically unregisters itself once transfer is complete.
3452 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003453static void http_stats_io_handler(struct appctx *appctx)
3454{
3455 struct stream_interface *si = appctx->owner;
3456 struct stream *s = si_strm(si);
3457 struct channel *req = si_oc(si);
3458 struct channel *res = si_ic(si);
3459
Christopher Fauletef779222018-10-31 08:47:01 +01003460 if (IS_HTX_STRM(s))
3461 return htx_stats_io_handler(appctx);
3462
William Lallemand74c24fb2016-11-21 17:18:36 +01003463 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3464 goto out;
3465
Joseph Herlant29023ec2018-11-15 13:39:46 -08003466 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003467 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003468 /* already subscribed, we'll be called later once the buffer is
3469 * available.
3470 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003471 goto out;
3472 }
3473
William Lallemand74c24fb2016-11-21 17:18:36 +01003474 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003475 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3476 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003477
3478 /* all states are processed in sequence */
3479 if (appctx->st0 == STAT_HTTP_HEAD) {
3480 if (stats_send_http_headers(si)) {
3481 if (s->txn->meth == HTTP_METH_HEAD)
3482 appctx->st0 = STAT_HTTP_DONE;
3483 else
3484 appctx->st0 = STAT_HTTP_DUMP;
3485 }
3486 }
3487
3488 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003489 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003490 unsigned int data_len;
3491 unsigned int last_len;
3492 unsigned int last_fwd = 0;
3493
3494 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3495 /* One difficulty we're facing is that we must prevent
3496 * the input data from being automatically forwarded to
3497 * the output area. For this, we temporarily disable
3498 * forwarding on the channel.
3499 */
3500 last_fwd = si_ic(si)->to_forward;
3501 si_ic(si)->to_forward = 0;
3502 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003503 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003504 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003505 si_ic(si)->to_forward = last_fwd;
3506 goto out;
3507 }
3508 }
3509
Willy Tarreau97f538b2018-06-15 19:41:31 +02003510 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003511 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003512 appctx->st0 = STAT_HTTP_DONE;
3513
Willy Tarreau97f538b2018-06-15 19:41:31 +02003514 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003515
3516 /* Now we must either adjust or remove the chunk size. This is
3517 * not easy because the chunk size might wrap at the end of the
3518 * buffer, so we pretend we have nothing in the buffer, we write
3519 * the size, then restore the buffer's contents. Note that we can
3520 * only do that because no forwarding is scheduled on the stats
3521 * applet.
3522 */
3523 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3524 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003525 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003526
3527 if (last_len != data_len) {
3528 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003529 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003530 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003531
3532 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003533 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003534 }
3535 /* now re-enable forwarding */
3536 channel_forward(si_ic(si), last_fwd);
3537 }
3538 }
3539
3540 if (appctx->st0 == STAT_HTTP_POST) {
3541 if (stats_process_http_post(si))
3542 appctx->st0 = STAT_HTTP_LAST;
3543 else if (si_oc(si)->flags & CF_SHUTR)
3544 appctx->st0 = STAT_HTTP_DONE;
3545 }
3546
3547 if (appctx->st0 == STAT_HTTP_LAST) {
3548 if (stats_send_http_redirect(si))
3549 appctx->st0 = STAT_HTTP_DONE;
3550 }
3551
3552 if (appctx->st0 == STAT_HTTP_DONE) {
3553 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3554 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003555 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003556 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003557 goto out;
3558 }
3559 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003560 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003561 }
3562
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003563 if (appctx->st0 == STAT_HTTP_END) {
3564 if (!(res->flags & CF_SHUTR)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003565 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003566 si_shutr(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003567 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003568
3569 /* eat the whole request */
3570 if (co_data(req))
3571 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003572 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003573
William Lallemand74c24fb2016-11-21 17:18:36 +01003574 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003575 /* we have left the request in the buffer for the case where we
3576 * process a POST, and this automatically re-enables activity on
3577 * read. It's better to indicate that we want to stop reading when
3578 * we're sending, so that we know there's at most one direction
3579 * deciding to wake the applet up. It saves it from looping when
3580 * emitting large blocks into small TCP windows.
3581 */
3582 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003583 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003584}
3585
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003586/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003587static int stats_dump_info_fields(struct buffer *out,
3588 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003589{
3590 int field;
3591
3592 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3593 if (!field_format(info, field))
3594 continue;
3595
3596 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3597 return 0;
3598 if (!stats_emit_raw_data_field(out, &info[field]))
3599 return 0;
3600 if (!chunk_strcat(out, "\n"))
3601 return 0;
3602 }
3603 return 1;
3604}
3605
3606/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003607static int stats_dump_typed_info_fields(struct buffer *out,
3608 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003609{
3610 int field;
3611
3612 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3613 if (!field_format(info, field))
3614 continue;
3615
3616 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3617 return 0;
3618 if (!stats_emit_field_tags(out, &info[field], ':'))
3619 return 0;
3620 if (!stats_emit_typed_data_field(out, &info[field]))
3621 return 0;
3622 if (!chunk_strcat(out, "\n"))
3623 return 0;
3624 }
3625 return 1;
3626}
3627
3628/* Fill <info> with HAProxy global info. <info> is preallocated
3629 * array of length <len>. The length of the aray must be
3630 * INF_TOTAL_FIELDS. If this length is less then this value, the
3631 * function returns 0, otherwise, it returns 1.
3632 */
3633int stats_fill_info(struct field *info, int len)
3634{
3635 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003636 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003637
3638#ifdef USE_OPENSSL
3639 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3640 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3641 int ssl_reuse = 0;
3642
3643 if (ssl_key_rate < ssl_sess_rate) {
3644 /* count the ssl reuse ratio and avoid overflows in both directions */
3645 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3646 }
3647#endif
3648
3649 if (len < INF_TOTAL_FIELDS)
3650 return 0;
3651
3652 chunk_reset(out);
3653 memset(info, 0, sizeof(*info) * len);
3654
3655 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003656 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3657 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003658
Yves Lafon95317282018-02-26 11:10:37 +01003659 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003660 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3661 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3662 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3663
3664 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3665 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3666
3667 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3668 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3669 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3670 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3671 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3672 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3673 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3674 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3675 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3676 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3677 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3678 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3679#ifdef USE_OPENSSL
3680 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3681 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3682 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3683#endif
3684 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3685 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3686 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3687 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3688 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3689 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3690 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3691 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3692 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3693
3694#ifdef USE_OPENSSL
3695 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3696 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3697 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3698 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3699 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3700 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3701 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3702 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3703 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3704 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3705#endif
3706 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3707 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3708 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3709#ifdef USE_ZLIB
3710 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3711 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3712#endif
3713 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003714 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003715 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003716 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3717 if (global.desc)
3718 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003719 info[INF_STOPPING] = mkf_u32(0, stopping);
3720 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003721 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003722 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003723 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003724 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003725 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003726 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003727 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003728 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3729 info[INF_BYTES_OUT_RATE] = mkf_u64(FN_RATE, (unsigned long long)read_freq_ctr(&global.out_32bps) * 32);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003730
3731 return 1;
3732}
3733
3734/* This function dumps information onto the stream interface's read buffer.
3735 * It returns 0 as long as it does not complete, non-zero upon completion.
3736 * No state is used.
3737 */
3738static int stats_dump_info_to_buffer(struct stream_interface *si)
3739{
3740 struct appctx *appctx = __objt_appctx(si->end);
3741
3742 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3743 return 0;
3744
3745 chunk_reset(&trash);
3746
3747 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3748 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003749 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3750 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003751 else
3752 stats_dump_info_fields(&trash, info);
3753
Willy Tarreau06d80a92017-10-19 14:32:15 +02003754 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003755 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003756 return 0;
3757 }
3758
3759 return 1;
3760}
3761
Simon Horman6f6bb382017-01-04 09:37:26 +01003762/* This function dumps the schema onto the stream interface's read buffer.
3763 * It returns 0 as long as it does not complete, non-zero upon completion.
3764 * No state is used.
3765 *
3766 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3767 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3768 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003769static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003770{
3771
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003772 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003773
3774 chunk_strcat(out,
3775 "{"
3776 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3777 "\"oneOf\":["
3778 "{"
3779 "\"title\":\"Info\","
3780 "\"type\":\"array\","
3781 "\"items\":{"
Amaury Denoyellec80f9762020-10-02 18:31:59 +02003782 "\"title\":\"InfoItem\","
3783 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01003784 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01003785 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3786 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3787 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3788 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3789 "},"
3790 "\"required\":[\"field\",\"processNum\",\"tags\","
3791 "\"value\"]"
3792 "}"
3793 "},"
3794 "{"
3795 "\"title\":\"Stat\","
3796 "\"type\":\"array\","
3797 "\"items\":{"
3798 "\"title\":\"InfoItem\","
3799 "\"type\":\"object\","
3800 "\"properties\":{"
3801 "\"objType\":{"
3802 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3803 "\"Server\",\"Unknown\"]"
3804 "},"
3805 "\"proxyId\":{"
3806 "\"type\":\"integer\","
3807 "\"minimum\":0"
3808 "},"
3809 "\"id\":{"
3810 "\"type\":\"integer\","
3811 "\"minimum\":0"
3812 "},"
3813 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3814 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3815 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3816 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3817 "},"
3818 "\"required\":[\"objType\",\"proxyId\",\"id\","
3819 "\"field\",\"processNum\",\"tags\","
3820 "\"value\"]"
3821 "}"
3822 "},"
3823 "{"
3824 "\"title\":\"Error\","
3825 "\"type\":\"object\","
3826 "\"properties\":{"
3827 "\"errorStr\":{"
3828 "\"type\":\"string\""
Amaury Denoyellec80f9762020-10-02 18:31:59 +02003829 "}"
3830 "},"
3831 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01003832 "}"
3833 "],"
3834 "\"definitions\":{"
3835 "\"field\":{"
3836 "\"type\":\"object\","
3837 "\"pos\":{"
3838 "\"type\":\"integer\","
3839 "\"minimum\":0"
3840 "},"
3841 "\"name\":{"
3842 "\"type\":\"string\""
3843 "},"
3844 "\"required\":[\"pos\",\"name\"]"
3845 "},"
3846 "\"processNum\":{"
3847 "\"type\":\"integer\","
3848 "\"minimum\":1"
3849 "},"
3850 "\"tags\":{"
3851 "\"type\":\"object\","
3852 "\"origin\":{"
3853 "\"type\":\"string\","
3854 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3855 "\"Config\",\"Product\",\"Unknown\"]"
3856 "},"
3857 "\"nature\":{"
3858 "\"type\":\"string\","
3859 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3860 "\"Rate\",\"Counter\",\"Duration\","
3861 "\"Age\",\"Time\",\"Name\",\"Output\","
3862 "\"Avg\", \"Unknown\"]"
3863 "},"
3864 "\"scope\":{"
3865 "\"type\":\"string\","
3866 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3867 "\"System\",\"Unknown\"]"
3868 "},"
3869 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3870 "},"
3871 "\"typedValue\":{"
3872 "\"type\":\"object\","
3873 "\"oneOf\":["
3874 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3875 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3876 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3877 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3878 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3879 "],"
3880 "\"definitions\":{"
3881 "\"s32Value\":{"
3882 "\"properties\":{"
3883 "\"type\":{"
3884 "\"type\":\"string\","
3885 "\"enum\":[\"s32\"]"
3886 "},"
3887 "\"value\":{"
3888 "\"type\":\"integer\","
3889 "\"minimum\":-2147483648,"
3890 "\"maximum\":2147483647"
3891 "}"
3892 "},"
3893 "\"required\":[\"type\",\"value\"]"
3894 "},"
3895 "\"s64Value\":{"
3896 "\"properties\":{"
3897 "\"type\":{"
3898 "\"type\":\"string\","
3899 "\"enum\":[\"s64\"]"
3900 "},"
3901 "\"value\":{"
3902 "\"type\":\"integer\","
3903 "\"minimum\":-9007199254740991,"
3904 "\"maximum\":9007199254740991"
3905 "}"
3906 "},"
3907 "\"required\":[\"type\",\"value\"]"
3908 "},"
3909 "\"u32Value\":{"
3910 "\"properties\":{"
3911 "\"type\":{"
3912 "\"type\":\"string\","
3913 "\"enum\":[\"u32\"]"
3914 "},"
3915 "\"value\":{"
3916 "\"type\":\"integer\","
3917 "\"minimum\":0,"
3918 "\"maximum\":4294967295"
3919 "}"
3920 "},"
3921 "\"required\":[\"type\",\"value\"]"
3922 "},"
3923 "\"u64Value\":{"
3924 "\"properties\":{"
3925 "\"type\":{"
3926 "\"type\":\"string\","
3927 "\"enum\":[\"u64\"]"
3928 "},"
3929 "\"value\":{"
3930 "\"type\":\"integer\","
3931 "\"minimum\":0,"
3932 "\"maximum\":9007199254740991"
3933 "}"
3934 "},"
3935 "\"required\":[\"type\",\"value\"]"
3936 "},"
3937 "\"strValue\":{"
3938 "\"properties\":{"
3939 "\"type\":{"
3940 "\"type\":\"string\","
3941 "\"enum\":[\"str\"]"
3942 "},"
3943 "\"value\":{\"type\":\"string\"}"
3944 "},"
3945 "\"required\":[\"type\",\"value\"]"
3946 "},"
3947 "\"unknownValue\":{"
3948 "\"properties\":{"
3949 "\"type\":{"
3950 "\"type\":\"integer\","
3951 "\"minimum\":0"
3952 "},"
3953 "\"value\":{"
3954 "\"type\":\"string\","
3955 "\"enum\":[\"unknown\"]"
3956 "}"
3957 "},"
3958 "\"required\":[\"type\",\"value\"]"
3959 "}"
3960 "}"
3961 "}"
3962 "}"
3963 "}");
3964
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003965 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003966 chunk_reset(out);
3967 chunk_appendf(out,
3968 "{\"errorStr\":\"output buffer too short\"}");
3969 }
3970}
3971
3972/* This function dumps the schema onto the stream interface's read buffer.
3973 * It returns 0 as long as it does not complete, non-zero upon completion.
3974 * No state is used.
3975 */
3976static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3977{
3978 chunk_reset(&trash);
3979
3980 stats_dump_json_schema(&trash);
3981
Willy Tarreau06d80a92017-10-19 14:32:15 +02003982 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003983 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003984 return 0;
3985 }
3986
3987 return 1;
3988}
3989
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003990static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003991{
3992 struct proxy *px;
3993 struct server *sv;
3994 struct listener *li;
3995 int clrall = 0;
3996
3997 if (strcmp(args[2], "all") == 0)
3998 clrall = 1;
3999
4000 /* check permissions */
4001 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4002 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4003 return 1;
4004
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004005 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004006 if (clrall) {
4007 memset(&px->be_counters, 0, sizeof(px->be_counters));
4008 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4009 }
4010 else {
4011 px->be_counters.conn_max = 0;
4012 px->be_counters.p.http.rps_max = 0;
4013 px->be_counters.sps_max = 0;
4014 px->be_counters.cps_max = 0;
4015 px->be_counters.nbpend_max = 0;
Christopher Fauletdf73efe2019-11-08 14:53:15 +01004016 px->be_counters.qtime_max = 0;
4017 px->be_counters.ctime_max = 0;
4018 px->be_counters.dtime_max = 0;
4019 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004020
4021 px->fe_counters.conn_max = 0;
4022 px->fe_counters.p.http.rps_max = 0;
4023 px->fe_counters.sps_max = 0;
4024 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004025 }
4026
4027 for (sv = px->srv; sv; sv = sv->next)
4028 if (clrall)
4029 memset(&sv->counters, 0, sizeof(sv->counters));
4030 else {
4031 sv->counters.cur_sess_max = 0;
4032 sv->counters.nbpend_max = 0;
4033 sv->counters.sps_max = 0;
Christopher Fauletdf73efe2019-11-08 14:53:15 +01004034 sv->counters.qtime_max = 0;
4035 sv->counters.ctime_max = 0;
4036 sv->counters.dtime_max = 0;
4037 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004038 }
4039
4040 list_for_each_entry(li, &px->conf.listeners, by_fe)
4041 if (li->counters) {
4042 if (clrall)
4043 memset(li->counters, 0, sizeof(*li->counters));
4044 else
4045 li->counters->conn_max = 0;
4046 }
4047 }
4048
4049 global.cps_max = 0;
4050 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004051 global.ssl_max = 0;
4052 global.ssl_fe_keys_max = 0;
4053 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004054
4055 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004056 return 1;
4057}
4058
4059
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004060static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004061{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004062 appctx->ctx.stats.scope_str = 0;
4063 appctx->ctx.stats.scope_len = 0;
4064 appctx->ctx.stats.flags = 0;
4065
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004066 if (strcmp(args[2], "typed") == 0)
4067 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004068 else if (strcmp(args[2], "json") == 0)
4069 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004070 return 0;
4071}
4072
4073
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004074static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004075{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004076 appctx->ctx.stats.scope_str = 0;
4077 appctx->ctx.stats.scope_len = 0;
4078 appctx->ctx.stats.flags = 0;
4079
Willy Tarreau2b812e22016-11-22 16:18:05 +01004080 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004081 struct proxy *px;
4082
4083 px = proxy_find_by_name(args[2], 0, 0);
4084 if (px)
4085 appctx->ctx.stats.iid = px->uuid;
4086 else
4087 appctx->ctx.stats.iid = atoi(args[2]);
4088
4089 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004090 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004091 appctx->ctx.cli.msg = "No such proxy.\n";
4092 appctx->st0 = CLI_ST_PRINT;
4093 return 1;
4094 }
4095
Willy Tarreau2b812e22016-11-22 16:18:05 +01004096 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004097 appctx->ctx.stats.type = atoi(args[3]);
4098 appctx->ctx.stats.sid = atoi(args[4]);
4099 if (strcmp(args[5], "typed") == 0)
4100 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004101 else if (strcmp(args[5], "json") == 0)
4102 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004103 }
4104 else if (strcmp(args[2], "typed") == 0)
4105 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004106 else if (strcmp(args[2], "json") == 0)
4107 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004108
Willy Tarreau2b812e22016-11-22 16:18:05 +01004109 return 0;
4110}
4111
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004112static int cli_io_handler_dump_info(struct appctx *appctx)
4113{
4114 return stats_dump_info_to_buffer(appctx->owner);
4115}
4116
Willy Tarreau2b812e22016-11-22 16:18:05 +01004117/* This I/O handler runs as an applet embedded in a stream interface. It is
4118 * used to send raw stats over a socket.
4119 */
4120static int cli_io_handler_dump_stat(struct appctx *appctx)
4121{
Christopher Fauletef779222018-10-31 08:47:01 +01004122 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004123}
4124
Simon Horman6f6bb382017-01-04 09:37:26 +01004125static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4126{
4127 return stats_dump_json_schema_to_buffer(appctx->owner);
4128}
4129
Willy Tarreau2b812e22016-11-22 16:18:05 +01004130/* register cli keywords */
4131static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004132 { { "clear", "counters", NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreaue9038432019-10-09 07:19:02 +02004133 { { "show", "info", NULL }, "show info : report information about the running process [json|typed]", cli_parse_show_info, cli_io_handler_dump_info, NULL },
4134 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server [json|typed]", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01004135 { { "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 +01004136 {{},}
4137}};
4138
Willy Tarreau0108d902018-11-25 19:14:37 +01004139INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4140
William Lallemand74c24fb2016-11-21 17:18:36 +01004141struct applet http_stats_applet = {
4142 .obj_type = OBJ_TYPE_APPLET,
4143 .name = "<STATS>", /* used for logging */
4144 .fct = http_stats_io_handler,
4145 .release = NULL,
4146};
4147
William Lallemand74c24fb2016-11-21 17:18:36 +01004148/*
4149 * Local variables:
4150 * c-indent-level: 8
4151 * c-basic-offset: 8
4152 * End:
4153 */