blob: 992005a2c16b2c6bde84f5351c54c1c1b9ce5149 [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 }
918 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
919 style = "going_down";
920 }
921 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
922 style = "nolb";
923 }
924 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
925 style = "no_check";
926 }
927 else if (!stats[ST_F_CHKFAIL].type ||
928 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
929 /* no check or max health = UP */
930 if (stats[ST_F_WEIGHT].u.u32)
931 style = "up";
932 else
933 style = "draining";
934 }
935 else {
936 style = "going_down";
937 }
938
939 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
940 chunk_appendf(out, "<tr class=\"maintain\">");
941 else
942 chunk_appendf(out,
943 "<tr class=\"%s_%s\">",
944 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
945
946
947 if (flags & ST_SHOWADMIN)
948 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000949 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
950 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100951 field_str(stats, ST_F_SVNAME));
952
953 chunk_appendf(out,
954 "<td class=ac><a name=\"%s/%s\"></a>%s"
955 "<a class=lfsb href=\"#%s/%s\">%s</a>"
956 "",
957 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
958 (flags & ST_SHLGNDS) ? "<u>" : "",
959 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
960
961 if (flags & ST_SHLGNDS) {
962 chunk_appendf(out, "<div class=tips>");
963
964 if (isdigit(*field_str(stats, ST_F_ADDR)))
965 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
966 else if (*field_str(stats, ST_F_ADDR) == '[')
967 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
968 else if (*field_str(stats, ST_F_ADDR))
969 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
970
971 /* id */
972 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
973
974 /* cookie */
975 if (stats[ST_F_COOKIE].type) {
976 chunk_appendf(out, ", cookie: '");
977 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
978 chunk_htmlencode(out, &src);
979 chunk_appendf(out, "'");
980 }
981
982 chunk_appendf(out, "</div>");
983 }
984
985 chunk_appendf(out,
986 /* queue : current, max, limit */
987 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
988 /* sessions rate : current, max, limit */
989 "<td>%s</td><td>%s</td><td></td>"
990 "",
991 (flags & ST_SHLGNDS) ? "</u>" : "",
992 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
993 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
994
995 chunk_appendf(out,
996 /* sessions: current, max, limit, total */
Willy Tarreau0ff699e2019-09-08 09:24:56 +0200997 "<td><u>%s<div class=tips>"
998 "<table class=det>"
999 "<tr><th>Current active connections:</th><td>%s</td></tr>"
1000 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
1001 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1002 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1003 "</table></div></u>"
1004 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001005 "<td><u>%s<div class=tips><table class=det>"
1006 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1007 "",
Willy Tarreau0ff699e2019-09-08 09:24:56 +02001008 U2H(stats[ST_F_SCUR].u.u32),
1009 U2H(stats[ST_F_SCUR].u.u32),
1010 U2H(stats[ST_F_SRV_ICUR].u.u32),
1011 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1012 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1013 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001014 U2H(stats[ST_F_STOT].u.u64),
1015 U2H(stats[ST_F_STOT].u.u64));
1016
1017 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1018 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1019 unsigned long long tot;
1020
1021 tot = stats[ST_F_HRSP_OTHER].u.u64;
1022 tot += stats[ST_F_HRSP_1XX].u.u64;
1023 tot += stats[ST_F_HRSP_2XX].u.u64;
1024 tot += stats[ST_F_HRSP_3XX].u.u64;
1025 tot += stats[ST_F_HRSP_4XX].u.u64;
1026 tot += stats[ST_F_HRSP_5XX].u.u64;
1027
1028 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001029 "<tr><th>New connections:</th><td>%s</td></tr>"
1030 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001031 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1032 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1033 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1034 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1035 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1036 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1037 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001038 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001039 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001040 U2H(stats[ST_F_CONNECT].u.u64),
1041 U2H(stats[ST_F_REUSE].u.u64),
1042 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1043 (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 +01001044 U2H(tot),
1045 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1046 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1047 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1048 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1049 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 +02001050 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1051 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001052 }
1053
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001054 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1055 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1056 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1057 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1058 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001059 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001060 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1061 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1062 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1063 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001064
1065 chunk_appendf(out,
1066 "</table></div></u></td>"
1067 /* sessions: lbtot, last */
1068 "<td>%s</td><td>%s</td>",
1069 U2H(stats[ST_F_LBTOT].u.u64),
1070 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1071
1072 chunk_appendf(out,
1073 /* bytes : in, out */
1074 "<td>%s</td><td>%s</td>"
1075 /* denied: req, resp */
1076 "<td></td><td>%s</td>"
1077 /* errors : request, connect */
1078 "<td></td><td>%s</td>"
1079 /* errors : response */
1080 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1081 /* warnings: retries, redispatches */
1082 "<td>%lld</td><td>%lld</td>"
1083 "",
1084 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1085 U2H(stats[ST_F_DRESP].u.u64),
1086 U2H(stats[ST_F_ECON].u.u64),
1087 U2H(stats[ST_F_ERESP].u.u64),
1088 (long long)stats[ST_F_CLI_ABRT].u.u64,
1089 (long long)stats[ST_F_SRV_ABRT].u.u64,
1090 (long long)stats[ST_F_WRETR].u.u64,
1091 (long long)stats[ST_F_WREDIS].u.u64);
1092
1093 /* status, last change */
1094 chunk_appendf(out, "<td class=ac>");
1095
1096 /* FIXME!!!!
1097 * LASTCHG should contain the last change for *this* server and must be computed
1098 * properly above, as was done below, ie: this server if maint, otherwise ref server
1099 * if tracking. Note that ref is either local or remote depending on tracking.
1100 */
1101
1102
1103 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1104 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1105 }
1106 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1107 chunk_strcat(out, "<i>no check</i>");
1108 }
1109 else {
1110 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1111 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1112 if (stats[ST_F_CHECK_HEALTH].u.u32)
1113 chunk_strcat(out, " &uarr;");
1114 }
1115 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1116 chunk_strcat(out, " &darr;");
1117 }
1118
1119 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1120 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1121 chunk_appendf(out,
1122 "</td><td class=ac><u> %s",
1123 field_str(stats, ST_F_AGENT_STATUS));
1124
1125 if (stats[ST_F_AGENT_CODE].type)
1126 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1127
1128 if (stats[ST_F_AGENT_DURATION].type)
1129 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1130
1131 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1132
1133 if (*field_str(stats, ST_F_LAST_AGT)) {
1134 chunk_appendf(out, ": ");
1135 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1136 chunk_htmlencode(out, &src);
1137 }
1138 chunk_appendf(out, "</div></u>");
1139 }
1140 else if (stats[ST_F_CHECK_STATUS].type) {
1141 chunk_appendf(out,
1142 "</td><td class=ac><u> %s",
1143 field_str(stats, ST_F_CHECK_STATUS));
1144
1145 if (stats[ST_F_CHECK_CODE].type)
1146 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1147
1148 if (stats[ST_F_CHECK_DURATION].type)
1149 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1150
1151 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1152
1153 if (*field_str(stats, ST_F_LAST_CHK)) {
1154 chunk_appendf(out, ": ");
1155 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1156 chunk_htmlencode(out, &src);
1157 }
1158 chunk_appendf(out, "</div></u>");
1159 }
1160 else
1161 chunk_appendf(out, "</td><td>");
1162
1163 chunk_appendf(out,
1164 /* weight */
1165 "</td><td class=ac>%d</td>"
1166 /* act, bck */
1167 "<td class=ac>%s</td><td class=ac>%s</td>"
1168 "",
1169 stats[ST_F_WEIGHT].u.u32,
1170 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1171 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1172
1173 /* check failures: unique, fatal, down time */
1174 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1175 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1176 }
1177 else if (stats[ST_F_CHKFAIL].type) {
1178 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1179
1180 if (stats[ST_F_HANAFAIL].type)
1181 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1182
1183 chunk_appendf(out,
1184 "<div class=tips>Failed Health Checks%s</div></u></td>"
1185 "<td>%lld</td><td>%s</td>"
1186 "",
1187 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1188 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1189 }
1190 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1191 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1192 chunk_appendf(out,
1193 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1194 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1195 }
1196 else
1197 chunk_appendf(out, "<td colspan=3></td>");
1198
1199 /* throttle */
1200 if (stats[ST_F_THROTTLE].type)
1201 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1202 else
1203 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1204 }
1205 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1206 chunk_appendf(out, "<tr class=\"backend\">");
1207 if (flags & ST_SHOWADMIN) {
1208 /* Column sub-heading for Enable or Disable server */
1209 chunk_appendf(out, "<td></td>");
1210 }
1211 chunk_appendf(out,
1212 "<td class=ac>"
1213 /* name */
1214 "%s<a name=\"%s/Backend\"></a>"
1215 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1216 "",
1217 (flags & ST_SHLGNDS)?"<u>":"",
1218 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1219
1220 if (flags & ST_SHLGNDS) {
1221 /* balancing */
1222 chunk_appendf(out, "<div class=tips>balancing: %s",
1223 field_str(stats, ST_F_ALGO));
1224
1225 /* cookie */
1226 if (stats[ST_F_COOKIE].type) {
1227 chunk_appendf(out, ", cookie: '");
1228 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1229 chunk_htmlencode(out, &src);
1230 chunk_appendf(out, "'");
1231 }
1232 chunk_appendf(out, "</div>");
1233 }
1234
1235 chunk_appendf(out,
1236 "%s</td>"
1237 /* queue : current, max */
1238 "<td>%s</td><td>%s</td><td></td>"
1239 /* sessions rate : current, max, limit */
1240 "<td>%s</td><td>%s</td><td></td>"
1241 "",
1242 (flags & ST_SHLGNDS)?"</u>":"",
1243 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1244 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1245
1246 chunk_appendf(out,
1247 /* sessions: current, max, limit, total */
1248 "<td>%s</td><td>%s</td><td>%s</td>"
1249 "<td><u>%s<div class=tips><table class=det>"
1250 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1251 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001252 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 +01001253 U2H(stats[ST_F_STOT].u.u64),
1254 U2H(stats[ST_F_STOT].u.u64));
1255
1256 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1257 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1258 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001259 "<tr><th>New connections:</th><td>%s</td></tr>"
1260 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001261 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1262 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1263 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1264 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1265 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1266 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1267 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1268 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001269 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1270 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001271 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001272 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001273 U2H(stats[ST_F_CONNECT].u.u64),
1274 U2H(stats[ST_F_REUSE].u.u64),
1275 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1276 (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 +01001277 U2H(stats[ST_F_REQ_TOT].u.u64),
1278 U2H(stats[ST_F_HRSP_1XX].u.u64),
1279 U2H(stats[ST_F_HRSP_2XX].u.u64),
1280 U2H(stats[ST_F_COMP_RSP].u.u64),
1281 stats[ST_F_HRSP_2XX].u.u64 ?
1282 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1283 U2H(stats[ST_F_HRSP_3XX].u.u64),
1284 U2H(stats[ST_F_HRSP_4XX].u.u64),
1285 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001286 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001287 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1288 U2H(stats[ST_F_CACHE_HITS].u.u64),
1289 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1290 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001291 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001292 }
1293
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001294 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1295 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1296 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1297 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1298 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001299 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001300 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1301 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1302 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1303 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001304
1305 chunk_appendf(out,
1306 "</table></div></u></td>"
1307 /* sessions: lbtot, last */
1308 "<td>%s</td><td>%s</td>"
1309 /* bytes: in */
1310 "<td>%s</td>"
1311 "",
1312 U2H(stats[ST_F_LBTOT].u.u64),
1313 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1314 U2H(stats[ST_F_BIN].u.u64));
1315
1316 chunk_appendf(out,
1317 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1318 "<td>%s%s<div class=tips><table class=det>"
1319 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1320 "<tr><th>Compression in:</th><td>%s</td></tr>"
1321 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1322 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1323 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1324 "</table></div>%s</td>",
1325 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1326 U2H(stats[ST_F_BOUT].u.u64),
1327 U2H(stats[ST_F_BOUT].u.u64),
1328 U2H(stats[ST_F_COMP_IN].u.u64),
1329 U2H(stats[ST_F_COMP_OUT].u.u64),
1330 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1331 U2H(stats[ST_F_COMP_BYP].u.u64),
1332 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1333 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,
1334 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1335
1336 chunk_appendf(out,
1337 /* denied: req, resp */
1338 "<td>%s</td><td>%s</td>"
1339 /* errors : request, connect */
1340 "<td></td><td>%s</td>"
1341 /* errors : response */
1342 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1343 /* warnings: retries, redispatches */
1344 "<td>%lld</td><td>%lld</td>"
1345 /* backend status: reflect backend status (up/down): we display UP
1346 * if the backend has known working servers or if it has no server at
1347 * all (eg: for stats). Then we display the total weight, number of
1348 * active and backups. */
1349 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1350 "<td class=ac>%d</td><td class=ac>%d</td>"
1351 "",
1352 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1353 U2H(stats[ST_F_ECON].u.u64),
1354 U2H(stats[ST_F_ERESP].u.u64),
1355 (long long)stats[ST_F_CLI_ABRT].u.u64,
1356 (long long)stats[ST_F_SRV_ABRT].u.u64,
1357 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1358 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1359 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1360 stats[ST_F_WEIGHT].u.u32,
1361 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1362
1363 chunk_appendf(out,
1364 /* rest of backend: nothing, down transitions, total downtime, throttle */
1365 "<td class=ac>&nbsp;</td><td>%d</td>"
1366 "<td>%s</td>"
1367 "<td></td>"
1368 "</tr>",
1369 stats[ST_F_CHKDOWN].u.u32,
1370 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1371 }
1372 return 1;
1373}
1374
1375int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1376{
Simon Horman05ee2132017-01-04 09:37:25 +01001377 int ret;
1378
William Lallemand74c24fb2016-11-21 17:18:36 +01001379 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1380 flags |= ST_SHOWADMIN;
1381
1382 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001383 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001384 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001385 ret = stats_dump_fields_typed(&trash, stats);
1386 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1387 ret = stats_dump_fields_json(&trash, stats,
1388 !(appctx->ctx.stats.flags &
1389 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001390 else
Simon Horman05ee2132017-01-04 09:37:25 +01001391 ret = stats_dump_fields_csv(&trash, stats);
1392
1393 if (ret)
1394 appctx->ctx.stats.flags |= STAT_STARTED;
1395
1396 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001397}
1398
1399/* Fill <stats> with the frontend statistics. <stats> is
1400 * preallocated array of length <len>. The length of the array
1401 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1402 * this value, the function returns 0, otherwise, it returns 1.
1403 */
1404int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1405{
1406 if (len < ST_F_TOTAL_FIELDS)
1407 return 0;
1408
1409 memset(stats, 0, sizeof(*stats) * len);
1410
1411 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1412 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1413 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1414 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1415 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1416 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1417 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1418 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1419 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1420 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1421 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1422 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1423 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1424 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1425 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1426 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1427 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1428 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1429 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1430 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1431 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1432 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001433 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001434
1435 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1436 if (px->mode == PR_MODE_HTTP) {
1437 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1438 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1439 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1440 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1441 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1442 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1443 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001444 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1445 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001446 }
1447
1448 /* requests : req_rate, req_rate_max, req_tot, */
1449 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1450 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1451 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1452
1453 /* compression: in, out, bypassed, responses */
1454 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1455 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1456 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1457 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1458
1459 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1460 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1461 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1462 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1463
1464 return 1;
1465}
1466
1467/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1468 * the state from stream interface <si>. The caller is responsible for clearing
1469 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1470 */
1471static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1472{
1473 struct appctx *appctx = __objt_appctx(si->end);
1474
1475 if (!(px->cap & PR_CAP_FE))
1476 return 0;
1477
1478 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1479 return 0;
1480
1481 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1482 return 0;
1483
1484 return stats_dump_one_line(stats, 0, px, appctx);
1485}
1486
1487/* Fill <stats> with the listener statistics. <stats> is
1488 * preallocated array of length <len>. The length of the array
1489 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1490 * then this value, the function returns 0, otherwise, it
1491 * returns 1. <flags> can take the value ST_SHLGNDS.
1492 */
1493int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1494 struct field *stats, int len)
1495{
Willy Tarreau83061a82018-07-13 11:56:34 +02001496 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001497
1498 if (len < ST_F_TOTAL_FIELDS)
1499 return 0;
1500
1501 if (!l->counters)
1502 return 0;
1503
1504 chunk_reset(out);
1505 memset(stats, 0, sizeof(*stats) * len);
1506
1507 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1508 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1509 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1510 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1511 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1512 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1513 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1514 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1515 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1516 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1517 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1518 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1519 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1520 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001521 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 +01001522 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1523 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1524 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1525 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001526 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001527
1528 if (flags & ST_SHLGNDS) {
1529 char str[INET6_ADDRSTRLEN];
1530 int port;
1531
1532 port = get_host_port(&l->addr);
1533 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1534 case AF_INET:
1535 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1536 chunk_appendf(out, "%s:%d", str, port);
1537 break;
1538 case AF_INET6:
1539 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1540 chunk_appendf(out, "[%s]:%d", str, port);
1541 break;
1542 case AF_UNIX:
1543 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1544 break;
1545 case -1:
1546 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1547 chunk_strcat(out, strerror(errno));
1548 break;
1549 default: /* address family not supported */
1550 break;
1551 }
1552 }
1553
1554 return 1;
1555}
1556
1557/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1558 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1559 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1560 * otherwise.
1561 */
1562static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1563{
1564 struct appctx *appctx = __objt_appctx(si->end);
1565
1566 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1567 return 0;
1568
1569 return stats_dump_one_line(stats, flags, px, appctx);
1570}
1571
1572enum srv_stats_state {
1573 SRV_STATS_STATE_DOWN = 0,
1574 SRV_STATS_STATE_DOWN_AGENT,
1575 SRV_STATS_STATE_GOING_UP,
1576 SRV_STATS_STATE_UP_GOING_DOWN,
1577 SRV_STATS_STATE_UP,
1578 SRV_STATS_STATE_NOLB_GOING_DOWN,
1579 SRV_STATS_STATE_NOLB,
1580 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1581 SRV_STATS_STATE_DRAIN,
1582 SRV_STATS_STATE_DRAIN_AGENT,
1583 SRV_STATS_STATE_NO_CHECK,
1584
1585 SRV_STATS_STATE_COUNT, /* Must be last */
1586};
1587
1588static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1589 [SRV_STATS_STATE_DOWN] = "DOWN",
1590 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1591 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1592 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1593 [SRV_STATS_STATE_UP] = "UP",
1594 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1595 [SRV_STATS_STATE_NOLB] = "NOLB",
1596 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1597 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1598 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1599 [SRV_STATS_STATE_NO_CHECK] = "no check"
1600};
1601
1602/* Fill <stats> with the server statistics. <stats> is
1603 * preallocated array of length <len>. The length of the array
1604 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1605 * then this value, the function returns 0, otherwise, it
1606 * returns 1. <flags> can take the value ST_SHLGNDS.
1607 */
1608int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1609 struct field *stats, int len)
1610{
1611 struct server *via, *ref;
1612 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001613 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001614 enum srv_stats_state state;
1615 char *fld_status;
1616
1617 if (len < ST_F_TOTAL_FIELDS)
1618 return 0;
1619
1620 memset(stats, 0, sizeof(*stats) * len);
1621
1622 /* we have "via" which is the tracked server as described in the configuration,
1623 * and "ref" which is the checked server and the end of the chain.
1624 */
1625 via = sv->track ? sv->track : sv;
1626 ref = via;
1627 while (ref->track)
1628 ref = ref->track;
1629
Emeric Brun52a91d32017-08-31 14:41:55 +02001630 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001631 if ((ref->check.state & CHK_ST_ENABLED) &&
1632 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1633 state = SRV_STATS_STATE_UP_GOING_DOWN;
1634 } else {
1635 state = SRV_STATS_STATE_UP;
1636 }
1637
Emeric Brun52a91d32017-08-31 14:41:55 +02001638 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001639 if (ref->agent.state & CHK_ST_ENABLED)
1640 state = SRV_STATS_STATE_DRAIN_AGENT;
1641 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1642 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1643 else
1644 state = SRV_STATS_STATE_DRAIN;
1645 }
1646
1647 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1648 state = SRV_STATS_STATE_NO_CHECK;
1649 }
1650 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001651 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001652 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1653 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1654 state = SRV_STATS_STATE_NOLB;
1655 } else {
1656 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1657 }
1658 }
1659 else { /* stopped */
1660 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1661 state = SRV_STATS_STATE_DOWN_AGENT;
1662 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1663 state = SRV_STATS_STATE_DOWN; /* DOWN */
1664 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1665 state = SRV_STATS_STATE_GOING_UP;
1666 } else {
1667 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1668 }
1669 }
1670
1671 chunk_reset(out);
1672
1673 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1674 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1675 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1676 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1677 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1678 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1679 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1680
1681 if (sv->maxconn)
1682 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1683
Willy Tarreau0ff699e2019-09-08 09:24:56 +02001684 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1685 if (sv->max_idle_conns != -1)
1686 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1687
William Lallemand74c24fb2016-11-21 17:18:36 +01001688 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1689 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1690 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1691 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1692 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1693 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1694 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1695 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001696 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001697 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1698 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001699
1700 /* status */
1701 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001702 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001703 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001704 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001705 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001706 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001707 chunk_appendf(out, "MAINT");
1708 else
1709 chunk_appendf(out,
1710 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001711 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1712 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001713
1714 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1715 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001716 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 +01001717 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1718 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1719
1720 /* check failures: unique, fatal; last change, total downtime */
1721 if (sv->check.state & CHK_ST_ENABLED) {
1722 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1723 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1724 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1725 }
1726
1727 if (sv->maxqueue)
1728 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1729
1730 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1731 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1732 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1733
Emeric Brun52a91d32017-08-31 14:41:55 +02001734 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001735 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1736
1737 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1738
1739 if (sv->track) {
1740 char *fld_track = chunk_newstr(out);
1741
1742 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1743 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1744 }
1745
1746 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1747 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1748 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1749
1750 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1751 const char *fld_chksts;
1752
1753 fld_chksts = chunk_newstr(out);
1754 chunk_strcat(out, "* "); // for check in progress
1755 chunk_strcat(out, get_check_status_info(sv->check.status));
1756 if (!(sv->check.state & CHK_ST_INPROGRESS))
1757 fld_chksts += 2; // skip "* "
1758 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1759
1760 if (sv->check.status >= HCHK_STATUS_L57DATA)
1761 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1762
1763 if (sv->check.status >= HCHK_STATUS_CHECKED)
1764 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1765
1766 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1767 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1768 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1769 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1770 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1771 }
1772
1773 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1774 const char *fld_chksts;
1775
1776 fld_chksts = chunk_newstr(out);
1777 chunk_strcat(out, "* "); // for check in progress
1778 chunk_strcat(out, get_check_status_info(sv->agent.status));
1779 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1780 fld_chksts += 2; // skip "* "
1781 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1782
1783 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1784 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1785
1786 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1787 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1788
1789 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1790 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1791 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1792 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1793 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1794 }
1795
1796 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1797 if (px->mode == PR_MODE_HTTP) {
1798 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1799 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1800 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1801 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1802 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1803 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1804 }
1805
1806 if (ref->observe)
1807 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1808
1809 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1810 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1811 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1812
1813 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1814 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1815 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1816 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1817
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001818 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1819 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1820 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1821 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1822
William Lallemand74c24fb2016-11-21 17:18:36 +01001823 if (flags & ST_SHLGNDS) {
1824 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1825 case AF_INET:
1826 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001827 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001828 break;
1829 case AF_INET6:
1830 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001831 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001832 break;
1833 case AF_UNIX:
1834 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1835 break;
1836 case -1:
1837 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1838 chunk_strcat(out, strerror(errno));
1839 break;
1840 default: /* address family not supported */
1841 break;
1842 }
1843
1844 if (sv->cookie)
1845 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1846 }
1847
1848 return 1;
1849}
1850
1851/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1852 * from stream interface <si>, stats flags <flags>, and server state <state>.
1853 * The caller is responsible for clearing the trash if needed. Returns non-zero
1854 * if it emits anything, zero otherwise.
1855 */
1856static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1857{
1858 struct appctx *appctx = __objt_appctx(si->end);
1859
1860 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1861 return 0;
1862
1863 return stats_dump_one_line(stats, flags, px, appctx);
1864}
1865
1866/* Fill <stats> with the backend statistics. <stats> is
1867 * preallocated array of length <len>. The length of the array
1868 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1869 * then this value, the function returns 0, otherwise, it
1870 * returns 1. <flags> can take the value ST_SHLGNDS.
1871 */
1872int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1873{
1874 if (len < ST_F_TOTAL_FIELDS)
1875 return 0;
1876
1877 memset(stats, 0, sizeof(*stats) * len);
1878
1879 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1880 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1881 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1882 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1883 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001884 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001885 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1886 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1887 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1888 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1889 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1890 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1891 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1892 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1893 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1894 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1895 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001896 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001897 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1898 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001899 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1900 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1901 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1902 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1903 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1904 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1905 if (px->srv)
1906 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1907
1908 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1909 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1910 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1911 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1912 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1913 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1914 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1915
1916 if (flags & ST_SHLGNDS) {
1917 if (px->cookie_name)
1918 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1919 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1920 }
1921
1922 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1923 if (px->mode == PR_MODE_HTTP) {
1924 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1925 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1926 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1927 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1928 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1929 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1930 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001931 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1932 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001933 }
1934
1935 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1936 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1937
1938 /* compression: in, out, bypassed, responses */
1939 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1940 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1941 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1942 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1943 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1944
1945 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1946 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1947 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1948 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1949
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001950 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1951 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1952 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1953 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1954
William Lallemand74c24fb2016-11-21 17:18:36 +01001955 return 1;
1956}
1957
1958/* Dumps a line for backend <px> to the trash for and uses the state from stream
1959 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1960 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1961 */
1962static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1963{
1964 struct appctx *appctx = __objt_appctx(si->end);
1965
1966 if (!(px->cap & PR_CAP_BE))
1967 return 0;
1968
1969 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1970 return 0;
1971
1972 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1973 return 0;
1974
1975 return stats_dump_one_line(stats, flags, px, appctx);
1976}
1977
1978/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1979 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1980 * for clearing the trash if needed.
1981 */
1982static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1983{
1984 struct appctx *appctx = __objt_appctx(si->end);
1985 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1986
1987 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1988 /* A form to enable/disable this proxy servers */
1989
1990 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1991 scope_txt[0] = 0;
1992 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001993 const char *scope_ptr = stats_scope_ptr(appctx, si);
1994
William Lallemand74c24fb2016-11-21 17:18:36 +01001995 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001996 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001997 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1998 }
1999
2000 chunk_appendf(&trash,
2001 "<form method=\"post\">");
2002 }
2003
2004 /* print a new table */
2005 chunk_appendf(&trash,
2006 "<table class=\"tbl\" width=\"100%%\">\n"
2007 "<tr class=\"titre\">"
2008 "<th class=\"pxname\" width=\"10%%\">");
2009
2010 chunk_appendf(&trash,
2011 "<a name=\"%s\"></a>%s"
2012 "<a class=px href=\"#%s\">%s</a>",
2013 px->id,
2014 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2015 px->id, px->id);
2016
2017 if (uri->flags & ST_SHLGNDS) {
2018 /* cap, mode, id */
2019 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2020 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2021 px->uuid);
2022 chunk_appendf(&trash, "</div>");
2023 }
2024
2025 chunk_appendf(&trash,
2026 "%s</th>"
2027 "<th class=\"%s\" width=\"90%%\">%s</th>"
2028 "</tr>\n"
2029 "</table>\n"
2030 "<table class=\"tbl\" width=\"100%%\">\n"
2031 "<tr class=\"titre\">",
2032 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2033 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2034
2035 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2036 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00002037 chunk_appendf(&trash,
2038 "<th rowspan=2 width=1><input type=\"checkbox\" \
2039 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
2040 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2041 px->id,
2042 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01002043 }
2044
2045 chunk_appendf(&trash,
2046 "<th rowspan=2></th>"
2047 "<th colspan=3>Queue</th>"
2048 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2049 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2050 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2051 "<th colspan=9>Server</th>"
2052 "</tr>\n"
2053 "<tr class=\"titre\">"
2054 "<th>Cur</th><th>Max</th><th>Limit</th>"
2055 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2056 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2057 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2058 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2059 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2060 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2061 "<th>Thrtle</th>\n"
2062 "</tr>");
2063}
2064
2065/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2066 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2067 */
2068static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2069{
2070 struct appctx *appctx = __objt_appctx(si->end);
2071 chunk_appendf(&trash, "</table>");
2072
2073 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2074 /* close the form used to enable/disable this proxy servers */
2075 chunk_appendf(&trash,
2076 "Choose the action to perform on the checked servers : "
2077 "<select name=action>"
2078 "<option value=\"\"></option>"
2079 "<option value=\"ready\">Set state to READY</option>"
2080 "<option value=\"drain\">Set state to DRAIN</option>"
2081 "<option value=\"maint\">Set state to MAINT</option>"
2082 "<option value=\"dhlth\">Health: disable checks</option>"
2083 "<option value=\"ehlth\">Health: enable checks</option>"
2084 "<option value=\"hrunn\">Health: force UP</option>"
2085 "<option value=\"hnolb\">Health: force NOLB</option>"
2086 "<option value=\"hdown\">Health: force DOWN</option>"
2087 "<option value=\"dagent\">Agent: disable checks</option>"
2088 "<option value=\"eagent\">Agent: enable checks</option>"
2089 "<option value=\"arunn\">Agent: force UP</option>"
2090 "<option value=\"adown\">Agent: force DOWN</option>"
2091 "<option value=\"shutdown\">Kill Sessions</option>"
2092 "</select>"
2093 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2094 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2095 "</form>",
2096 px->uuid);
2097 }
2098
2099 chunk_appendf(&trash, "<p>\n");
2100}
2101
2102/*
2103 * Dumps statistics for a proxy. The output is sent to the stream interface's
2104 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2105 * buffer space, or non-zero if everything completed. This function is used
2106 * both by the CLI and the HTTP entry points, and is able to dump the output
2107 * in HTML or CSV formats. If the later, <uri> must be NULL.
2108 */
Christopher Fauletef779222018-10-31 08:47:01 +01002109int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2110 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002111{
2112 struct appctx *appctx = __objt_appctx(si->end);
2113 struct stream *s = si_strm(si);
2114 struct channel *rep = si_ic(si);
2115 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2116 struct listener *l;
2117 unsigned int flags;
2118
2119 if (uri)
2120 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002121 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002122 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2123 else
2124 flags = ST_SHNODE | ST_SHDESC;
2125
2126 chunk_reset(&trash);
2127
2128 switch (appctx->ctx.stats.px_st) {
2129 case STAT_PX_ST_INIT:
2130 /* we are on a new proxy */
2131 if (uri && uri->scope) {
2132 /* we have a limited scope, we have to check the proxy name */
2133 struct stat_scope *scope;
2134 int len;
2135
2136 len = strlen(px->id);
2137 scope = uri->scope;
2138
2139 while (scope) {
2140 /* match exact proxy name */
2141 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2142 break;
2143
2144 /* match '.' which means 'self' proxy */
2145 if (!strcmp(scope->px_id, ".") && px == s->be)
2146 break;
2147 scope = scope->next;
2148 }
2149
2150 /* proxy name not found : don't dump anything */
2151 if (scope == NULL)
2152 return 1;
2153 }
2154
2155 /* if the user has requested a limited output and the proxy
2156 * name does not match, skip it.
2157 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002158 if (appctx->ctx.stats.scope_len) {
2159 const char *scope_ptr = stats_scope_ptr(appctx, si);
2160
2161 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2162 return 1;
2163 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002164
2165 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2166 (appctx->ctx.stats.iid != -1) &&
2167 (px->uuid != appctx->ctx.stats.iid))
2168 return 1;
2169
2170 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2171 /* fall through */
2172
2173 case STAT_PX_ST_TH:
2174 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2175 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002176 if (!stats_putchk(rep, htx, &trash))
2177 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002178 }
2179
2180 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2181 /* fall through */
2182
2183 case STAT_PX_ST_FE:
2184 /* print the frontend */
2185 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002186 if (!stats_putchk(rep, htx, &trash))
2187 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002188 }
2189
2190 appctx->ctx.stats.l = px->conf.listeners.n;
2191 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2192 /* fall through */
2193
2194 case STAT_PX_ST_LI:
2195 /* stats.l has been initialized above */
2196 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002197 if (htx) {
2198 if (htx_almost_full(htx))
2199 goto full;
2200 }
2201 else {
2202 if (buffer_almost_full(&rep->buf))
2203 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002204 }
2205
2206 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2207 if (!l->counters)
2208 continue;
2209
2210 if (appctx->ctx.stats.flags & STAT_BOUND) {
2211 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2212 break;
2213
2214 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2215 continue;
2216 }
2217
2218 /* print the frontend */
2219 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002220 if (!stats_putchk(rep, htx, &trash))
2221 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002222 }
2223 }
2224
2225 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2226 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2227 /* fall through */
2228
2229 case STAT_PX_ST_SV:
2230 /* stats.sv has been initialized above */
2231 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002232 if (htx) {
2233 if (htx_almost_full(htx))
2234 goto full;
2235 }
2236 else {
2237 if (buffer_almost_full(&rep->buf))
2238 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002239 }
2240
2241 sv = appctx->ctx.stats.sv;
2242
2243 if (appctx->ctx.stats.flags & STAT_BOUND) {
2244 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2245 break;
2246
2247 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2248 continue;
2249 }
2250
2251 svs = sv;
2252 while (svs->track)
2253 svs = svs->track;
2254
2255 /* do not report servers which are DOWN and not changing state */
2256 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002257 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2258 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002259 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2260 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2261 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2262 continue;
2263 }
2264
2265 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002266 if (!stats_putchk(rep, htx, &trash))
2267 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002268 }
2269 } /* for sv */
2270
2271 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2272 /* fall through */
2273
2274 case STAT_PX_ST_BE:
2275 /* print the backend */
2276 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002277 if (!stats_putchk(rep, htx, &trash))
2278 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002279 }
2280
2281 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2282 /* fall through */
2283
2284 case STAT_PX_ST_END:
2285 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2286 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002287 if (!stats_putchk(rep, htx, &trash))
2288 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002289 }
2290
2291 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2292 /* fall through */
2293
2294 case STAT_PX_ST_FIN:
2295 return 1;
2296
2297 default:
2298 /* unknown state, we should put an abort() here ! */
2299 return 1;
2300 }
Christopher Fauletef779222018-10-31 08:47:01 +01002301
2302 full:
2303 si_rx_room_blk(si);
2304 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002305}
2306
2307/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2308 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2309 */
2310static void stats_dump_html_head(struct uri_auth *uri)
2311{
2312 /* WARNING! This must fit in the first buffer !!! */
2313 chunk_appendf(&trash,
2314 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2315 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2316 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2317 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2318 "<style type=\"text/css\"><!--\n"
2319 "body {"
2320 " font-family: arial, helvetica, sans-serif;"
2321 " font-size: 12px;"
2322 " font-weight: normal;"
2323 " color: black;"
2324 " background: white;"
2325 "}\n"
2326 "th,td {"
2327 " font-size: 10px;"
2328 "}\n"
2329 "h1 {"
2330 " font-size: x-large;"
2331 " margin-bottom: 0.5em;"
2332 "}\n"
2333 "h2 {"
2334 " font-family: helvetica, arial;"
2335 " font-size: x-large;"
2336 " font-weight: bold;"
2337 " font-style: italic;"
2338 " color: #6020a0;"
2339 " margin-top: 0em;"
2340 " margin-bottom: 0em;"
2341 "}\n"
2342 "h3 {"
2343 " font-family: helvetica, arial;"
2344 " font-size: 16px;"
2345 " font-weight: bold;"
2346 " color: #b00040;"
2347 " background: #e8e8d0;"
2348 " margin-top: 0em;"
2349 " margin-bottom: 0em;"
2350 "}\n"
2351 "li {"
2352 " margin-top: 0.25em;"
2353 " margin-right: 2em;"
2354 "}\n"
2355 ".hr {margin-top: 0.25em;"
2356 " border-color: black;"
2357 " border-bottom-style: solid;"
2358 "}\n"
2359 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2360 ".total {background: #20D0D0;color: #ffff80;}\n"
2361 ".frontend {background: #e8e8d0;}\n"
2362 ".socket {background: #d0d0d0;}\n"
2363 ".backend {background: #e8e8d0;}\n"
2364 ".active_down {background: #ff9090;}\n"
2365 ".active_going_up {background: #ffd020;}\n"
2366 ".active_going_down {background: #ffffa0;}\n"
2367 ".active_up {background: #c0ffc0;}\n"
2368 ".active_nolb {background: #20a0ff;}\n"
2369 ".active_draining {background: #20a0FF;}\n"
2370 ".active_no_check {background: #e0e0e0;}\n"
2371 ".backup_down {background: #ff9090;}\n"
2372 ".backup_going_up {background: #ff80ff;}\n"
2373 ".backup_going_down {background: #c060ff;}\n"
2374 ".backup_up {background: #b0d0ff;}\n"
2375 ".backup_nolb {background: #90b0e0;}\n"
2376 ".backup_draining {background: #cc9900;}\n"
2377 ".backup_no_check {background: #e0e0e0;}\n"
2378 ".maintain {background: #c07820;}\n"
2379 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2380 "\n"
2381 "a.px:link {color: #ffff40; text-decoration: none;}"
2382 "a.px:visited {color: #ffff40; text-decoration: none;}"
2383 "a.px:hover {color: #ffffff; text-decoration: none;}"
2384 "a.lfsb:link {color: #000000; text-decoration: none;}"
2385 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2386 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2387 "\n"
2388 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2389 "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"
2390 "table.tbl td.ac { text-align: center;}\n"
2391 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2392 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2393 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2394 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2395 "\n"
2396 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2397 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2398 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2399 "table.det { border-collapse: collapse; border-style: none; }\n"
2400 "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"
2401 "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"
2402 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2403 "div.tips {\n"
2404 " display:block;\n"
2405 " visibility:hidden;\n"
2406 " z-index:2147483647;\n"
2407 " position:absolute;\n"
2408 " padding:2px 4px 3px;\n"
2409 " background:#f0f060; color:#000000;\n"
2410 " border:1px solid #7040c0;\n"
2411 " white-space:nowrap;\n"
2412 " font-style:normal;font-size:11px;font-weight:normal;\n"
2413 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2414 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2415 "}\n"
2416 "u:hover div.tips {visibility:visible;}\n"
2417 "-->\n"
2418 "</style></head>\n",
2419 (uri->flags & ST_SHNODE) ? " on " : "",
2420 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2421 );
2422}
2423
2424/* Dumps the HTML stats information block to the trash for and uses the state from
2425 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2426 * for clearing the trash if needed.
2427 */
2428static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2429{
2430 struct appctx *appctx = __objt_appctx(si->end);
2431 unsigned int up = (now.tv_sec - start_date.tv_sec);
2432 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002433 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002434 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2435
2436 /* Turn the bytes per second to bits per second and take care of the
2437 * usual ethernet overhead in order to help figure how far we are from
2438 * interface saturation since it's the only case which usually matters.
2439 * For this we count the total size of an Ethernet frame on the wire
2440 * including preamble and IFG (1538) for the largest TCP segment it
2441 * transports (1448 with TCP timestamps). This is not valid for smaller
2442 * packets (under-estimated), but it gives a reasonably accurate
2443 * estimation of how far we are from uplink saturation.
2444 */
2445 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002446
2447 /* WARNING! this has to fit the first packet too.
2448 * We are around 3.5 kB, add adding entries will
2449 * become tricky if we want to support 4kB buffers !
2450 */
2451 chunk_appendf(&trash,
2452 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2453 PRODUCT_NAME "%s</a></h1>\n"
2454 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2455 "<hr width=\"100%%\" class=\"hr\">\n"
2456 "<h3>&gt; General process information</h3>\n"
2457 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002458 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002459 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2460 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2461 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002462 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002463 "Running tasks: %d/%d; idle = %d %%<br>\n"
2464 "</td><td align=\"center\" nowrap>\n"
2465 "<table class=\"lgd\"><tr>\n"
2466 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2467 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2468 "</tr><tr>\n"
2469 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2470 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2471 "</tr><tr>\n"
2472 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2473 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2474 "</tr><tr>\n"
2475 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2476 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2477 "</tr><tr>\n"
2478 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2479 "</tr><tr>\n"
2480 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2481 "</tr></table>\n"
2482 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2483 "</td>"
2484 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2485 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2486 "",
Willy Tarreau909b9d82019-01-04 18:20:32 +01002487 (uri->flags & ST_HIDEVER) ? "" : (stats_version_string),
William Lallemand74c24fb2016-11-21 17:18:36 +01002488 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2489 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2490 (uri->flags & ST_SHDESC) ? ": " : "",
2491 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002492 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002493 up / 86400, (up % 86400) / 3600,
2494 (up % 3600) / 60, (up % 60),
2495 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2496 global.rlimit_memmax ? " MB" : "",
2497 global.rlimit_nofile,
2498 global.maxsock, global.maxconn, global.maxpipes,
2499 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002500 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2501 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002502 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002503 );
2504
2505 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002506 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002507 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2508
2509 chunk_appendf(&trash,
2510 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2511 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2512 STAT_SCOPE_TXT_MAXLEN);
2513
2514 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2515 scope_txt[0] = 0;
2516 if (appctx->ctx.stats.scope_len) {
2517 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002518 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002519 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2520 }
2521
2522 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2523 chunk_appendf(&trash,
2524 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2525 uri->uri_prefix,
2526 "",
2527 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2528 scope_txt);
2529 else
2530 chunk_appendf(&trash,
2531 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2532 uri->uri_prefix,
2533 ";up",
2534 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2535 scope_txt);
2536
2537 if (uri->refresh > 0) {
2538 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2539 chunk_appendf(&trash,
2540 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2541 uri->uri_prefix,
2542 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2543 "",
2544 scope_txt);
2545 else
2546 chunk_appendf(&trash,
2547 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2548 uri->uri_prefix,
2549 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2550 ";norefresh",
2551 scope_txt);
2552 }
2553
2554 chunk_appendf(&trash,
2555 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2556 uri->uri_prefix,
2557 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2558 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2559 scope_txt);
2560
2561 chunk_appendf(&trash,
2562 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2563 uri->uri_prefix,
2564 (uri->refresh > 0) ? ";norefresh" : "",
2565 scope_txt);
2566
2567 chunk_appendf(&trash,
2568 "</ul></td>"
2569 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2570 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2571 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2572 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2573 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2574 "</ul>"
2575 "</td>"
2576 "</tr></table>\n"
2577 ""
2578 );
2579
2580 if (appctx->ctx.stats.st_code) {
2581 switch (appctx->ctx.stats.st_code) {
2582 case STAT_STATUS_DONE:
2583 chunk_appendf(&trash,
2584 "<p><div class=active_up>"
2585 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2586 "Action processed successfully."
2587 "</div>\n", uri->uri_prefix,
2588 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2589 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2590 scope_txt);
2591 break;
2592 case STAT_STATUS_NONE:
2593 chunk_appendf(&trash,
2594 "<p><div class=active_going_down>"
2595 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2596 "Nothing has changed."
2597 "</div>\n", uri->uri_prefix,
2598 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2599 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2600 scope_txt);
2601 break;
2602 case STAT_STATUS_PART:
2603 chunk_appendf(&trash,
2604 "<p><div class=active_going_down>"
2605 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2606 "Action partially processed.<br>"
2607 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2608 "</div>\n", uri->uri_prefix,
2609 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2610 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2611 scope_txt);
2612 break;
2613 case STAT_STATUS_ERRP:
2614 chunk_appendf(&trash,
2615 "<p><div class=active_down>"
2616 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2617 "Action not processed because of invalid parameters."
2618 "<ul>"
2619 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002620 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002621 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2622 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2623 "</ul>"
2624 "</div>\n", uri->uri_prefix,
2625 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2626 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2627 scope_txt);
2628 break;
2629 case STAT_STATUS_EXCD:
2630 chunk_appendf(&trash,
2631 "<p><div class=active_down>"
2632 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2633 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2634 "You should retry with less servers at a time.</b>"
2635 "</div>\n", uri->uri_prefix,
2636 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2637 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2638 scope_txt);
2639 break;
2640 case STAT_STATUS_DENY:
2641 chunk_appendf(&trash,
2642 "<p><div class=active_down>"
2643 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2644 "<b>Action denied.</b>"
2645 "</div>\n", uri->uri_prefix,
2646 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2647 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2648 scope_txt);
2649 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002650 case STAT_STATUS_IVAL:
2651 chunk_appendf(&trash,
2652 "<p><div class=active_down>"
2653 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2654 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2655 "</div>\n", uri->uri_prefix,
2656 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2657 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2658 scope_txt);
2659 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002660 default:
2661 chunk_appendf(&trash,
2662 "<p><div class=active_no_check>"
2663 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2664 "Unexpected result."
2665 "</div>\n", uri->uri_prefix,
2666 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2667 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2668 scope_txt);
2669 }
2670 chunk_appendf(&trash, "<p>\n");
2671 }
2672}
2673
2674/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2675 * for clearing the trash if needed.
2676 */
2677static void stats_dump_html_end()
2678{
2679 chunk_appendf(&trash, "</body></html>\n");
2680}
2681
Simon Horman05ee2132017-01-04 09:37:25 +01002682/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2683 * for clearing it if needed.
2684 */
2685static void stats_dump_json_header()
2686{
2687 chunk_strcat(&trash, "[");
2688}
2689
2690
2691/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2692 * for clearing the trash if needed.
2693 */
2694static void stats_dump_json_end()
2695{
2696 chunk_strcat(&trash, "]");
2697}
2698
William Lallemand74c24fb2016-11-21 17:18:36 +01002699/* This function dumps statistics onto the stream interface's read buffer in
2700 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2701 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2702 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2703 * and the stream must be closed, or -1 in case of any error. This function is
2704 * used by both the CLI and the HTTP handlers.
2705 */
Christopher Fauletef779222018-10-31 08:47:01 +01002706static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2707 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002708{
2709 struct appctx *appctx = __objt_appctx(si->end);
2710 struct channel *rep = si_ic(si);
2711 struct proxy *px;
2712
2713 chunk_reset(&trash);
2714
2715 switch (appctx->st2) {
2716 case STAT_ST_INIT:
2717 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2718 /* fall through */
2719
2720 case STAT_ST_HEAD:
2721 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2722 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002723 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002724 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002725 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2726 stats_dump_csv_header();
2727
Christopher Fauletef779222018-10-31 08:47:01 +01002728 if (!stats_putchk(rep, htx, &trash))
2729 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002730
2731 appctx->st2 = STAT_ST_INFO;
2732 /* fall through */
2733
2734 case STAT_ST_INFO:
2735 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2736 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002737 if (!stats_putchk(rep, htx, &trash))
2738 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002739 }
2740
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002741 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002742 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2743 appctx->st2 = STAT_ST_LIST;
2744 /* fall through */
2745
2746 case STAT_ST_LIST:
2747 /* dump proxies */
2748 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002749 if (htx) {
2750 if (htx_almost_full(htx))
2751 goto full;
2752 }
2753 else {
2754 if (buffer_almost_full(&rep->buf))
2755 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002756 }
2757
2758 px = appctx->ctx.stats.px;
2759 /* skip the disabled proxies, global frontend and non-networked ones */
2760 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002761 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002762 return 0;
2763
2764 appctx->ctx.stats.px = px->next;
2765 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2766 }
2767 /* here, we just have reached the last proxy */
2768
2769 appctx->st2 = STAT_ST_END;
2770 /* fall through */
2771
2772 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002773 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2774 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2775 stats_dump_html_end();
2776 else
2777 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002778 if (!stats_putchk(rep, htx, &trash))
2779 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002780 }
2781
2782 appctx->st2 = STAT_ST_FIN;
2783 /* fall through */
2784
2785 case STAT_ST_FIN:
2786 return 1;
2787
2788 default:
2789 /* unknown state ! */
2790 appctx->st2 = STAT_ST_FIN;
2791 return -1;
2792 }
Christopher Fauletef779222018-10-31 08:47:01 +01002793
2794 full:
2795 si_rx_room_blk(si);
2796 return 0;
2797
William Lallemand74c24fb2016-11-21 17:18:36 +01002798}
2799
2800/* We reached the stats page through a POST request. The appctx is
2801 * expected to have already been allocated by the caller.
2802 * Parse the posted data and enable/disable servers if necessary.
2803 * Returns 1 if request was parsed or zero if it needs more data.
2804 */
2805static int stats_process_http_post(struct stream_interface *si)
2806{
2807 struct stream *s = si_strm(si);
2808 struct appctx *appctx = objt_appctx(si->end);
2809
2810 struct proxy *px = NULL;
2811 struct server *sv = NULL;
2812
2813 char key[LINESIZE];
2814 int action = ST_ADM_ACTION_NONE;
2815 int reprocess = 0;
2816
2817 int total_servers = 0;
2818 int altered_servers = 0;
2819
2820 char *first_param, *cur_param, *next_param, *end_params;
2821 char *st_cur_param = NULL;
2822 char *st_next_param = NULL;
2823
Christopher Fauleta3618372018-12-13 21:59:56 +01002824 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002825
Christopher Fauleta3618372018-12-13 21:59:56 +01002826 if (IS_HTX_STRM(s)) {
2827 struct htx *htx = htxbuf(&s->req.buf);
2828 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002829
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002830 /* we need more data */
2831 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2832 /* check if we can receive more */
2833 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2834 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2835 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002836 }
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002837 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002838 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002839
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002840 /* The request was fully received. Copy data */
Christopher Faulet8fa60e42019-05-23 11:04:05 +02002841 blk = htx_get_head_blk(htx);
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002842 while (blk) {
Christopher Fauleta3618372018-12-13 21:59:56 +01002843 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002844
Christopher Faulet54b5e212019-06-04 10:08:28 +02002845 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta3618372018-12-13 21:59:56 +01002846 break;
2847 if (type == HTX_BLK_DATA) {
2848 struct ist v = htx_get_blk_value(htx, blk);
2849
2850 if (!chunk_memcat(temp, v.ptr, v.len)) {
2851 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2852 goto out;
2853 }
2854 }
Christopher Fauleted7a0662019-01-14 11:07:34 +01002855 blk = htx_get_next_blk(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002856 }
2857 }
2858 else {
2859 int reql;
2860
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002861 /* we need more data */
2862 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2863 /* check if we can receive more */
2864 if (c_room(&s->req) <= global.tune.maxrewrite) {
2865 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2866 goto out;
2867 }
2868 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002869 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002870 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2871 s->txn->req.eoh + 2);
2872 if (reql <= 0) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002873 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2874 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002875 }
2876 temp->data = reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002877 }
2878
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002879 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002880 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002881 cur_param = next_param = end_params;
2882 *end_params = '\0';
2883
2884 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2885
2886 /*
2887 * Parse the parameters in reverse order to only store the last value.
2888 * From the html form, the backend and the action are at the end.
2889 */
2890 while (cur_param > first_param) {
2891 char *value;
2892 int poffset, plen;
2893
2894 cur_param--;
2895
2896 if ((*cur_param == '&') || (cur_param == first_param)) {
2897 reprocess_servers:
2898 /* Parse the key */
2899 poffset = (cur_param != first_param ? 1 : 0);
2900 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2901 if ((plen > 0) && (plen <= sizeof(key))) {
2902 strncpy(key, cur_param + poffset, plen);
2903 key[plen - 1] = '\0';
2904 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002905 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002906 goto out;
2907 }
2908
2909 /* Parse the value */
2910 value = key;
2911 while (*value != '\0' && *value != '=') {
2912 value++;
2913 }
2914 if (*value == '=') {
2915 /* Ok, a value is found, we can mark the end of the key */
2916 *value++ = '\0';
2917 }
2918 if (url_decode(key) < 0 || url_decode(value) < 0)
2919 break;
2920
2921 /* Now we can check the key to see what to do */
2922 if (!px && (strcmp(key, "b") == 0)) {
2923 if ((px = proxy_be_by_name(value)) == NULL) {
2924 /* the backend name is unknown or ambiguous (duplicate names) */
2925 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2926 goto out;
2927 }
2928 }
2929 else if (!action && (strcmp(key, "action") == 0)) {
2930 if (strcmp(value, "ready") == 0) {
2931 action = ST_ADM_ACTION_READY;
2932 }
2933 else if (strcmp(value, "drain") == 0) {
2934 action = ST_ADM_ACTION_DRAIN;
2935 }
2936 else if (strcmp(value, "maint") == 0) {
2937 action = ST_ADM_ACTION_MAINT;
2938 }
2939 else if (strcmp(value, "shutdown") == 0) {
2940 action = ST_ADM_ACTION_SHUTDOWN;
2941 }
2942 else if (strcmp(value, "dhlth") == 0) {
2943 action = ST_ADM_ACTION_DHLTH;
2944 }
2945 else if (strcmp(value, "ehlth") == 0) {
2946 action = ST_ADM_ACTION_EHLTH;
2947 }
2948 else if (strcmp(value, "hrunn") == 0) {
2949 action = ST_ADM_ACTION_HRUNN;
2950 }
2951 else if (strcmp(value, "hnolb") == 0) {
2952 action = ST_ADM_ACTION_HNOLB;
2953 }
2954 else if (strcmp(value, "hdown") == 0) {
2955 action = ST_ADM_ACTION_HDOWN;
2956 }
2957 else if (strcmp(value, "dagent") == 0) {
2958 action = ST_ADM_ACTION_DAGENT;
2959 }
2960 else if (strcmp(value, "eagent") == 0) {
2961 action = ST_ADM_ACTION_EAGENT;
2962 }
2963 else if (strcmp(value, "arunn") == 0) {
2964 action = ST_ADM_ACTION_ARUNN;
2965 }
2966 else if (strcmp(value, "adown") == 0) {
2967 action = ST_ADM_ACTION_ADOWN;
2968 }
2969 /* else these are the old supported methods */
2970 else if (strcmp(value, "disable") == 0) {
2971 action = ST_ADM_ACTION_DISABLE;
2972 }
2973 else if (strcmp(value, "enable") == 0) {
2974 action = ST_ADM_ACTION_ENABLE;
2975 }
2976 else if (strcmp(value, "stop") == 0) {
2977 action = ST_ADM_ACTION_STOP;
2978 }
2979 else if (strcmp(value, "start") == 0) {
2980 action = ST_ADM_ACTION_START;
2981 }
2982 else {
2983 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2984 goto out;
2985 }
2986 }
2987 else if (strcmp(key, "s") == 0) {
2988 if (!(px && action)) {
2989 /*
2990 * Indicates that we'll need to reprocess the parameters
2991 * as soon as backend and action are known
2992 */
2993 if (!reprocess) {
2994 st_cur_param = cur_param;
2995 st_next_param = next_param;
2996 }
2997 reprocess = 1;
2998 }
2999 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003000 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003001 switch (action) {
3002 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003003 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003004 altered_servers++;
3005 total_servers++;
3006 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3007 }
3008 break;
3009 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003010 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003011 altered_servers++;
3012 total_servers++;
3013 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3014 }
3015 break;
3016 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003017 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003018 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3019 altered_servers++;
3020 total_servers++;
3021 }
3022 break;
3023 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003024 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003025 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3026 altered_servers++;
3027 total_servers++;
3028 }
3029 break;
3030 case ST_ADM_ACTION_DHLTH:
3031 if (sv->check.state & CHK_ST_CONFIGURED) {
3032 sv->check.state &= ~CHK_ST_ENABLED;
3033 altered_servers++;
3034 total_servers++;
3035 }
3036 break;
3037 case ST_ADM_ACTION_EHLTH:
3038 if (sv->check.state & CHK_ST_CONFIGURED) {
3039 sv->check.state |= CHK_ST_ENABLED;
3040 altered_servers++;
3041 total_servers++;
3042 }
3043 break;
3044 case ST_ADM_ACTION_HRUNN:
3045 if (!(sv->track)) {
3046 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003047 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003048 altered_servers++;
3049 total_servers++;
3050 }
3051 break;
3052 case ST_ADM_ACTION_HNOLB:
3053 if (!(sv->track)) {
3054 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003055 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003056 altered_servers++;
3057 total_servers++;
3058 }
3059 break;
3060 case ST_ADM_ACTION_HDOWN:
3061 if (!(sv->track)) {
3062 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003063 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003064 altered_servers++;
3065 total_servers++;
3066 }
3067 break;
3068 case ST_ADM_ACTION_DAGENT:
3069 if (sv->agent.state & CHK_ST_CONFIGURED) {
3070 sv->agent.state &= ~CHK_ST_ENABLED;
3071 altered_servers++;
3072 total_servers++;
3073 }
3074 break;
3075 case ST_ADM_ACTION_EAGENT:
3076 if (sv->agent.state & CHK_ST_CONFIGURED) {
3077 sv->agent.state |= CHK_ST_ENABLED;
3078 altered_servers++;
3079 total_servers++;
3080 }
3081 break;
3082 case ST_ADM_ACTION_ARUNN:
3083 if (sv->agent.state & CHK_ST_ENABLED) {
3084 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003085 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003086 altered_servers++;
3087 total_servers++;
3088 }
3089 break;
3090 case ST_ADM_ACTION_ADOWN:
3091 if (sv->agent.state & CHK_ST_ENABLED) {
3092 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003093 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003094 altered_servers++;
3095 total_servers++;
3096 }
3097 break;
3098 case ST_ADM_ACTION_READY:
3099 srv_adm_set_ready(sv);
3100 altered_servers++;
3101 total_servers++;
3102 break;
3103 case ST_ADM_ACTION_DRAIN:
3104 srv_adm_set_drain(sv);
3105 altered_servers++;
3106 total_servers++;
3107 break;
3108 case ST_ADM_ACTION_MAINT:
3109 srv_adm_set_maint(sv);
3110 altered_servers++;
3111 total_servers++;
3112 break;
3113 case ST_ADM_ACTION_SHUTDOWN:
3114 if (px->state != PR_STSTOPPED) {
3115 struct stream *sess, *sess_bck;
3116
3117 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3118 if (sess->srv_conn == sv)
3119 stream_shutdown(sess, SF_ERR_KILLED);
3120
3121 altered_servers++;
3122 total_servers++;
3123 }
3124 break;
3125 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003126 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003127 } else {
3128 /* the server name is unknown or ambiguous (duplicate names) */
3129 total_servers++;
3130 }
3131 }
3132 if (reprocess && px && action) {
3133 /* Now, we know the backend and the action chosen by the user.
3134 * We can safely restart from the first server parameter
3135 * to reprocess them
3136 */
3137 cur_param = st_cur_param;
3138 next_param = st_next_param;
3139 reprocess = 0;
3140 goto reprocess_servers;
3141 }
3142
3143 next_param = cur_param;
3144 }
3145 }
3146
3147 if (total_servers == 0) {
3148 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3149 }
3150 else if (altered_servers == 0) {
3151 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3152 }
3153 else if (altered_servers == total_servers) {
3154 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3155 }
3156 else {
3157 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3158 }
3159 out:
3160 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003161 wait:
3162 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3163 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003164}
3165
3166
3167static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
3168{
3169 struct stream *s = si_strm(si);
3170 struct uri_auth *uri = s->be->uri_auth;
3171 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003172 struct htx_sl *sl;
3173 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003174
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003175 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);
3176 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3177 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003178 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003179 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003180
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003181 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003182 goto full;
3183 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3184 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3185 goto full;
3186 }
3187 else {
3188 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3189 goto full;
3190 }
3191
3192 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3193 const char *refresh = U2A(uri->refresh);
3194 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3195 goto full;
3196 }
3197
3198 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3199 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3200 goto full;
3201 }
3202
3203 if (!htx_add_endof(htx, HTX_BLK_EOH))
3204 goto full;
3205
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003206 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003207 return 1;
3208
3209 full:
3210 htx_reset(htx);
3211 si_rx_room_blk(si);
3212 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003213}
3214
Christopher Fauletef779222018-10-31 08:47:01 +01003215
3216static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx)
3217{
3218 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3219 struct stream *s = si_strm(si);
3220 struct uri_auth *uri = s->be->uri_auth;
3221 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003222 struct htx_sl *sl;
3223 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003224
3225 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3226 scope_txt[0] = 0;
3227 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003228 const char *scope_ptr = stats_scope_ptr(appctx, si);
3229
Christopher Fauletef779222018-10-31 08:47:01 +01003230 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003231 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003232 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3233 }
3234
3235 /* We don't want to land on the posted stats page because a refresh will
3236 * repost the data. We don't want this to happen on accident so we redirect
3237 * the browse to the stats page with a GET.
3238 */
3239 chunk_printf(&trash, "%s;st=%s%s%s%s",
3240 uri->uri_prefix,
3241 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3242 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3243 stat_status_codes[appctx->ctx.stats.st_code]) ?
3244 stat_status_codes[appctx->ctx.stats.st_code] :
3245 stat_status_codes[STAT_STATUS_UNKN],
3246 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3247 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3248 scope_txt);
3249
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003250 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3251 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3252 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003253 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003254 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003255
3256 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003257 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3258 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3259 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3260 goto full;
3261
3262 if (!htx_add_endof(htx, HTX_BLK_EOH))
3263 goto full;
3264
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003265 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003266 return 1;
3267
3268full:
3269 htx_reset(htx);
3270 si_rx_room_blk(si);
3271 return 0;
3272}
William Lallemand74c24fb2016-11-21 17:18:36 +01003273
3274static int stats_send_http_headers(struct stream_interface *si)
3275{
3276 struct stream *s = si_strm(si);
3277 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003278 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003279
3280 chunk_printf(&trash,
3281 "HTTP/1.1 200 OK\r\n"
3282 "Cache-Control: no-cache\r\n"
3283 "Connection: close\r\n"
3284 "Content-Type: %s\r\n",
3285 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
3286
3287 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
3288 chunk_appendf(&trash, "Refresh: %d\r\n",
3289 uri->refresh);
3290
3291 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
3292
3293 if (appctx->ctx.stats.flags & STAT_CHUNKED)
3294 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
3295 else
3296 chunk_appendf(&trash, "\r\n");
3297
Willy Tarreau06d80a92017-10-19 14:32:15 +02003298 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003299 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003300 return 0;
3301 }
3302
3303 return 1;
3304}
3305
3306static int stats_send_http_redirect(struct stream_interface *si)
3307{
3308 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3309 struct stream *s = si_strm(si);
3310 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003311 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003312
3313 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3314 scope_txt[0] = 0;
3315 if (appctx->ctx.stats.scope_len) {
3316 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02003317 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 +01003318 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3319 }
3320
3321 /* We don't want to land on the posted stats page because a refresh will
3322 * repost the data. We don't want this to happen on accident so we redirect
3323 * the browse to the stats page with a GET.
3324 */
3325 chunk_printf(&trash,
3326 "HTTP/1.1 303 See Other\r\n"
3327 "Cache-Control: no-cache\r\n"
3328 "Content-Type: text/plain\r\n"
3329 "Connection: close\r\n"
3330 "Location: %s;st=%s%s%s%s\r\n"
3331 "Content-length: 0\r\n"
3332 "\r\n",
3333 uri->uri_prefix,
3334 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3335 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3336 stat_status_codes[appctx->ctx.stats.st_code]) ?
3337 stat_status_codes[appctx->ctx.stats.st_code] :
3338 stat_status_codes[STAT_STATUS_UNKN],
3339 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3340 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3341 scope_txt);
3342
Willy Tarreau06d80a92017-10-19 14:32:15 +02003343 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003344 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003345 return 0;
3346 }
3347
3348 return 1;
3349}
3350
Simon Horman05ee2132017-01-04 09:37:25 +01003351
William Lallemand74c24fb2016-11-21 17:18:36 +01003352/* This I/O handler runs as an applet embedded in a stream interface. It is
3353 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3354 * appctx->st0 contains the operation in progress (dump, done). The handler
3355 * automatically unregisters itself once transfer is complete.
3356 */
Christopher Fauletef779222018-10-31 08:47:01 +01003357static void htx_stats_io_handler(struct appctx *appctx)
3358{
3359 struct stream_interface *si = appctx->owner;
3360 struct stream *s = si_strm(si);
3361 struct channel *req = si_oc(si);
3362 struct channel *res = si_ic(si);
3363 struct htx *req_htx, *res_htx;
3364
3365 res_htx = htx_from_buf(&res->buf);
3366
3367 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3368 goto out;
3369
3370 /* Check if the input buffer is avalaible. */
3371 if (!b_size(&res->buf)) {
3372 si_rx_room_blk(si);
3373 goto out;
3374 }
3375
3376 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003377 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3378 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003379
3380 /* all states are processed in sequence */
3381 if (appctx->st0 == STAT_HTTP_HEAD) {
3382 if (stats_send_htx_headers(si, res_htx)) {
3383 if (s->txn->meth == HTTP_METH_HEAD)
3384 appctx->st0 = STAT_HTTP_DONE;
3385 else
3386 appctx->st0 = STAT_HTTP_DUMP;
3387 }
3388 }
3389
3390 if (appctx->st0 == STAT_HTTP_DUMP) {
3391 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3392 appctx->st0 = STAT_HTTP_DONE;
3393 }
3394
3395 if (appctx->st0 == STAT_HTTP_POST) {
3396 if (stats_process_http_post(si))
3397 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003398 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003399 appctx->st0 = STAT_HTTP_DONE;
3400 }
3401
3402 if (appctx->st0 == STAT_HTTP_LAST) {
3403 if (stats_send_htx_redirect(si, res_htx))
3404 appctx->st0 = STAT_HTTP_DONE;
3405 }
3406
3407 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003408 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003409 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3410 si_rx_room_blk(si);
3411 goto out;
3412 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003413 channel_add_input(&s->res, 1);
3414 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003415 }
3416
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003417 if (appctx->st0 == STAT_HTTP_END) {
3418 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003419 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003420 si_shutr(si);
3421 }
3422
3423 /* eat the whole request */
3424 if (co_data(req)) {
3425 req_htx = htx_from_buf(&req->buf);
3426 co_htx_skip(req, req_htx, co_data(req));
3427 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003428 }
3429 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003430
Christopher Fauletef779222018-10-31 08:47:01 +01003431 out:
3432 /* we have left the request in the buffer for the case where we
3433 * process a POST, and this automatically re-enables activity on
3434 * read. It's better to indicate that we want to stop reading when
3435 * we're sending, so that we know there's at most one direction
3436 * deciding to wake the applet up. It saves it from looping when
3437 * emitting large blocks into small TCP windows.
3438 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003439 htx_to_buf(res_htx, &res->buf);
3440 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003441 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003442}
3443
3444
3445/* This I/O handler runs as an applet embedded in a stream interface. It is
3446 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3447 * appctx->st0 contains the operation in progress (dump, done). The handler
3448 * automatically unregisters itself once transfer is complete.
3449 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003450static void http_stats_io_handler(struct appctx *appctx)
3451{
3452 struct stream_interface *si = appctx->owner;
3453 struct stream *s = si_strm(si);
3454 struct channel *req = si_oc(si);
3455 struct channel *res = si_ic(si);
3456
Christopher Fauletef779222018-10-31 08:47:01 +01003457 if (IS_HTX_STRM(s))
3458 return htx_stats_io_handler(appctx);
3459
William Lallemand74c24fb2016-11-21 17:18:36 +01003460 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3461 goto out;
3462
Joseph Herlant29023ec2018-11-15 13:39:46 -08003463 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003464 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003465 /* already subscribed, we'll be called later once the buffer is
3466 * available.
3467 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003468 goto out;
3469 }
3470
William Lallemand74c24fb2016-11-21 17:18:36 +01003471 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003472 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3473 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003474
3475 /* all states are processed in sequence */
3476 if (appctx->st0 == STAT_HTTP_HEAD) {
3477 if (stats_send_http_headers(si)) {
3478 if (s->txn->meth == HTTP_METH_HEAD)
3479 appctx->st0 = STAT_HTTP_DONE;
3480 else
3481 appctx->st0 = STAT_HTTP_DUMP;
3482 }
3483 }
3484
3485 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003486 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003487 unsigned int data_len;
3488 unsigned int last_len;
3489 unsigned int last_fwd = 0;
3490
3491 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3492 /* One difficulty we're facing is that we must prevent
3493 * the input data from being automatically forwarded to
3494 * the output area. For this, we temporarily disable
3495 * forwarding on the channel.
3496 */
3497 last_fwd = si_ic(si)->to_forward;
3498 si_ic(si)->to_forward = 0;
3499 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003500 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003501 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003502 si_ic(si)->to_forward = last_fwd;
3503 goto out;
3504 }
3505 }
3506
Willy Tarreau97f538b2018-06-15 19:41:31 +02003507 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003508 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003509 appctx->st0 = STAT_HTTP_DONE;
3510
Willy Tarreau97f538b2018-06-15 19:41:31 +02003511 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003512
3513 /* Now we must either adjust or remove the chunk size. This is
3514 * not easy because the chunk size might wrap at the end of the
3515 * buffer, so we pretend we have nothing in the buffer, we write
3516 * the size, then restore the buffer's contents. Note that we can
3517 * only do that because no forwarding is scheduled on the stats
3518 * applet.
3519 */
3520 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3521 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003522 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003523
3524 if (last_len != data_len) {
3525 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003526 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003527 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003528
3529 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003530 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003531 }
3532 /* now re-enable forwarding */
3533 channel_forward(si_ic(si), last_fwd);
3534 }
3535 }
3536
3537 if (appctx->st0 == STAT_HTTP_POST) {
3538 if (stats_process_http_post(si))
3539 appctx->st0 = STAT_HTTP_LAST;
3540 else if (si_oc(si)->flags & CF_SHUTR)
3541 appctx->st0 = STAT_HTTP_DONE;
3542 }
3543
3544 if (appctx->st0 == STAT_HTTP_LAST) {
3545 if (stats_send_http_redirect(si))
3546 appctx->st0 = STAT_HTTP_DONE;
3547 }
3548
3549 if (appctx->st0 == STAT_HTTP_DONE) {
3550 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3551 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003552 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003553 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003554 goto out;
3555 }
3556 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003557 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003558 }
3559
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003560 if (appctx->st0 == STAT_HTTP_END) {
3561 if (!(res->flags & CF_SHUTR)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003562 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003563 si_shutr(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003564 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003565
3566 /* eat the whole request */
3567 if (co_data(req))
3568 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003569 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003570
William Lallemand74c24fb2016-11-21 17:18:36 +01003571 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003572 /* we have left the request in the buffer for the case where we
3573 * process a POST, and this automatically re-enables activity on
3574 * read. It's better to indicate that we want to stop reading when
3575 * we're sending, so that we know there's at most one direction
3576 * deciding to wake the applet up. It saves it from looping when
3577 * emitting large blocks into small TCP windows.
3578 */
3579 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003580 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003581}
3582
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003583/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003584static int stats_dump_info_fields(struct buffer *out,
3585 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003586{
3587 int field;
3588
3589 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3590 if (!field_format(info, field))
3591 continue;
3592
3593 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3594 return 0;
3595 if (!stats_emit_raw_data_field(out, &info[field]))
3596 return 0;
3597 if (!chunk_strcat(out, "\n"))
3598 return 0;
3599 }
3600 return 1;
3601}
3602
3603/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003604static int stats_dump_typed_info_fields(struct buffer *out,
3605 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003606{
3607 int field;
3608
3609 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3610 if (!field_format(info, field))
3611 continue;
3612
3613 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3614 return 0;
3615 if (!stats_emit_field_tags(out, &info[field], ':'))
3616 return 0;
3617 if (!stats_emit_typed_data_field(out, &info[field]))
3618 return 0;
3619 if (!chunk_strcat(out, "\n"))
3620 return 0;
3621 }
3622 return 1;
3623}
3624
3625/* Fill <info> with HAProxy global info. <info> is preallocated
3626 * array of length <len>. The length of the aray must be
3627 * INF_TOTAL_FIELDS. If this length is less then this value, the
3628 * function returns 0, otherwise, it returns 1.
3629 */
3630int stats_fill_info(struct field *info, int len)
3631{
3632 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003633 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003634
3635#ifdef USE_OPENSSL
3636 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3637 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3638 int ssl_reuse = 0;
3639
3640 if (ssl_key_rate < ssl_sess_rate) {
3641 /* count the ssl reuse ratio and avoid overflows in both directions */
3642 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3643 }
3644#endif
3645
3646 if (len < INF_TOTAL_FIELDS)
3647 return 0;
3648
3649 chunk_reset(out);
3650 memset(info, 0, sizeof(*info) * len);
3651
3652 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003653 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3654 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003655
Yves Lafon95317282018-02-26 11:10:37 +01003656 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003657 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3658 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3659 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3660
3661 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3662 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3663
3664 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3665 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3666 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3667 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3668 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3669 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3670 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3671 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3672 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3673 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3674 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3675 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3676#ifdef USE_OPENSSL
3677 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3678 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3679 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3680#endif
3681 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3682 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3683 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3684 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3685 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3686 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3687 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3688 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3689 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3690
3691#ifdef USE_OPENSSL
3692 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3693 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3694 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3695 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3696 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3697 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3698 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3699 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3700 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3701 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3702#endif
3703 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3704 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3705 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3706#ifdef USE_ZLIB
3707 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3708 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3709#endif
3710 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003711 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003712 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003713 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3714 if (global.desc)
3715 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003716 info[INF_STOPPING] = mkf_u32(0, stopping);
3717 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003718 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003719 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003720 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003721 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003722 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003723 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003724 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003725 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3726 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 +01003727
3728 return 1;
3729}
3730
3731/* This function dumps information onto the stream interface's read buffer.
3732 * It returns 0 as long as it does not complete, non-zero upon completion.
3733 * No state is used.
3734 */
3735static int stats_dump_info_to_buffer(struct stream_interface *si)
3736{
3737 struct appctx *appctx = __objt_appctx(si->end);
3738
3739 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3740 return 0;
3741
3742 chunk_reset(&trash);
3743
3744 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3745 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003746 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3747 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003748 else
3749 stats_dump_info_fields(&trash, info);
3750
Willy Tarreau06d80a92017-10-19 14:32:15 +02003751 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003752 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003753 return 0;
3754 }
3755
3756 return 1;
3757}
3758
Simon Horman6f6bb382017-01-04 09:37:26 +01003759/* This function dumps the schema onto the stream interface's read buffer.
3760 * It returns 0 as long as it does not complete, non-zero upon completion.
3761 * No state is used.
3762 *
3763 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3764 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3765 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003766static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003767{
3768
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003769 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003770
3771 chunk_strcat(out,
3772 "{"
3773 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3774 "\"oneOf\":["
3775 "{"
3776 "\"title\":\"Info\","
3777 "\"type\":\"array\","
3778 "\"items\":{"
3779 "\"properties\":{"
3780 "\"title\":\"InfoItem\","
3781 "\"type\":\"object\","
3782 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3783 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3784 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3785 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3786 "},"
3787 "\"required\":[\"field\",\"processNum\",\"tags\","
3788 "\"value\"]"
3789 "}"
3790 "},"
3791 "{"
3792 "\"title\":\"Stat\","
3793 "\"type\":\"array\","
3794 "\"items\":{"
3795 "\"title\":\"InfoItem\","
3796 "\"type\":\"object\","
3797 "\"properties\":{"
3798 "\"objType\":{"
3799 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3800 "\"Server\",\"Unknown\"]"
3801 "},"
3802 "\"proxyId\":{"
3803 "\"type\":\"integer\","
3804 "\"minimum\":0"
3805 "},"
3806 "\"id\":{"
3807 "\"type\":\"integer\","
3808 "\"minimum\":0"
3809 "},"
3810 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3811 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3812 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3813 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3814 "},"
3815 "\"required\":[\"objType\",\"proxyId\",\"id\","
3816 "\"field\",\"processNum\",\"tags\","
3817 "\"value\"]"
3818 "}"
3819 "},"
3820 "{"
3821 "\"title\":\"Error\","
3822 "\"type\":\"object\","
3823 "\"properties\":{"
3824 "\"errorStr\":{"
3825 "\"type\":\"string\""
3826 "},"
3827 "\"required\":[\"errorStr\"]"
3828 "}"
3829 "}"
3830 "],"
3831 "\"definitions\":{"
3832 "\"field\":{"
3833 "\"type\":\"object\","
3834 "\"pos\":{"
3835 "\"type\":\"integer\","
3836 "\"minimum\":0"
3837 "},"
3838 "\"name\":{"
3839 "\"type\":\"string\""
3840 "},"
3841 "\"required\":[\"pos\",\"name\"]"
3842 "},"
3843 "\"processNum\":{"
3844 "\"type\":\"integer\","
3845 "\"minimum\":1"
3846 "},"
3847 "\"tags\":{"
3848 "\"type\":\"object\","
3849 "\"origin\":{"
3850 "\"type\":\"string\","
3851 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3852 "\"Config\",\"Product\",\"Unknown\"]"
3853 "},"
3854 "\"nature\":{"
3855 "\"type\":\"string\","
3856 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3857 "\"Rate\",\"Counter\",\"Duration\","
3858 "\"Age\",\"Time\",\"Name\",\"Output\","
3859 "\"Avg\", \"Unknown\"]"
3860 "},"
3861 "\"scope\":{"
3862 "\"type\":\"string\","
3863 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3864 "\"System\",\"Unknown\"]"
3865 "},"
3866 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3867 "},"
3868 "\"typedValue\":{"
3869 "\"type\":\"object\","
3870 "\"oneOf\":["
3871 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3872 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3873 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3874 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3875 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3876 "],"
3877 "\"definitions\":{"
3878 "\"s32Value\":{"
3879 "\"properties\":{"
3880 "\"type\":{"
3881 "\"type\":\"string\","
3882 "\"enum\":[\"s32\"]"
3883 "},"
3884 "\"value\":{"
3885 "\"type\":\"integer\","
3886 "\"minimum\":-2147483648,"
3887 "\"maximum\":2147483647"
3888 "}"
3889 "},"
3890 "\"required\":[\"type\",\"value\"]"
3891 "},"
3892 "\"s64Value\":{"
3893 "\"properties\":{"
3894 "\"type\":{"
3895 "\"type\":\"string\","
3896 "\"enum\":[\"s64\"]"
3897 "},"
3898 "\"value\":{"
3899 "\"type\":\"integer\","
3900 "\"minimum\":-9007199254740991,"
3901 "\"maximum\":9007199254740991"
3902 "}"
3903 "},"
3904 "\"required\":[\"type\",\"value\"]"
3905 "},"
3906 "\"u32Value\":{"
3907 "\"properties\":{"
3908 "\"type\":{"
3909 "\"type\":\"string\","
3910 "\"enum\":[\"u32\"]"
3911 "},"
3912 "\"value\":{"
3913 "\"type\":\"integer\","
3914 "\"minimum\":0,"
3915 "\"maximum\":4294967295"
3916 "}"
3917 "},"
3918 "\"required\":[\"type\",\"value\"]"
3919 "},"
3920 "\"u64Value\":{"
3921 "\"properties\":{"
3922 "\"type\":{"
3923 "\"type\":\"string\","
3924 "\"enum\":[\"u64\"]"
3925 "},"
3926 "\"value\":{"
3927 "\"type\":\"integer\","
3928 "\"minimum\":0,"
3929 "\"maximum\":9007199254740991"
3930 "}"
3931 "},"
3932 "\"required\":[\"type\",\"value\"]"
3933 "},"
3934 "\"strValue\":{"
3935 "\"properties\":{"
3936 "\"type\":{"
3937 "\"type\":\"string\","
3938 "\"enum\":[\"str\"]"
3939 "},"
3940 "\"value\":{\"type\":\"string\"}"
3941 "},"
3942 "\"required\":[\"type\",\"value\"]"
3943 "},"
3944 "\"unknownValue\":{"
3945 "\"properties\":{"
3946 "\"type\":{"
3947 "\"type\":\"integer\","
3948 "\"minimum\":0"
3949 "},"
3950 "\"value\":{"
3951 "\"type\":\"string\","
3952 "\"enum\":[\"unknown\"]"
3953 "}"
3954 "},"
3955 "\"required\":[\"type\",\"value\"]"
3956 "}"
3957 "}"
3958 "}"
3959 "}"
3960 "}");
3961
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003962 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003963 chunk_reset(out);
3964 chunk_appendf(out,
3965 "{\"errorStr\":\"output buffer too short\"}");
3966 }
3967}
3968
3969/* This function dumps the schema onto the stream interface's read buffer.
3970 * It returns 0 as long as it does not complete, non-zero upon completion.
3971 * No state is used.
3972 */
3973static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3974{
3975 chunk_reset(&trash);
3976
3977 stats_dump_json_schema(&trash);
3978
Willy Tarreau06d80a92017-10-19 14:32:15 +02003979 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003980 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003981 return 0;
3982 }
3983
3984 return 1;
3985}
3986
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003987static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003988{
3989 struct proxy *px;
3990 struct server *sv;
3991 struct listener *li;
3992 int clrall = 0;
3993
3994 if (strcmp(args[2], "all") == 0)
3995 clrall = 1;
3996
3997 /* check permissions */
3998 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3999 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4000 return 1;
4001
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004002 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004003 if (clrall) {
4004 memset(&px->be_counters, 0, sizeof(px->be_counters));
4005 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4006 }
4007 else {
4008 px->be_counters.conn_max = 0;
4009 px->be_counters.p.http.rps_max = 0;
4010 px->be_counters.sps_max = 0;
4011 px->be_counters.cps_max = 0;
4012 px->be_counters.nbpend_max = 0;
Christopher Fauletdf73efe2019-11-08 14:53:15 +01004013 px->be_counters.qtime_max = 0;
4014 px->be_counters.ctime_max = 0;
4015 px->be_counters.dtime_max = 0;
4016 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004017
4018 px->fe_counters.conn_max = 0;
4019 px->fe_counters.p.http.rps_max = 0;
4020 px->fe_counters.sps_max = 0;
4021 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004022 }
4023
4024 for (sv = px->srv; sv; sv = sv->next)
4025 if (clrall)
4026 memset(&sv->counters, 0, sizeof(sv->counters));
4027 else {
4028 sv->counters.cur_sess_max = 0;
4029 sv->counters.nbpend_max = 0;
4030 sv->counters.sps_max = 0;
Christopher Fauletdf73efe2019-11-08 14:53:15 +01004031 sv->counters.qtime_max = 0;
4032 sv->counters.ctime_max = 0;
4033 sv->counters.dtime_max = 0;
4034 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004035 }
4036
4037 list_for_each_entry(li, &px->conf.listeners, by_fe)
4038 if (li->counters) {
4039 if (clrall)
4040 memset(li->counters, 0, sizeof(*li->counters));
4041 else
4042 li->counters->conn_max = 0;
4043 }
4044 }
4045
4046 global.cps_max = 0;
4047 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004048 global.ssl_max = 0;
4049 global.ssl_fe_keys_max = 0;
4050 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004051
4052 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004053 return 1;
4054}
4055
4056
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004057static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004058{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004059 appctx->ctx.stats.scope_str = 0;
4060 appctx->ctx.stats.scope_len = 0;
4061 appctx->ctx.stats.flags = 0;
4062
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004063 if (strcmp(args[2], "typed") == 0)
4064 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004065 else if (strcmp(args[2], "json") == 0)
4066 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004067 return 0;
4068}
4069
4070
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004071static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004072{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004073 appctx->ctx.stats.scope_str = 0;
4074 appctx->ctx.stats.scope_len = 0;
4075 appctx->ctx.stats.flags = 0;
4076
Willy Tarreau2b812e22016-11-22 16:18:05 +01004077 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004078 struct proxy *px;
4079
4080 px = proxy_find_by_name(args[2], 0, 0);
4081 if (px)
4082 appctx->ctx.stats.iid = px->uuid;
4083 else
4084 appctx->ctx.stats.iid = atoi(args[2]);
4085
4086 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004087 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004088 appctx->ctx.cli.msg = "No such proxy.\n";
4089 appctx->st0 = CLI_ST_PRINT;
4090 return 1;
4091 }
4092
Willy Tarreau2b812e22016-11-22 16:18:05 +01004093 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004094 appctx->ctx.stats.type = atoi(args[3]);
4095 appctx->ctx.stats.sid = atoi(args[4]);
4096 if (strcmp(args[5], "typed") == 0)
4097 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004098 else if (strcmp(args[5], "json") == 0)
4099 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004100 }
4101 else if (strcmp(args[2], "typed") == 0)
4102 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004103 else if (strcmp(args[2], "json") == 0)
4104 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004105
Willy Tarreau2b812e22016-11-22 16:18:05 +01004106 return 0;
4107}
4108
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004109static int cli_io_handler_dump_info(struct appctx *appctx)
4110{
4111 return stats_dump_info_to_buffer(appctx->owner);
4112}
4113
Willy Tarreau2b812e22016-11-22 16:18:05 +01004114/* This I/O handler runs as an applet embedded in a stream interface. It is
4115 * used to send raw stats over a socket.
4116 */
4117static int cli_io_handler_dump_stat(struct appctx *appctx)
4118{
Christopher Fauletef779222018-10-31 08:47:01 +01004119 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004120}
4121
Simon Horman6f6bb382017-01-04 09:37:26 +01004122static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4123{
4124 return stats_dump_json_schema_to_buffer(appctx->owner);
4125}
4126
Willy Tarreau2b812e22016-11-22 16:18:05 +01004127/* register cli keywords */
4128static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004129 { { "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 +02004130 { { "show", "info", NULL }, "show info : report information about the running process [json|typed]", cli_parse_show_info, cli_io_handler_dump_info, NULL },
4131 { { "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 +01004132 { { "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 +01004133 {{},}
4134}};
4135
Willy Tarreau0108d902018-11-25 19:14:37 +01004136INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4137
William Lallemand74c24fb2016-11-21 17:18:36 +01004138struct applet http_stats_applet = {
4139 .obj_type = OBJ_TYPE_APPLET,
4140 .name = "<STATS>", /* used for logging */
4141 .fct = http_stats_io_handler,
4142 .release = NULL,
4143};
4144
William Lallemand74c24fb2016-11-21 17:18:36 +01004145/*
4146 * Local variables:
4147 * c-indent-level: 8
4148 * c-basic-offset: 8
4149 * End:
4150 */