blob: e59ad10bb86bb5d216fa00fa7acf8511c5eb7696 [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>
William Lallemand74c24fb2016-11-21 17:18:36 +010065#include <proto/proxy.h>
66#include <proto/sample.h>
67#include <proto/session.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020068#include <proto/ssl_sock.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010069#include <proto/stream.h>
70#include <proto/server.h>
71#include <proto/raw_sock.h>
72#include <proto/stream_interface.h>
73#include <proto/task.h>
74
William Lallemand74c24fb2016-11-21 17:18:36 +010075
Christopher Faulet2b9b6782019-02-27 16:42:58 +010076/* status codes available for the stats admin page (strictly 4 chars length) */
77const char *stat_status_codes[STAT_STATUS_SIZE] = {
78 [STAT_STATUS_DENY] = "DENY",
79 [STAT_STATUS_DONE] = "DONE",
80 [STAT_STATUS_ERRP] = "ERRP",
81 [STAT_STATUS_EXCD] = "EXCD",
82 [STAT_STATUS_NONE] = "NONE",
83 [STAT_STATUS_PART] = "PART",
84 [STAT_STATUS_UNKN] = "UNKN",
85 [STAT_STATUS_IVAL] = "IVAL",
86};
87
Willy Tarreau0baac8c2016-11-22 16:36:53 +010088/* These are the field names for each INF_* field position. Please pay attention
89 * to always use the exact same name except that the strings for new names must
90 * be lower case or CamelCase while the enum entries must be upper case.
91 */
92const char *info_field_names[INF_TOTAL_FIELDS] = {
93 [INF_NAME] = "Name",
94 [INF_VERSION] = "Version",
95 [INF_RELEASE_DATE] = "Release_date",
Yves Lafon95317282018-02-26 11:10:37 +010096 [INF_NBTHREAD] = "Nbthread",
Willy Tarreau0baac8c2016-11-22 16:36:53 +010097 [INF_NBPROC] = "Nbproc",
98 [INF_PROCESS_NUM] = "Process_num",
99 [INF_PID] = "Pid",
100 [INF_UPTIME] = "Uptime",
101 [INF_UPTIME_SEC] = "Uptime_sec",
102 [INF_MEMMAX_MB] = "Memmax_MB",
103 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
104 [INF_POOL_USED_MB] = "PoolUsed_MB",
105 [INF_POOL_FAILED] = "PoolFailed",
106 [INF_ULIMIT_N] = "Ulimit-n",
107 [INF_MAXSOCK] = "Maxsock",
108 [INF_MAXCONN] = "Maxconn",
109 [INF_HARD_MAXCONN] = "Hard_maxconn",
110 [INF_CURR_CONN] = "CurrConns",
111 [INF_CUM_CONN] = "CumConns",
112 [INF_CUM_REQ] = "CumReq",
113 [INF_MAX_SSL_CONNS] = "MaxSslConns",
114 [INF_CURR_SSL_CONNS] = "CurrSslConns",
115 [INF_CUM_SSL_CONNS] = "CumSslConns",
116 [INF_MAXPIPES] = "Maxpipes",
117 [INF_PIPES_USED] = "PipesUsed",
118 [INF_PIPES_FREE] = "PipesFree",
119 [INF_CONN_RATE] = "ConnRate",
120 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
121 [INF_MAX_CONN_RATE] = "MaxConnRate",
122 [INF_SESS_RATE] = "SessRate",
123 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
124 [INF_MAX_SESS_RATE] = "MaxSessRate",
125 [INF_SSL_RATE] = "SslRate",
126 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
127 [INF_MAX_SSL_RATE] = "MaxSslRate",
128 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
129 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
130 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
131 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
132 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
133 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
134 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
135 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
136 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
137 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
138 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
139 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
140 [INF_TASKS] = "Tasks",
141 [INF_RUN_QUEUE] = "Run_queue",
142 [INF_IDLE_PCT] = "Idle_pct",
143 [INF_NODE] = "node",
144 [INF_DESCRIPTION] = "description",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100145 [INF_STOPPING] = "Stopping",
146 [INF_JOBS] = "Jobs",
William Lallemanda7199262018-11-16 16:57:20 +0100147 [INF_UNSTOPPABLE_JOBS] = "Unstoppable Jobs",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100148 [INF_LISTENERS] = "Listeners",
Willy Tarreau199ad242018-11-05 16:31:22 +0100149 [INF_ACTIVE_PEERS] = "ActivePeers",
Willy Tarreau2d372c22018-11-05 17:12:27 +0100150 [INF_CONNECTED_PEERS] = "ConnectedPeers",
Willy Tarreau13ef7732018-11-12 07:25:28 +0100151 [INF_DROPPED_LOGS] = "DroppedLogs",
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100152 [INF_BUSY_POLLING] = "BusyPolling",
Baptiste Assmann333939c2019-01-21 08:34:50 +0100153 [INF_FAILED_RESOLUTIONS] = "FailedResolutions",
Willy Tarreau7cf0e452019-05-23 11:39:14 +0200154 [INF_TOTAL_BYTES_OUT] = "TotalBytesOut",
155 [INF_BYTES_OUT_RATE] = "BytesOutRate",
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100156};
157
William Lallemand74c24fb2016-11-21 17:18:36 +0100158const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
159 [ST_F_PXNAME] = "pxname",
160 [ST_F_SVNAME] = "svname",
161 [ST_F_QCUR] = "qcur",
162 [ST_F_QMAX] = "qmax",
163 [ST_F_SCUR] = "scur",
164 [ST_F_SMAX] = "smax",
165 [ST_F_SLIM] = "slim",
166 [ST_F_STOT] = "stot",
167 [ST_F_BIN] = "bin",
168 [ST_F_BOUT] = "bout",
169 [ST_F_DREQ] = "dreq",
170 [ST_F_DRESP] = "dresp",
171 [ST_F_EREQ] = "ereq",
172 [ST_F_ECON] = "econ",
173 [ST_F_ERESP] = "eresp",
174 [ST_F_WRETR] = "wretr",
175 [ST_F_WREDIS] = "wredis",
176 [ST_F_STATUS] = "status",
177 [ST_F_WEIGHT] = "weight",
178 [ST_F_ACT] = "act",
179 [ST_F_BCK] = "bck",
180 [ST_F_CHKFAIL] = "chkfail",
181 [ST_F_CHKDOWN] = "chkdown",
182 [ST_F_LASTCHG] = "lastchg",
183 [ST_F_DOWNTIME] = "downtime",
184 [ST_F_QLIMIT] = "qlimit",
185 [ST_F_PID] = "pid",
186 [ST_F_IID] = "iid",
187 [ST_F_SID] = "sid",
188 [ST_F_THROTTLE] = "throttle",
189 [ST_F_LBTOT] = "lbtot",
190 [ST_F_TRACKED] = "tracked",
191 [ST_F_TYPE] = "type",
192 [ST_F_RATE] = "rate",
193 [ST_F_RATE_LIM] = "rate_lim",
194 [ST_F_RATE_MAX] = "rate_max",
195 [ST_F_CHECK_STATUS] = "check_status",
196 [ST_F_CHECK_CODE] = "check_code",
197 [ST_F_CHECK_DURATION] = "check_duration",
198 [ST_F_HRSP_1XX] = "hrsp_1xx",
199 [ST_F_HRSP_2XX] = "hrsp_2xx",
200 [ST_F_HRSP_3XX] = "hrsp_3xx",
201 [ST_F_HRSP_4XX] = "hrsp_4xx",
202 [ST_F_HRSP_5XX] = "hrsp_5xx",
203 [ST_F_HRSP_OTHER] = "hrsp_other",
204 [ST_F_HANAFAIL] = "hanafail",
205 [ST_F_REQ_RATE] = "req_rate",
206 [ST_F_REQ_RATE_MAX] = "req_rate_max",
207 [ST_F_REQ_TOT] = "req_tot",
208 [ST_F_CLI_ABRT] = "cli_abrt",
209 [ST_F_SRV_ABRT] = "srv_abrt",
210 [ST_F_COMP_IN] = "comp_in",
211 [ST_F_COMP_OUT] = "comp_out",
212 [ST_F_COMP_BYP] = "comp_byp",
213 [ST_F_COMP_RSP] = "comp_rsp",
214 [ST_F_LASTSESS] = "lastsess",
215 [ST_F_LAST_CHK] = "last_chk",
216 [ST_F_LAST_AGT] = "last_agt",
217 [ST_F_QTIME] = "qtime",
218 [ST_F_CTIME] = "ctime",
219 [ST_F_RTIME] = "rtime",
220 [ST_F_TTIME] = "ttime",
221 [ST_F_AGENT_STATUS] = "agent_status",
222 [ST_F_AGENT_CODE] = "agent_code",
223 [ST_F_AGENT_DURATION] = "agent_duration",
224 [ST_F_CHECK_DESC] = "check_desc",
225 [ST_F_AGENT_DESC] = "agent_desc",
226 [ST_F_CHECK_RISE] = "check_rise",
227 [ST_F_CHECK_FALL] = "check_fall",
228 [ST_F_CHECK_HEALTH] = "check_health",
229 [ST_F_AGENT_RISE] = "agent_rise",
230 [ST_F_AGENT_FALL] = "agent_fall",
231 [ST_F_AGENT_HEALTH] = "agent_health",
232 [ST_F_ADDR] = "addr",
233 [ST_F_COOKIE] = "cookie",
234 [ST_F_MODE] = "mode",
235 [ST_F_ALGO] = "algo",
236 [ST_F_CONN_RATE] = "conn_rate",
237 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
238 [ST_F_CONN_TOT] = "conn_tot",
239 [ST_F_INTERCEPTED] = "intercepted",
240 [ST_F_DCON] = "dcon",
241 [ST_F_DSES] = "dses",
Tim Duesterhus3fd19732018-05-27 20:35:08 +0200242 [ST_F_WREW] = "wrew",
Willy Tarreauf1573842018-12-14 11:35:36 +0100243 [ST_F_CONNECT] = "connect",
244 [ST_F_REUSE] = "reuse",
Willy Tarreaua1214a52018-12-14 14:00:25 +0100245 [ST_F_CACHE_LOOKUPS] = "cache_lookups",
246 [ST_F_CACHE_HITS] = "cache_hits",
William Lallemand74c24fb2016-11-21 17:18:36 +0100247};
248
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100249/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100250static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100251/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100252static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100253
Christopher Faulet6338a082019-09-09 15:50:54 +0200254static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100255
Christopher Fauletef779222018-10-31 08:47:01 +0100256static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
257{
258 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100259 if (chk->data >= channel_htx_recv_max(chn, htx))
260 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200261 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100262 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100263 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100264 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100265 }
266 else {
267 if (ci_putchk(chn, chk) == -1)
268 return 0;
269 }
270 return 1;
271}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100272
Christopher Fauleted7a0662019-01-14 11:07:34 +0100273static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
274{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200275 struct channel *req = si_oc(si);
276 struct htx *htx = htxbuf(&req->buf);
277 struct htx_blk *blk;
278 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100279
Christopher Fauletb7f88902019-07-15 21:56:43 +0200280 blk = htx_get_head_blk(htx);
281 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
282 ALREADY_CHECKED(blk);
283 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
284 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100285}
286
William Lallemand74c24fb2016-11-21 17:18:36 +0100287/*
288 * http_stats_io_handler()
289 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
290 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100291 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100292 * -> stats_dump_html_head() // emits the HTML headers
293 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
294 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
295 * -> stats_dump_html_px_hdr()
296 * -> stats_dump_fe_stats()
297 * -> stats_dump_li_stats()
298 * -> stats_dump_sv_stats()
299 * -> stats_dump_be_stats()
300 * -> stats_dump_html_px_end()
301 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100302 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100303 */
304
305
William Lallemand74c24fb2016-11-21 17:18:36 +0100306/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
307 * for clearing it if needed.
308 * NOTE: Some tools happen to rely on the field position instead of its name,
309 * so please only append new fields at the end, never in the middle.
310 */
311static void stats_dump_csv_header()
312{
313 int field;
314
315 chunk_appendf(&trash, "# ");
316 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
317 chunk_appendf(&trash, "%s,", stat_field_names[field]);
318
319 chunk_appendf(&trash, "\n");
320}
321
322
323/* Emits a stats field without any surrounding element and properly encoded to
324 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
325 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200326int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100327{
328 switch (field_format(f, 0)) {
329 case FF_EMPTY: return 1;
330 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
331 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
332 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
333 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
334 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
335 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
336 }
337}
338
339/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
340 * output is supposed to be used on its own line. Returns non-zero on success, 0
341 * if the buffer is full.
342 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200343int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100344{
345 switch (field_format(f, 0)) {
346 case FF_EMPTY: return 1;
347 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
348 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
349 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
350 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
351 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
352 default: return chunk_appendf(out, "%08x:?", f->type);
353 }
354}
355
Simon Horman05ee2132017-01-04 09:37:25 +0100356/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
357 * the recommendation for interoperable integers in section 6 of RFC 7159.
358 */
359#define JSON_INT_MAX ((1ULL << 53) - 1)
360#define JSON_INT_MIN (0 - JSON_INT_MAX)
361
362/* Emits a stats field value and its type in JSON.
363 * Returns non-zero on success, 0 on error.
364 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200365int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100366{
367 int old_len;
368 char buf[20];
369 const char *type, *value = buf, *quote = "";
370
371 switch (field_format(f, 0)) {
372 case FF_EMPTY: return 1;
373 case FF_S32: type = "\"s32\"";
374 snprintf(buf, sizeof(buf), "%d", f->u.s32);
375 break;
376 case FF_U32: type = "\"u32\"";
377 snprintf(buf, sizeof(buf), "%u", f->u.u32);
378 break;
379 case FF_S64: type = "\"s64\"";
380 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
381 return 0;
382 type = "\"s64\"";
383 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
384 break;
385 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
386 return 0;
387 type = "\"u64\"";
388 snprintf(buf, sizeof(buf), "%llu",
389 (unsigned long long) f->u.u64);
390 break;
391 case FF_STR: type = "\"str\"";
392 value = field_str(f, 0);
393 quote = "\"";
394 break;
395 default: snprintf(buf, sizeof(buf), "%u", f->type);
396 type = buf;
397 value = "unknown";
398 quote = "\"";
399 break;
400 }
401
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200402 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100403 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
404 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200405 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100406}
407
William Lallemand74c24fb2016-11-21 17:18:36 +0100408/* Emits an encoding of the field type on 3 characters followed by a delimiter.
409 * Returns non-zero on success, 0 if the buffer is full.
410 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200411int stats_emit_field_tags(struct buffer *out, const struct field *f,
412 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100413{
414 char origin, nature, scope;
415
416 switch (field_origin(f, 0)) {
417 case FO_METRIC: origin = 'M'; break;
418 case FO_STATUS: origin = 'S'; break;
419 case FO_KEY: origin = 'K'; break;
420 case FO_CONFIG: origin = 'C'; break;
421 case FO_PRODUCT: origin = 'P'; break;
422 default: origin = '?'; break;
423 }
424
425 switch (field_nature(f, 0)) {
426 case FN_GAUGE: nature = 'G'; break;
427 case FN_LIMIT: nature = 'L'; break;
428 case FN_MIN: nature = 'm'; break;
429 case FN_MAX: nature = 'M'; break;
430 case FN_RATE: nature = 'R'; break;
431 case FN_COUNTER: nature = 'C'; break;
432 case FN_DURATION: nature = 'D'; break;
433 case FN_AGE: nature = 'A'; break;
434 case FN_TIME: nature = 'T'; break;
435 case FN_NAME: nature = 'N'; break;
436 case FN_OUTPUT: nature = 'O'; break;
437 case FN_AVG: nature = 'a'; break;
438 default: nature = '?'; break;
439 }
440
441 switch (field_scope(f, 0)) {
442 case FS_PROCESS: scope = 'P'; break;
443 case FS_SERVICE: scope = 'S'; break;
444 case FS_SYSTEM: scope = 's'; break;
445 case FS_CLUSTER: scope = 'C'; break;
446 default: scope = '?'; break;
447 }
448
449 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
450}
451
Simon Horman05ee2132017-01-04 09:37:25 +0100452/* Emits an encoding of the field type as JSON.
453 * Returns non-zero on success, 0 if the buffer is full.
454 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200455int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100456{
457 const char *origin, *nature, *scope;
458 int old_len;
459
460 switch (field_origin(f, 0)) {
461 case FO_METRIC: origin = "Metric"; break;
462 case FO_STATUS: origin = "Status"; break;
463 case FO_KEY: origin = "Key"; break;
464 case FO_CONFIG: origin = "Config"; break;
465 case FO_PRODUCT: origin = "Product"; break;
466 default: origin = "Unknown"; break;
467 }
468
469 switch (field_nature(f, 0)) {
470 case FN_GAUGE: nature = "Gauge"; break;
471 case FN_LIMIT: nature = "Limit"; break;
472 case FN_MIN: nature = "Min"; break;
473 case FN_MAX: nature = "Max"; break;
474 case FN_RATE: nature = "Rate"; break;
475 case FN_COUNTER: nature = "Counter"; break;
476 case FN_DURATION: nature = "Duration"; break;
477 case FN_AGE: nature = "Age"; break;
478 case FN_TIME: nature = "Time"; break;
479 case FN_NAME: nature = "Name"; break;
480 case FN_OUTPUT: nature = "Output"; break;
481 case FN_AVG: nature = "Avg"; break;
482 default: nature = "Unknown"; break;
483 }
484
485 switch (field_scope(f, 0)) {
486 case FS_PROCESS: scope = "Process"; break;
487 case FS_SERVICE: scope = "Service"; break;
488 case FS_SYSTEM: scope = "System"; break;
489 case FS_CLUSTER: scope = "Cluster"; break;
490 default: scope = "Unknown"; break;
491 }
492
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200493 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100494 chunk_appendf(out, "\"tags\":{"
495 "\"origin\":\"%s\","
496 "\"nature\":\"%s\","
497 "\"scope\":\"%s\""
498 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200499 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100500}
William Lallemand74c24fb2016-11-21 17:18:36 +0100501
502/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200503static int stats_dump_fields_csv(struct buffer *out,
504 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100505{
506 int field;
507
508 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
509 if (!stats_emit_raw_data_field(out, &stats[field]))
510 return 0;
511 if (!chunk_strcat(out, ","))
512 return 0;
513 }
514 chunk_strcat(out, "\n");
515 return 1;
516}
517
518/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200519static int stats_dump_fields_typed(struct buffer *out,
520 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100521{
522 int field;
523
524 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
525 if (!stats[field].type)
526 continue;
527
528 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
529 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
530 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
531 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
532 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
533 '?',
534 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
535 field, stat_field_names[field], stats[ST_F_PID].u.u32);
536
537 if (!stats_emit_field_tags(out, &stats[field], ':'))
538 return 0;
539 if (!stats_emit_typed_data_field(out, &stats[field]))
540 return 0;
541 if (!chunk_strcat(out, "\n"))
542 return 0;
543 }
544 return 1;
545}
546
Simon Horman05ee2132017-01-04 09:37:25 +0100547/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200548static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100549 const struct field *info)
550{
551 int field;
552 int started = 0;
553
554 if (!chunk_strcat(out, "["))
555 return 0;
556
557 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
558 int old_len;
559
560 if (!field_format(info, field))
561 continue;
562
563 if (started && !chunk_strcat(out, ","))
564 goto err;
565 started = 1;
566
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200567 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100568 chunk_appendf(out,
569 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
570 "\"processNum\":%u,",
571 field, info_field_names[field],
572 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200573 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100574 goto err;
575
576 if (!stats_emit_json_field_tags(out, &info[field]))
577 goto err;
578
579 if (!stats_emit_json_data_field(out, &info[field]))
580 goto err;
581
582 if (!chunk_strcat(out, "}"))
583 goto err;
584 }
585
586 if (!chunk_strcat(out, "]"))
587 goto err;
588 return 1;
589
590err:
591 chunk_reset(out);
592 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
593 return 0;
594}
595
596/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200597static int stats_dump_fields_json(struct buffer *out,
598 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100599 int first_stat)
600{
601 int field;
602 int started = 0;
603
604 if (!first_stat && !chunk_strcat(out, ","))
605 return 0;
606 if (!chunk_strcat(out, "["))
607 return 0;
608
609 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
610 const char *obj_type;
611 int old_len;
612
613 if (!stats[field].type)
614 continue;
615
616 if (started && !chunk_strcat(out, ","))
617 goto err;
618 started = 1;
619
620 switch (stats[ST_F_TYPE].u.u32) {
621 case STATS_TYPE_FE: obj_type = "Frontend"; break;
622 case STATS_TYPE_BE: obj_type = "Backend"; break;
623 case STATS_TYPE_SO: obj_type = "Listener"; break;
624 case STATS_TYPE_SV: obj_type = "Server"; break;
625 default: obj_type = "Unknown"; break;
626 }
627
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200628 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100629 chunk_appendf(out,
630 "{"
631 "\"objType\":\"%s\","
632 "\"proxyId\":%d,"
633 "\"id\":%d,"
634 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
635 "\"processNum\":%u,",
636 obj_type, stats[ST_F_IID].u.u32,
637 stats[ST_F_SID].u.u32, field,
638 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200639 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100640 goto err;
641
642 if (!stats_emit_json_field_tags(out, &stats[field]))
643 goto err;
644
645 if (!stats_emit_json_data_field(out, &stats[field]))
646 goto err;
647
648 if (!chunk_strcat(out, "}"))
649 goto err;
650 }
651
652 if (!chunk_strcat(out, "]"))
653 goto err;
654
655 return 1;
656
657err:
658 chunk_reset(out);
659 if (!first_stat)
660 chunk_strcat(out, ",");
661 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
662 return 0;
663}
664
William Lallemand74c24fb2016-11-21 17:18:36 +0100665/* Dump all fields from <stats> into <out> using the HTML format. A column is
666 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
667 * are provided if ST_SHLGNDS is present in <flags>.
668 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200669static int stats_dump_fields_html(struct buffer *out,
670 const struct field *stats,
671 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100672{
Willy Tarreau83061a82018-07-13 11:56:34 +0200673 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100674
675 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
676 chunk_appendf(out,
677 /* name, queue */
678 "<tr class=\"frontend\">");
679
680 if (flags & ST_SHOWADMIN) {
681 /* Column sub-heading for Enable or Disable server */
682 chunk_appendf(out, "<td></td>");
683 }
684
685 chunk_appendf(out,
686 "<td class=ac>"
687 "<a name=\"%s/Frontend\"></a>"
688 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
689 "<td colspan=3></td>"
690 "",
691 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
692
693 chunk_appendf(out,
694 /* sessions rate : current */
695 "<td><u>%s<div class=tips><table class=det>"
696 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
697 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
698 "",
699 U2H(stats[ST_F_RATE].u.u32),
700 U2H(stats[ST_F_CONN_RATE].u.u32),
701 U2H(stats[ST_F_RATE].u.u32));
702
703 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
704 chunk_appendf(out,
705 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
706 U2H(stats[ST_F_REQ_RATE].u.u32));
707
708 chunk_appendf(out,
709 "</table></div></u></td>"
710 /* sessions rate : max */
711 "<td><u>%s<div class=tips><table class=det>"
712 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
713 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
714 "",
715 U2H(stats[ST_F_RATE_MAX].u.u32),
716 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
717 U2H(stats[ST_F_RATE_MAX].u.u32));
718
719 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
720 chunk_appendf(out,
721 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
722 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
723
724 chunk_appendf(out,
725 "</table></div></u></td>"
726 /* sessions rate : limit */
727 "<td>%s</td>",
728 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
729
730 chunk_appendf(out,
731 /* sessions: current, max, limit, total */
732 "<td>%s</td><td>%s</td><td>%s</td>"
733 "<td><u>%s<div class=tips><table class=det>"
734 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
735 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
736 "",
737 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
738 U2H(stats[ST_F_STOT].u.u64),
739 U2H(stats[ST_F_CONN_TOT].u.u64),
740 U2H(stats[ST_F_STOT].u.u64));
741
742 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
743 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
744 chunk_appendf(out,
745 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
746 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
747 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
748 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
749 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
750 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
751 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
752 "<tr><th>- other responses:</th><td>%s</td></tr>"
753 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100754 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
755 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200756 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100757 "",
758 U2H(stats[ST_F_REQ_TOT].u.u64),
759 U2H(stats[ST_F_HRSP_1XX].u.u64),
760 U2H(stats[ST_F_HRSP_2XX].u.u64),
761 U2H(stats[ST_F_COMP_RSP].u.u64),
762 stats[ST_F_HRSP_2XX].u.u64 ?
763 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
764 U2H(stats[ST_F_HRSP_3XX].u.u64),
765 U2H(stats[ST_F_HRSP_4XX].u.u64),
766 U2H(stats[ST_F_HRSP_5XX].u.u64),
767 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200768 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100769 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
770 U2H(stats[ST_F_CACHE_HITS].u.u64),
771 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
772 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200773 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100774 }
775
776 chunk_appendf(out,
777 "</table></div></u></td>"
778 /* sessions: lbtot, lastsess */
779 "<td></td><td></td>"
780 /* bytes : in */
781 "<td>%s</td>"
782 "",
783 U2H(stats[ST_F_BIN].u.u64));
784
785 chunk_appendf(out,
786 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
787 "<td>%s%s<div class=tips><table class=det>"
788 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
789 "<tr><th>Compression in:</th><td>%s</td></tr>"
790 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
791 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
792 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
793 "</table></div>%s</td>",
794 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
795 U2H(stats[ST_F_BOUT].u.u64),
796 U2H(stats[ST_F_BOUT].u.u64),
797 U2H(stats[ST_F_COMP_IN].u.u64),
798 U2H(stats[ST_F_COMP_OUT].u.u64),
799 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
800 U2H(stats[ST_F_COMP_BYP].u.u64),
801 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
802 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,
803 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
804
805 chunk_appendf(out,
806 /* denied: req, resp */
807 "<td>%s</td><td>%s</td>"
808 /* errors : request, connect, response */
809 "<td>%s</td><td></td><td></td>"
810 /* warnings: retries, redispatches */
811 "<td></td><td></td>"
812 /* server status : reflect frontend status */
813 "<td class=ac>%s</td>"
814 /* rest of server: nothing */
815 "<td class=ac colspan=8></td></tr>"
816 "",
817 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
818 U2H(stats[ST_F_EREQ].u.u64),
819 field_str(stats, ST_F_STATUS));
820 }
821 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
822 chunk_appendf(out, "<tr class=socket>");
823 if (flags & ST_SHOWADMIN) {
824 /* Column sub-heading for Enable or Disable server */
825 chunk_appendf(out, "<td></td>");
826 }
827
828 chunk_appendf(out,
829 /* frontend name, listener name */
830 "<td class=ac><a name=\"%s/+%s\"></a>%s"
831 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
832 "",
833 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
834 (flags & ST_SHLGNDS)?"<u>":"",
835 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
836
837 if (flags & ST_SHLGNDS) {
838 chunk_appendf(out, "<div class=tips>");
839
840 if (isdigit(*field_str(stats, ST_F_ADDR)))
841 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
842 else if (*field_str(stats, ST_F_ADDR) == '[')
843 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
844 else if (*field_str(stats, ST_F_ADDR))
845 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
846
847 /* id */
848 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
849 }
850
851 chunk_appendf(out,
852 /* queue */
853 "%s</td><td colspan=3></td>"
854 /* sessions rate: current, max, limit */
855 "<td colspan=3>&nbsp;</td>"
856 /* sessions: current, max, limit, total, lbtot, lastsess */
857 "<td>%s</td><td>%s</td><td>%s</td>"
858 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
859 /* bytes: in, out */
860 "<td>%s</td><td>%s</td>"
861 "",
862 (flags & ST_SHLGNDS)?"</u>":"",
863 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
864 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
865
866 chunk_appendf(out,
867 /* denied: req, resp */
868 "<td>%s</td><td>%s</td>"
869 /* errors: request, connect, response */
870 "<td>%s</td><td></td><td></td>"
871 /* warnings: retries, redispatches */
872 "<td></td><td></td>"
873 /* server status: reflect listener status */
874 "<td class=ac>%s</td>"
875 /* rest of server: nothing */
876 "<td class=ac colspan=8></td></tr>"
877 "",
878 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
879 U2H(stats[ST_F_EREQ].u.u64),
880 field_str(stats, ST_F_STATUS));
881 }
882 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
883 const char *style;
884
885 /* determine the style to use depending on the server's state,
886 * its health and weight. There isn't a 1-to-1 mapping between
887 * state and styles for the cases where the server is (still)
888 * up. The reason is that we don't want to report nolb and
889 * drain with the same color.
890 */
891
892 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
893 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
894 style = "down";
895 }
896 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
897 style = "going_up";
898 }
899 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
900 style = "going_down";
901 }
902 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
903 style = "nolb";
904 }
905 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
906 style = "no_check";
907 }
908 else if (!stats[ST_F_CHKFAIL].type ||
909 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
910 /* no check or max health = UP */
911 if (stats[ST_F_WEIGHT].u.u32)
912 style = "up";
913 else
914 style = "draining";
915 }
916 else {
917 style = "going_down";
918 }
919
920 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
921 chunk_appendf(out, "<tr class=\"maintain\">");
922 else
923 chunk_appendf(out,
924 "<tr class=\"%s_%s\">",
925 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
926
927
928 if (flags & ST_SHOWADMIN)
929 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000930 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
931 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100932 field_str(stats, ST_F_SVNAME));
933
934 chunk_appendf(out,
935 "<td class=ac><a name=\"%s/%s\"></a>%s"
936 "<a class=lfsb href=\"#%s/%s\">%s</a>"
937 "",
938 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
939 (flags & ST_SHLGNDS) ? "<u>" : "",
940 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
941
942 if (flags & ST_SHLGNDS) {
943 chunk_appendf(out, "<div class=tips>");
944
945 if (isdigit(*field_str(stats, ST_F_ADDR)))
946 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
947 else if (*field_str(stats, ST_F_ADDR) == '[')
948 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
949 else if (*field_str(stats, ST_F_ADDR))
950 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
951
952 /* id */
953 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
954
955 /* cookie */
956 if (stats[ST_F_COOKIE].type) {
957 chunk_appendf(out, ", cookie: '");
958 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
959 chunk_htmlencode(out, &src);
960 chunk_appendf(out, "'");
961 }
962
963 chunk_appendf(out, "</div>");
964 }
965
966 chunk_appendf(out,
967 /* queue : current, max, limit */
968 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
969 /* sessions rate : current, max, limit */
970 "<td>%s</td><td>%s</td><td></td>"
971 "",
972 (flags & ST_SHLGNDS) ? "</u>" : "",
973 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
974 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
975
976 chunk_appendf(out,
977 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200978 "<td><u>%s<div class=tips>"
979 "<table class=det>"
980 "<tr><th>Current active connections:</th><td>%s</td></tr>"
981 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
982 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
983 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
984 "</table></div></u>"
985 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100986 "<td><u>%s<div class=tips><table class=det>"
987 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
988 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200989 U2H(stats[ST_F_SCUR].u.u32),
990 U2H(stats[ST_F_SCUR].u.u32),
991 U2H(stats[ST_F_SRV_ICUR].u.u32),
992 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
993 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
994 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +0100995 U2H(stats[ST_F_STOT].u.u64),
996 U2H(stats[ST_F_STOT].u.u64));
997
998 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
999 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1000 unsigned long long tot;
1001
1002 tot = stats[ST_F_HRSP_OTHER].u.u64;
1003 tot += stats[ST_F_HRSP_1XX].u.u64;
1004 tot += stats[ST_F_HRSP_2XX].u.u64;
1005 tot += stats[ST_F_HRSP_3XX].u.u64;
1006 tot += stats[ST_F_HRSP_4XX].u.u64;
1007 tot += stats[ST_F_HRSP_5XX].u.u64;
1008
1009 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001010 "<tr><th>New connections:</th><td>%s</td></tr>"
1011 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001012 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1013 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1014 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1015 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1016 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1017 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1018 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001019 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001020 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001021 U2H(stats[ST_F_CONNECT].u.u64),
1022 U2H(stats[ST_F_REUSE].u.u64),
1023 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1024 (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 +01001025 U2H(tot),
1026 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1027 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1028 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1029 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1030 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 +02001031 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1032 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001033 }
1034
1035 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
1036 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1037 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1038 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1039 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1040 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1041
1042 chunk_appendf(out,
1043 "</table></div></u></td>"
1044 /* sessions: lbtot, last */
1045 "<td>%s</td><td>%s</td>",
1046 U2H(stats[ST_F_LBTOT].u.u64),
1047 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1048
1049 chunk_appendf(out,
1050 /* bytes : in, out */
1051 "<td>%s</td><td>%s</td>"
1052 /* denied: req, resp */
1053 "<td></td><td>%s</td>"
1054 /* errors : request, connect */
1055 "<td></td><td>%s</td>"
1056 /* errors : response */
1057 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1058 /* warnings: retries, redispatches */
1059 "<td>%lld</td><td>%lld</td>"
1060 "",
1061 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1062 U2H(stats[ST_F_DRESP].u.u64),
1063 U2H(stats[ST_F_ECON].u.u64),
1064 U2H(stats[ST_F_ERESP].u.u64),
1065 (long long)stats[ST_F_CLI_ABRT].u.u64,
1066 (long long)stats[ST_F_SRV_ABRT].u.u64,
1067 (long long)stats[ST_F_WRETR].u.u64,
1068 (long long)stats[ST_F_WREDIS].u.u64);
1069
1070 /* status, last change */
1071 chunk_appendf(out, "<td class=ac>");
1072
1073 /* FIXME!!!!
1074 * LASTCHG should contain the last change for *this* server and must be computed
1075 * properly above, as was done below, ie: this server if maint, otherwise ref server
1076 * if tracking. Note that ref is either local or remote depending on tracking.
1077 */
1078
1079
1080 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1081 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1082 }
1083 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1084 chunk_strcat(out, "<i>no check</i>");
1085 }
1086 else {
1087 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1088 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1089 if (stats[ST_F_CHECK_HEALTH].u.u32)
1090 chunk_strcat(out, " &uarr;");
1091 }
1092 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1093 chunk_strcat(out, " &darr;");
1094 }
1095
1096 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1097 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1098 chunk_appendf(out,
1099 "</td><td class=ac><u> %s",
1100 field_str(stats, ST_F_AGENT_STATUS));
1101
1102 if (stats[ST_F_AGENT_CODE].type)
1103 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1104
1105 if (stats[ST_F_AGENT_DURATION].type)
1106 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1107
1108 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1109
1110 if (*field_str(stats, ST_F_LAST_AGT)) {
1111 chunk_appendf(out, ": ");
1112 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1113 chunk_htmlencode(out, &src);
1114 }
1115 chunk_appendf(out, "</div></u>");
1116 }
1117 else if (stats[ST_F_CHECK_STATUS].type) {
1118 chunk_appendf(out,
1119 "</td><td class=ac><u> %s",
1120 field_str(stats, ST_F_CHECK_STATUS));
1121
1122 if (stats[ST_F_CHECK_CODE].type)
1123 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1124
1125 if (stats[ST_F_CHECK_DURATION].type)
1126 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1127
1128 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1129
1130 if (*field_str(stats, ST_F_LAST_CHK)) {
1131 chunk_appendf(out, ": ");
1132 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1133 chunk_htmlencode(out, &src);
1134 }
1135 chunk_appendf(out, "</div></u>");
1136 }
1137 else
1138 chunk_appendf(out, "</td><td>");
1139
1140 chunk_appendf(out,
1141 /* weight */
1142 "</td><td class=ac>%d</td>"
1143 /* act, bck */
1144 "<td class=ac>%s</td><td class=ac>%s</td>"
1145 "",
1146 stats[ST_F_WEIGHT].u.u32,
1147 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1148 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1149
1150 /* check failures: unique, fatal, down time */
1151 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1152 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1153 }
1154 else if (stats[ST_F_CHKFAIL].type) {
1155 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1156
1157 if (stats[ST_F_HANAFAIL].type)
1158 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1159
1160 chunk_appendf(out,
1161 "<div class=tips>Failed Health Checks%s</div></u></td>"
1162 "<td>%lld</td><td>%s</td>"
1163 "",
1164 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1165 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1166 }
1167 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1168 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1169 chunk_appendf(out,
1170 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1171 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1172 }
1173 else
1174 chunk_appendf(out, "<td colspan=3></td>");
1175
1176 /* throttle */
1177 if (stats[ST_F_THROTTLE].type)
1178 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1179 else
1180 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1181 }
1182 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1183 chunk_appendf(out, "<tr class=\"backend\">");
1184 if (flags & ST_SHOWADMIN) {
1185 /* Column sub-heading for Enable or Disable server */
1186 chunk_appendf(out, "<td></td>");
1187 }
1188 chunk_appendf(out,
1189 "<td class=ac>"
1190 /* name */
1191 "%s<a name=\"%s/Backend\"></a>"
1192 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1193 "",
1194 (flags & ST_SHLGNDS)?"<u>":"",
1195 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1196
1197 if (flags & ST_SHLGNDS) {
1198 /* balancing */
1199 chunk_appendf(out, "<div class=tips>balancing: %s",
1200 field_str(stats, ST_F_ALGO));
1201
1202 /* cookie */
1203 if (stats[ST_F_COOKIE].type) {
1204 chunk_appendf(out, ", cookie: '");
1205 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1206 chunk_htmlencode(out, &src);
1207 chunk_appendf(out, "'");
1208 }
1209 chunk_appendf(out, "</div>");
1210 }
1211
1212 chunk_appendf(out,
1213 "%s</td>"
1214 /* queue : current, max */
1215 "<td>%s</td><td>%s</td><td></td>"
1216 /* sessions rate : current, max, limit */
1217 "<td>%s</td><td>%s</td><td></td>"
1218 "",
1219 (flags & ST_SHLGNDS)?"</u>":"",
1220 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1221 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1222
1223 chunk_appendf(out,
1224 /* sessions: current, max, limit, total */
1225 "<td>%s</td><td>%s</td><td>%s</td>"
1226 "<td><u>%s<div class=tips><table class=det>"
1227 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1228 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001229 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 +01001230 U2H(stats[ST_F_STOT].u.u64),
1231 U2H(stats[ST_F_STOT].u.u64));
1232
1233 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1234 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1235 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001236 "<tr><th>New connections:</th><td>%s</td></tr>"
1237 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001238 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1239 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1240 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1241 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1242 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1243 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1244 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1245 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001246 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1247 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001248 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001249 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1250 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001251 U2H(stats[ST_F_CONNECT].u.u64),
1252 U2H(stats[ST_F_REUSE].u.u64),
1253 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1254 (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 +01001255 U2H(stats[ST_F_REQ_TOT].u.u64),
1256 U2H(stats[ST_F_HRSP_1XX].u.u64),
1257 U2H(stats[ST_F_HRSP_2XX].u.u64),
1258 U2H(stats[ST_F_COMP_RSP].u.u64),
1259 stats[ST_F_HRSP_2XX].u.u64 ?
1260 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1261 U2H(stats[ST_F_HRSP_3XX].u.u64),
1262 U2H(stats[ST_F_HRSP_4XX].u.u64),
1263 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001264 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001265 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1266 U2H(stats[ST_F_CACHE_HITS].u.u64),
1267 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1268 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001269 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001270 }
1271
1272 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1273 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1274 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1275 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1276 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1277
1278 chunk_appendf(out,
1279 "</table></div></u></td>"
1280 /* sessions: lbtot, last */
1281 "<td>%s</td><td>%s</td>"
1282 /* bytes: in */
1283 "<td>%s</td>"
1284 "",
1285 U2H(stats[ST_F_LBTOT].u.u64),
1286 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1287 U2H(stats[ST_F_BIN].u.u64));
1288
1289 chunk_appendf(out,
1290 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1291 "<td>%s%s<div class=tips><table class=det>"
1292 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1293 "<tr><th>Compression in:</th><td>%s</td></tr>"
1294 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1295 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1296 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1297 "</table></div>%s</td>",
1298 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1299 U2H(stats[ST_F_BOUT].u.u64),
1300 U2H(stats[ST_F_BOUT].u.u64),
1301 U2H(stats[ST_F_COMP_IN].u.u64),
1302 U2H(stats[ST_F_COMP_OUT].u.u64),
1303 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1304 U2H(stats[ST_F_COMP_BYP].u.u64),
1305 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1306 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,
1307 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1308
1309 chunk_appendf(out,
1310 /* denied: req, resp */
1311 "<td>%s</td><td>%s</td>"
1312 /* errors : request, connect */
1313 "<td></td><td>%s</td>"
1314 /* errors : response */
1315 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1316 /* warnings: retries, redispatches */
1317 "<td>%lld</td><td>%lld</td>"
1318 /* backend status: reflect backend status (up/down): we display UP
1319 * if the backend has known working servers or if it has no server at
1320 * all (eg: for stats). Then we display the total weight, number of
1321 * active and backups. */
1322 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1323 "<td class=ac>%d</td><td class=ac>%d</td>"
1324 "",
1325 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1326 U2H(stats[ST_F_ECON].u.u64),
1327 U2H(stats[ST_F_ERESP].u.u64),
1328 (long long)stats[ST_F_CLI_ABRT].u.u64,
1329 (long long)stats[ST_F_SRV_ABRT].u.u64,
1330 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1331 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1332 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1333 stats[ST_F_WEIGHT].u.u32,
1334 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1335
1336 chunk_appendf(out,
1337 /* rest of backend: nothing, down transitions, total downtime, throttle */
1338 "<td class=ac>&nbsp;</td><td>%d</td>"
1339 "<td>%s</td>"
1340 "<td></td>"
1341 "</tr>",
1342 stats[ST_F_CHKDOWN].u.u32,
1343 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1344 }
1345 return 1;
1346}
1347
1348int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1349{
Simon Horman05ee2132017-01-04 09:37:25 +01001350 int ret;
1351
William Lallemand74c24fb2016-11-21 17:18:36 +01001352 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1353 flags |= ST_SHOWADMIN;
1354
1355 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001356 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001357 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001358 ret = stats_dump_fields_typed(&trash, stats);
1359 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1360 ret = stats_dump_fields_json(&trash, stats,
1361 !(appctx->ctx.stats.flags &
1362 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001363 else
Simon Horman05ee2132017-01-04 09:37:25 +01001364 ret = stats_dump_fields_csv(&trash, stats);
1365
1366 if (ret)
1367 appctx->ctx.stats.flags |= STAT_STARTED;
1368
1369 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001370}
1371
1372/* Fill <stats> with the frontend statistics. <stats> is
1373 * preallocated array of length <len>. The length of the array
1374 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1375 * this value, the function returns 0, otherwise, it returns 1.
1376 */
1377int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1378{
1379 if (len < ST_F_TOTAL_FIELDS)
1380 return 0;
1381
1382 memset(stats, 0, sizeof(*stats) * len);
1383
1384 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1385 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1386 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1387 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1388 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1389 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1390 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1391 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1392 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1393 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1394 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1395 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1396 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1397 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1398 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1399 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1400 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1401 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1402 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1403 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1404 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1405 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001406 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001407
1408 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1409 if (px->mode == PR_MODE_HTTP) {
1410 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1411 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1412 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1413 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1414 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1415 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1416 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001417 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1418 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001419 }
1420
1421 /* requests : req_rate, req_rate_max, req_tot, */
1422 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1423 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1424 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1425
1426 /* compression: in, out, bypassed, responses */
1427 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1428 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1429 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1430 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1431
1432 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1433 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1434 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1435 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1436
1437 return 1;
1438}
1439
1440/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1441 * the state from stream interface <si>. The caller is responsible for clearing
1442 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1443 */
1444static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1445{
1446 struct appctx *appctx = __objt_appctx(si->end);
1447
1448 if (!(px->cap & PR_CAP_FE))
1449 return 0;
1450
1451 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1452 return 0;
1453
1454 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1455 return 0;
1456
1457 return stats_dump_one_line(stats, 0, px, appctx);
1458}
1459
1460/* Fill <stats> with the listener statistics. <stats> is
1461 * preallocated array of length <len>. The length of the array
1462 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1463 * then this value, the function returns 0, otherwise, it
1464 * returns 1. <flags> can take the value ST_SHLGNDS.
1465 */
1466int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1467 struct field *stats, int len)
1468{
Willy Tarreau83061a82018-07-13 11:56:34 +02001469 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001470
1471 if (len < ST_F_TOTAL_FIELDS)
1472 return 0;
1473
1474 if (!l->counters)
1475 return 0;
1476
1477 chunk_reset(out);
1478 memset(stats, 0, sizeof(*stats) * len);
1479
1480 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1481 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1482 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1483 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1484 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1485 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1486 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1487 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1488 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1489 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1490 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1491 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1492 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1493 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001494 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 +01001495 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1496 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1497 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1498 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001499 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001500
1501 if (flags & ST_SHLGNDS) {
1502 char str[INET6_ADDRSTRLEN];
1503 int port;
1504
1505 port = get_host_port(&l->addr);
1506 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1507 case AF_INET:
1508 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1509 chunk_appendf(out, "%s:%d", str, port);
1510 break;
1511 case AF_INET6:
1512 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1513 chunk_appendf(out, "[%s]:%d", str, port);
1514 break;
1515 case AF_UNIX:
1516 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1517 break;
1518 case -1:
1519 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1520 chunk_strcat(out, strerror(errno));
1521 break;
1522 default: /* address family not supported */
1523 break;
1524 }
1525 }
1526
1527 return 1;
1528}
1529
1530/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1531 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1532 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1533 * otherwise.
1534 */
1535static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1536{
1537 struct appctx *appctx = __objt_appctx(si->end);
1538
1539 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1540 return 0;
1541
1542 return stats_dump_one_line(stats, flags, px, appctx);
1543}
1544
1545enum srv_stats_state {
1546 SRV_STATS_STATE_DOWN = 0,
1547 SRV_STATS_STATE_DOWN_AGENT,
1548 SRV_STATS_STATE_GOING_UP,
1549 SRV_STATS_STATE_UP_GOING_DOWN,
1550 SRV_STATS_STATE_UP,
1551 SRV_STATS_STATE_NOLB_GOING_DOWN,
1552 SRV_STATS_STATE_NOLB,
1553 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1554 SRV_STATS_STATE_DRAIN,
1555 SRV_STATS_STATE_DRAIN_AGENT,
1556 SRV_STATS_STATE_NO_CHECK,
1557
1558 SRV_STATS_STATE_COUNT, /* Must be last */
1559};
1560
1561static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1562 [SRV_STATS_STATE_DOWN] = "DOWN",
1563 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1564 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1565 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1566 [SRV_STATS_STATE_UP] = "UP",
1567 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1568 [SRV_STATS_STATE_NOLB] = "NOLB",
1569 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1570 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1571 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1572 [SRV_STATS_STATE_NO_CHECK] = "no check"
1573};
1574
1575/* Fill <stats> with the server statistics. <stats> is
1576 * preallocated array of length <len>. The length of the array
1577 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1578 * then this value, the function returns 0, otherwise, it
1579 * returns 1. <flags> can take the value ST_SHLGNDS.
1580 */
1581int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1582 struct field *stats, int len)
1583{
1584 struct server *via, *ref;
1585 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001586 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001587 enum srv_stats_state state;
1588 char *fld_status;
1589
1590 if (len < ST_F_TOTAL_FIELDS)
1591 return 0;
1592
1593 memset(stats, 0, sizeof(*stats) * len);
1594
1595 /* we have "via" which is the tracked server as described in the configuration,
1596 * and "ref" which is the checked server and the end of the chain.
1597 */
1598 via = sv->track ? sv->track : sv;
1599 ref = via;
1600 while (ref->track)
1601 ref = ref->track;
1602
Emeric Brun52a91d32017-08-31 14:41:55 +02001603 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001604 if ((ref->check.state & CHK_ST_ENABLED) &&
1605 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1606 state = SRV_STATS_STATE_UP_GOING_DOWN;
1607 } else {
1608 state = SRV_STATS_STATE_UP;
1609 }
1610
Emeric Brun52a91d32017-08-31 14:41:55 +02001611 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001612 if (ref->agent.state & CHK_ST_ENABLED)
1613 state = SRV_STATS_STATE_DRAIN_AGENT;
1614 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1615 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1616 else
1617 state = SRV_STATS_STATE_DRAIN;
1618 }
1619
1620 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1621 state = SRV_STATS_STATE_NO_CHECK;
1622 }
1623 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001624 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001625 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1626 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1627 state = SRV_STATS_STATE_NOLB;
1628 } else {
1629 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1630 }
1631 }
1632 else { /* stopped */
1633 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1634 state = SRV_STATS_STATE_DOWN_AGENT;
1635 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1636 state = SRV_STATS_STATE_DOWN; /* DOWN */
1637 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1638 state = SRV_STATS_STATE_GOING_UP;
1639 } else {
1640 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1641 }
1642 }
1643
1644 chunk_reset(out);
1645
1646 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1647 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1648 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1649 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1650 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1651 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1652 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1653
1654 if (sv->maxconn)
1655 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1656
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001657 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1658 if (sv->max_idle_conns != -1)
1659 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1660
William Lallemand74c24fb2016-11-21 17:18:36 +01001661 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1662 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1663 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1664 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1665 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1666 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1667 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1668 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001669 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001670 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1671 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001672
1673 /* status */
1674 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001675 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001676 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001677 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001678 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001679 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001680 chunk_appendf(out, "MAINT");
1681 else
1682 chunk_appendf(out,
1683 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001684 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1685 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001686
1687 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1688 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001689 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 +01001690 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1691 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1692
1693 /* check failures: unique, fatal; last change, total downtime */
1694 if (sv->check.state & CHK_ST_ENABLED) {
1695 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1696 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1697 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1698 }
1699
1700 if (sv->maxqueue)
1701 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1702
1703 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1704 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1705 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1706
Emeric Brun52a91d32017-08-31 14:41:55 +02001707 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001708 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1709
1710 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1711
1712 if (sv->track) {
1713 char *fld_track = chunk_newstr(out);
1714
1715 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1716 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1717 }
1718
1719 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1720 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1721 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1722
1723 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1724 const char *fld_chksts;
1725
1726 fld_chksts = chunk_newstr(out);
1727 chunk_strcat(out, "* "); // for check in progress
1728 chunk_strcat(out, get_check_status_info(sv->check.status));
1729 if (!(sv->check.state & CHK_ST_INPROGRESS))
1730 fld_chksts += 2; // skip "* "
1731 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1732
1733 if (sv->check.status >= HCHK_STATUS_L57DATA)
1734 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1735
1736 if (sv->check.status >= HCHK_STATUS_CHECKED)
1737 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1738
1739 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1740 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1741 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1742 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1743 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1744 }
1745
1746 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1747 const char *fld_chksts;
1748
1749 fld_chksts = chunk_newstr(out);
1750 chunk_strcat(out, "* "); // for check in progress
1751 chunk_strcat(out, get_check_status_info(sv->agent.status));
1752 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1753 fld_chksts += 2; // skip "* "
1754 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1755
1756 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1757 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1758
1759 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1760 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1761
1762 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1763 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1764 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1765 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1766 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1767 }
1768
1769 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1770 if (px->mode == PR_MODE_HTTP) {
1771 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1772 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1773 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1774 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1775 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1776 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1777 }
1778
1779 if (ref->observe)
1780 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1781
1782 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1783 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1784 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1785
1786 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1787 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1788 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1789 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1790
1791 if (flags & ST_SHLGNDS) {
1792 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1793 case AF_INET:
1794 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001795 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001796 break;
1797 case AF_INET6:
1798 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001799 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001800 break;
1801 case AF_UNIX:
1802 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1803 break;
1804 case -1:
1805 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1806 chunk_strcat(out, strerror(errno));
1807 break;
1808 default: /* address family not supported */
1809 break;
1810 }
1811
1812 if (sv->cookie)
1813 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1814 }
1815
1816 return 1;
1817}
1818
1819/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1820 * from stream interface <si>, stats flags <flags>, and server state <state>.
1821 * The caller is responsible for clearing the trash if needed. Returns non-zero
1822 * if it emits anything, zero otherwise.
1823 */
1824static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1825{
1826 struct appctx *appctx = __objt_appctx(si->end);
1827
1828 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1829 return 0;
1830
1831 return stats_dump_one_line(stats, flags, px, appctx);
1832}
1833
1834/* Fill <stats> with the backend statistics. <stats> is
1835 * preallocated array of length <len>. The length of the array
1836 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1837 * then this value, the function returns 0, otherwise, it
1838 * returns 1. <flags> can take the value ST_SHLGNDS.
1839 */
1840int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1841{
1842 if (len < ST_F_TOTAL_FIELDS)
1843 return 0;
1844
1845 memset(stats, 0, sizeof(*stats) * len);
1846
1847 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1848 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1849 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1850 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1851 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001852 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001853 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1854 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1855 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1856 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1857 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1858 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1859 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1860 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1861 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1862 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1863 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001864 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001865 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1866 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001867 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1868 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1869 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1870 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1871 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1872 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1873 if (px->srv)
1874 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1875
1876 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1877 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1878 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1879 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1880 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1881 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1882 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1883
1884 if (flags & ST_SHLGNDS) {
1885 if (px->cookie_name)
1886 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1887 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1888 }
1889
1890 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1891 if (px->mode == PR_MODE_HTTP) {
1892 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1893 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1894 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1895 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1896 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1897 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1898 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001899 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1900 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001901 }
1902
1903 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1904 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1905
1906 /* compression: in, out, bypassed, responses */
1907 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1908 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1909 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1910 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1911 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1912
1913 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1914 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1915 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1916 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1917
1918 return 1;
1919}
1920
1921/* Dumps a line for backend <px> to the trash for and uses the state from stream
1922 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1923 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1924 */
1925static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1926{
1927 struct appctx *appctx = __objt_appctx(si->end);
1928
1929 if (!(px->cap & PR_CAP_BE))
1930 return 0;
1931
1932 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1933 return 0;
1934
1935 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1936 return 0;
1937
1938 return stats_dump_one_line(stats, flags, px, appctx);
1939}
1940
1941/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1942 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1943 * for clearing the trash if needed.
1944 */
1945static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1946{
1947 struct appctx *appctx = __objt_appctx(si->end);
1948 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1949
1950 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1951 /* A form to enable/disable this proxy servers */
1952
1953 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1954 scope_txt[0] = 0;
1955 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001956 const char *scope_ptr = stats_scope_ptr(appctx, si);
1957
William Lallemand74c24fb2016-11-21 17:18:36 +01001958 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001959 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001960 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1961 }
1962
1963 chunk_appendf(&trash,
1964 "<form method=\"post\">");
1965 }
1966
1967 /* print a new table */
1968 chunk_appendf(&trash,
1969 "<table class=\"tbl\" width=\"100%%\">\n"
1970 "<tr class=\"titre\">"
1971 "<th class=\"pxname\" width=\"10%%\">");
1972
1973 chunk_appendf(&trash,
1974 "<a name=\"%s\"></a>%s"
1975 "<a class=px href=\"#%s\">%s</a>",
1976 px->id,
1977 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1978 px->id, px->id);
1979
1980 if (uri->flags & ST_SHLGNDS) {
1981 /* cap, mode, id */
1982 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1983 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1984 px->uuid);
1985 chunk_appendf(&trash, "</div>");
1986 }
1987
1988 chunk_appendf(&trash,
1989 "%s</th>"
1990 "<th class=\"%s\" width=\"90%%\">%s</th>"
1991 "</tr>\n"
1992 "</table>\n"
1993 "<table class=\"tbl\" width=\"100%%\">\n"
1994 "<tr class=\"titre\">",
1995 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1996 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1997
1998 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1999 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00002000 chunk_appendf(&trash,
2001 "<th rowspan=2 width=1><input type=\"checkbox\" \
2002 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
2003 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2004 px->id,
2005 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01002006 }
2007
2008 chunk_appendf(&trash,
2009 "<th rowspan=2></th>"
2010 "<th colspan=3>Queue</th>"
2011 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2012 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2013 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2014 "<th colspan=9>Server</th>"
2015 "</tr>\n"
2016 "<tr class=\"titre\">"
2017 "<th>Cur</th><th>Max</th><th>Limit</th>"
2018 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2019 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2020 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2021 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2022 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2023 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2024 "<th>Thrtle</th>\n"
2025 "</tr>");
2026}
2027
2028/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2029 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2030 */
2031static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2032{
2033 struct appctx *appctx = __objt_appctx(si->end);
2034 chunk_appendf(&trash, "</table>");
2035
2036 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2037 /* close the form used to enable/disable this proxy servers */
2038 chunk_appendf(&trash,
2039 "Choose the action to perform on the checked servers : "
2040 "<select name=action>"
2041 "<option value=\"\"></option>"
2042 "<option value=\"ready\">Set state to READY</option>"
2043 "<option value=\"drain\">Set state to DRAIN</option>"
2044 "<option value=\"maint\">Set state to MAINT</option>"
2045 "<option value=\"dhlth\">Health: disable checks</option>"
2046 "<option value=\"ehlth\">Health: enable checks</option>"
2047 "<option value=\"hrunn\">Health: force UP</option>"
2048 "<option value=\"hnolb\">Health: force NOLB</option>"
2049 "<option value=\"hdown\">Health: force DOWN</option>"
2050 "<option value=\"dagent\">Agent: disable checks</option>"
2051 "<option value=\"eagent\">Agent: enable checks</option>"
2052 "<option value=\"arunn\">Agent: force UP</option>"
2053 "<option value=\"adown\">Agent: force DOWN</option>"
2054 "<option value=\"shutdown\">Kill Sessions</option>"
2055 "</select>"
2056 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2057 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2058 "</form>",
2059 px->uuid);
2060 }
2061
2062 chunk_appendf(&trash, "<p>\n");
2063}
2064
2065/*
2066 * Dumps statistics for a proxy. The output is sent to the stream interface's
2067 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2068 * buffer space, or non-zero if everything completed. This function is used
2069 * both by the CLI and the HTTP entry points, and is able to dump the output
2070 * in HTML or CSV formats. If the later, <uri> must be NULL.
2071 */
Christopher Fauletef779222018-10-31 08:47:01 +01002072int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2073 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002074{
2075 struct appctx *appctx = __objt_appctx(si->end);
2076 struct stream *s = si_strm(si);
2077 struct channel *rep = si_ic(si);
2078 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2079 struct listener *l;
2080 unsigned int flags;
2081
2082 if (uri)
2083 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002084 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002085 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2086 else
2087 flags = ST_SHNODE | ST_SHDESC;
2088
2089 chunk_reset(&trash);
2090
2091 switch (appctx->ctx.stats.px_st) {
2092 case STAT_PX_ST_INIT:
2093 /* we are on a new proxy */
2094 if (uri && uri->scope) {
2095 /* we have a limited scope, we have to check the proxy name */
2096 struct stat_scope *scope;
2097 int len;
2098
2099 len = strlen(px->id);
2100 scope = uri->scope;
2101
2102 while (scope) {
2103 /* match exact proxy name */
2104 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2105 break;
2106
2107 /* match '.' which means 'self' proxy */
2108 if (!strcmp(scope->px_id, ".") && px == s->be)
2109 break;
2110 scope = scope->next;
2111 }
2112
2113 /* proxy name not found : don't dump anything */
2114 if (scope == NULL)
2115 return 1;
2116 }
2117
2118 /* if the user has requested a limited output and the proxy
2119 * name does not match, skip it.
2120 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002121 if (appctx->ctx.stats.scope_len) {
2122 const char *scope_ptr = stats_scope_ptr(appctx, si);
2123
2124 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2125 return 1;
2126 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002127
2128 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2129 (appctx->ctx.stats.iid != -1) &&
2130 (px->uuid != appctx->ctx.stats.iid))
2131 return 1;
2132
2133 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2134 /* fall through */
2135
2136 case STAT_PX_ST_TH:
2137 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2138 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002139 if (!stats_putchk(rep, htx, &trash))
2140 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002141 }
2142
2143 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2144 /* fall through */
2145
2146 case STAT_PX_ST_FE:
2147 /* print the frontend */
2148 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002149 if (!stats_putchk(rep, htx, &trash))
2150 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002151 }
2152
2153 appctx->ctx.stats.l = px->conf.listeners.n;
2154 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2155 /* fall through */
2156
2157 case STAT_PX_ST_LI:
2158 /* stats.l has been initialized above */
2159 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002160 if (htx) {
2161 if (htx_almost_full(htx))
2162 goto full;
2163 }
2164 else {
2165 if (buffer_almost_full(&rep->buf))
2166 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002167 }
2168
2169 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2170 if (!l->counters)
2171 continue;
2172
2173 if (appctx->ctx.stats.flags & STAT_BOUND) {
2174 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2175 break;
2176
2177 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2178 continue;
2179 }
2180
2181 /* print the frontend */
2182 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002183 if (!stats_putchk(rep, htx, &trash))
2184 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002185 }
2186 }
2187
2188 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2189 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2190 /* fall through */
2191
2192 case STAT_PX_ST_SV:
2193 /* stats.sv has been initialized above */
2194 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002195 if (htx) {
2196 if (htx_almost_full(htx))
2197 goto full;
2198 }
2199 else {
2200 if (buffer_almost_full(&rep->buf))
2201 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002202 }
2203
2204 sv = appctx->ctx.stats.sv;
2205
2206 if (appctx->ctx.stats.flags & STAT_BOUND) {
2207 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2208 break;
2209
2210 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2211 continue;
2212 }
2213
2214 svs = sv;
2215 while (svs->track)
2216 svs = svs->track;
2217
2218 /* do not report servers which are DOWN and not changing state */
2219 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002220 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2221 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002222 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2223 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2224 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2225 continue;
2226 }
2227
2228 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002229 if (!stats_putchk(rep, htx, &trash))
2230 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002231 }
2232 } /* for sv */
2233
2234 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2235 /* fall through */
2236
2237 case STAT_PX_ST_BE:
2238 /* print the backend */
2239 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002240 if (!stats_putchk(rep, htx, &trash))
2241 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002242 }
2243
2244 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2245 /* fall through */
2246
2247 case STAT_PX_ST_END:
2248 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2249 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002250 if (!stats_putchk(rep, htx, &trash))
2251 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002252 }
2253
2254 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2255 /* fall through */
2256
2257 case STAT_PX_ST_FIN:
2258 return 1;
2259
2260 default:
2261 /* unknown state, we should put an abort() here ! */
2262 return 1;
2263 }
Christopher Fauletef779222018-10-31 08:47:01 +01002264
2265 full:
2266 si_rx_room_blk(si);
2267 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002268}
2269
2270/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2271 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2272 */
2273static void stats_dump_html_head(struct uri_auth *uri)
2274{
2275 /* WARNING! This must fit in the first buffer !!! */
2276 chunk_appendf(&trash,
2277 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2278 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2279 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2280 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2281 "<style type=\"text/css\"><!--\n"
2282 "body {"
2283 " font-family: arial, helvetica, sans-serif;"
2284 " font-size: 12px;"
2285 " font-weight: normal;"
2286 " color: black;"
2287 " background: white;"
2288 "}\n"
2289 "th,td {"
2290 " font-size: 10px;"
2291 "}\n"
2292 "h1 {"
2293 " font-size: x-large;"
2294 " margin-bottom: 0.5em;"
2295 "}\n"
2296 "h2 {"
2297 " font-family: helvetica, arial;"
2298 " font-size: x-large;"
2299 " font-weight: bold;"
2300 " font-style: italic;"
2301 " color: #6020a0;"
2302 " margin-top: 0em;"
2303 " margin-bottom: 0em;"
2304 "}\n"
2305 "h3 {"
2306 " font-family: helvetica, arial;"
2307 " font-size: 16px;"
2308 " font-weight: bold;"
2309 " color: #b00040;"
2310 " background: #e8e8d0;"
2311 " margin-top: 0em;"
2312 " margin-bottom: 0em;"
2313 "}\n"
2314 "li {"
2315 " margin-top: 0.25em;"
2316 " margin-right: 2em;"
2317 "}\n"
2318 ".hr {margin-top: 0.25em;"
2319 " border-color: black;"
2320 " border-bottom-style: solid;"
2321 "}\n"
2322 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2323 ".total {background: #20D0D0;color: #ffff80;}\n"
2324 ".frontend {background: #e8e8d0;}\n"
2325 ".socket {background: #d0d0d0;}\n"
2326 ".backend {background: #e8e8d0;}\n"
2327 ".active_down {background: #ff9090;}\n"
2328 ".active_going_up {background: #ffd020;}\n"
2329 ".active_going_down {background: #ffffa0;}\n"
2330 ".active_up {background: #c0ffc0;}\n"
2331 ".active_nolb {background: #20a0ff;}\n"
2332 ".active_draining {background: #20a0FF;}\n"
2333 ".active_no_check {background: #e0e0e0;}\n"
2334 ".backup_down {background: #ff9090;}\n"
2335 ".backup_going_up {background: #ff80ff;}\n"
2336 ".backup_going_down {background: #c060ff;}\n"
2337 ".backup_up {background: #b0d0ff;}\n"
2338 ".backup_nolb {background: #90b0e0;}\n"
2339 ".backup_draining {background: #cc9900;}\n"
2340 ".backup_no_check {background: #e0e0e0;}\n"
2341 ".maintain {background: #c07820;}\n"
2342 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2343 "\n"
2344 "a.px:link {color: #ffff40; text-decoration: none;}"
2345 "a.px:visited {color: #ffff40; text-decoration: none;}"
2346 "a.px:hover {color: #ffffff; text-decoration: none;}"
2347 "a.lfsb:link {color: #000000; text-decoration: none;}"
2348 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2349 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2350 "\n"
2351 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2352 "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"
2353 "table.tbl td.ac { text-align: center;}\n"
2354 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2355 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2356 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2357 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2358 "\n"
2359 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2360 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2361 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2362 "table.det { border-collapse: collapse; border-style: none; }\n"
2363 "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"
2364 "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"
2365 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2366 "div.tips {\n"
2367 " display:block;\n"
2368 " visibility:hidden;\n"
2369 " z-index:2147483647;\n"
2370 " position:absolute;\n"
2371 " padding:2px 4px 3px;\n"
2372 " background:#f0f060; color:#000000;\n"
2373 " border:1px solid #7040c0;\n"
2374 " white-space:nowrap;\n"
2375 " font-style:normal;font-size:11px;font-weight:normal;\n"
2376 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2377 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2378 "}\n"
2379 "u:hover div.tips {visibility:visible;}\n"
2380 "-->\n"
2381 "</style></head>\n",
2382 (uri->flags & ST_SHNODE) ? " on " : "",
2383 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2384 );
2385}
2386
2387/* Dumps the HTML stats information block to the trash for and uses the state from
2388 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2389 * for clearing the trash if needed.
2390 */
2391static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2392{
2393 struct appctx *appctx = __objt_appctx(si->end);
2394 unsigned int up = (now.tv_sec - start_date.tv_sec);
2395 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002396 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002397 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2398
2399 /* Turn the bytes per second to bits per second and take care of the
2400 * usual ethernet overhead in order to help figure how far we are from
2401 * interface saturation since it's the only case which usually matters.
2402 * For this we count the total size of an Ethernet frame on the wire
2403 * including preamble and IFG (1538) for the largest TCP segment it
2404 * transports (1448 with TCP timestamps). This is not valid for smaller
2405 * packets (under-estimated), but it gives a reasonably accurate
2406 * estimation of how far we are from uplink saturation.
2407 */
2408 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002409
2410 /* WARNING! this has to fit the first packet too.
2411 * We are around 3.5 kB, add adding entries will
2412 * become tricky if we want to support 4kB buffers !
2413 */
2414 chunk_appendf(&trash,
2415 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2416 PRODUCT_NAME "%s</a></h1>\n"
2417 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2418 "<hr width=\"100%%\" class=\"hr\">\n"
2419 "<h3>&gt; General process information</h3>\n"
2420 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002421 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002422 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2423 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2424 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002425 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002426 "Running tasks: %d/%d; idle = %d %%<br>\n"
2427 "</td><td align=\"center\" nowrap>\n"
2428 "<table class=\"lgd\"><tr>\n"
2429 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2430 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2431 "</tr><tr>\n"
2432 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2433 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2434 "</tr><tr>\n"
2435 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2436 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2437 "</tr><tr>\n"
2438 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2439 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2440 "</tr><tr>\n"
2441 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2442 "</tr><tr>\n"
2443 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2444 "</tr></table>\n"
2445 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2446 "</td>"
2447 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2448 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2449 "",
Willy Tarreau909b9d82019-01-04 18:20:32 +01002450 (uri->flags & ST_HIDEVER) ? "" : (stats_version_string),
William Lallemand74c24fb2016-11-21 17:18:36 +01002451 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2452 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2453 (uri->flags & ST_SHDESC) ? ": " : "",
2454 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002455 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002456 up / 86400, (up % 86400) / 3600,
2457 (up % 3600) / 60, (up % 60),
2458 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2459 global.rlimit_memmax ? " MB" : "",
2460 global.rlimit_nofile,
2461 global.maxsock, global.maxconn, global.maxpipes,
2462 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002463 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2464 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002465 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002466 );
2467
2468 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002469 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002470 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2471
2472 chunk_appendf(&trash,
2473 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2474 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2475 STAT_SCOPE_TXT_MAXLEN);
2476
2477 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2478 scope_txt[0] = 0;
2479 if (appctx->ctx.stats.scope_len) {
2480 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002481 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002482 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2483 }
2484
2485 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2486 chunk_appendf(&trash,
2487 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2488 uri->uri_prefix,
2489 "",
2490 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2491 scope_txt);
2492 else
2493 chunk_appendf(&trash,
2494 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2495 uri->uri_prefix,
2496 ";up",
2497 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2498 scope_txt);
2499
2500 if (uri->refresh > 0) {
2501 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2502 chunk_appendf(&trash,
2503 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2504 uri->uri_prefix,
2505 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2506 "",
2507 scope_txt);
2508 else
2509 chunk_appendf(&trash,
2510 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2511 uri->uri_prefix,
2512 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2513 ";norefresh",
2514 scope_txt);
2515 }
2516
2517 chunk_appendf(&trash,
2518 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2519 uri->uri_prefix,
2520 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2521 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2522 scope_txt);
2523
2524 chunk_appendf(&trash,
2525 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2526 uri->uri_prefix,
2527 (uri->refresh > 0) ? ";norefresh" : "",
2528 scope_txt);
2529
2530 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002531 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2532 uri->uri_prefix,
2533 (uri->refresh > 0) ? ";norefresh" : "",
2534 scope_txt, uri->uri_prefix);
2535
2536 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002537 "</ul></td>"
2538 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2539 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2540 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2541 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2542 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2543 "</ul>"
2544 "</td>"
2545 "</tr></table>\n"
2546 ""
2547 );
2548
2549 if (appctx->ctx.stats.st_code) {
2550 switch (appctx->ctx.stats.st_code) {
2551 case STAT_STATUS_DONE:
2552 chunk_appendf(&trash,
2553 "<p><div class=active_up>"
2554 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2555 "Action processed successfully."
2556 "</div>\n", uri->uri_prefix,
2557 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2558 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2559 scope_txt);
2560 break;
2561 case STAT_STATUS_NONE:
2562 chunk_appendf(&trash,
2563 "<p><div class=active_going_down>"
2564 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2565 "Nothing has changed."
2566 "</div>\n", uri->uri_prefix,
2567 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2568 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2569 scope_txt);
2570 break;
2571 case STAT_STATUS_PART:
2572 chunk_appendf(&trash,
2573 "<p><div class=active_going_down>"
2574 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2575 "Action partially processed.<br>"
2576 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2577 "</div>\n", uri->uri_prefix,
2578 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2579 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2580 scope_txt);
2581 break;
2582 case STAT_STATUS_ERRP:
2583 chunk_appendf(&trash,
2584 "<p><div class=active_down>"
2585 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2586 "Action not processed because of invalid parameters."
2587 "<ul>"
2588 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002589 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002590 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2591 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2592 "</ul>"
2593 "</div>\n", uri->uri_prefix,
2594 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2595 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2596 scope_txt);
2597 break;
2598 case STAT_STATUS_EXCD:
2599 chunk_appendf(&trash,
2600 "<p><div class=active_down>"
2601 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2602 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2603 "You should retry with less servers at a time.</b>"
2604 "</div>\n", uri->uri_prefix,
2605 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2606 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2607 scope_txt);
2608 break;
2609 case STAT_STATUS_DENY:
2610 chunk_appendf(&trash,
2611 "<p><div class=active_down>"
2612 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2613 "<b>Action denied.</b>"
2614 "</div>\n", uri->uri_prefix,
2615 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2616 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2617 scope_txt);
2618 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002619 case STAT_STATUS_IVAL:
2620 chunk_appendf(&trash,
2621 "<p><div class=active_down>"
2622 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2623 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
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;
William Lallemand74c24fb2016-11-21 17:18:36 +01002629 default:
2630 chunk_appendf(&trash,
2631 "<p><div class=active_no_check>"
2632 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2633 "Unexpected result."
2634 "</div>\n", uri->uri_prefix,
2635 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2636 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2637 scope_txt);
2638 }
2639 chunk_appendf(&trash, "<p>\n");
2640 }
2641}
2642
2643/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2644 * for clearing the trash if needed.
2645 */
2646static void stats_dump_html_end()
2647{
2648 chunk_appendf(&trash, "</body></html>\n");
2649}
2650
Simon Horman05ee2132017-01-04 09:37:25 +01002651/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2652 * for clearing it if needed.
2653 */
2654static void stats_dump_json_header()
2655{
2656 chunk_strcat(&trash, "[");
2657}
2658
2659
2660/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2661 * for clearing the trash if needed.
2662 */
2663static void stats_dump_json_end()
2664{
2665 chunk_strcat(&trash, "]");
2666}
2667
William Lallemand74c24fb2016-11-21 17:18:36 +01002668/* This function dumps statistics onto the stream interface's read buffer in
2669 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2670 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2671 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2672 * and the stream must be closed, or -1 in case of any error. This function is
2673 * used by both the CLI and the HTTP handlers.
2674 */
Christopher Fauletef779222018-10-31 08:47:01 +01002675static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2676 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002677{
2678 struct appctx *appctx = __objt_appctx(si->end);
2679 struct channel *rep = si_ic(si);
2680 struct proxy *px;
2681
2682 chunk_reset(&trash);
2683
2684 switch (appctx->st2) {
2685 case STAT_ST_INIT:
2686 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2687 /* fall through */
2688
2689 case STAT_ST_HEAD:
2690 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2691 stats_dump_html_head(uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002692 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2693 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002694 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002695 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002696 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2697 stats_dump_csv_header();
2698
Christopher Fauletef779222018-10-31 08:47:01 +01002699 if (!stats_putchk(rep, htx, &trash))
2700 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002701
Christopher Faulet6338a082019-09-09 15:50:54 +02002702 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2703 appctx->st2 = STAT_ST_FIN;
2704 return 1;
2705 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002706 appctx->st2 = STAT_ST_INFO;
2707 /* fall through */
2708
2709 case STAT_ST_INFO:
2710 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2711 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002712 if (!stats_putchk(rep, htx, &trash))
2713 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002714 }
2715
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002716 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002717 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2718 appctx->st2 = STAT_ST_LIST;
2719 /* fall through */
2720
2721 case STAT_ST_LIST:
2722 /* dump proxies */
2723 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002724 if (htx) {
2725 if (htx_almost_full(htx))
2726 goto full;
2727 }
2728 else {
2729 if (buffer_almost_full(&rep->buf))
2730 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002731 }
2732
2733 px = appctx->ctx.stats.px;
2734 /* skip the disabled proxies, global frontend and non-networked ones */
2735 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002736 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002737 return 0;
2738
2739 appctx->ctx.stats.px = px->next;
2740 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2741 }
2742 /* here, we just have reached the last proxy */
2743
2744 appctx->st2 = STAT_ST_END;
2745 /* fall through */
2746
2747 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002748 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2749 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2750 stats_dump_html_end();
2751 else
2752 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002753 if (!stats_putchk(rep, htx, &trash))
2754 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002755 }
2756
2757 appctx->st2 = STAT_ST_FIN;
2758 /* fall through */
2759
2760 case STAT_ST_FIN:
2761 return 1;
2762
2763 default:
2764 /* unknown state ! */
2765 appctx->st2 = STAT_ST_FIN;
2766 return -1;
2767 }
Christopher Fauletef779222018-10-31 08:47:01 +01002768
2769 full:
2770 si_rx_room_blk(si);
2771 return 0;
2772
William Lallemand74c24fb2016-11-21 17:18:36 +01002773}
2774
2775/* We reached the stats page through a POST request. The appctx is
2776 * expected to have already been allocated by the caller.
2777 * Parse the posted data and enable/disable servers if necessary.
2778 * Returns 1 if request was parsed or zero if it needs more data.
2779 */
2780static int stats_process_http_post(struct stream_interface *si)
2781{
2782 struct stream *s = si_strm(si);
2783 struct appctx *appctx = objt_appctx(si->end);
2784
2785 struct proxy *px = NULL;
2786 struct server *sv = NULL;
2787
2788 char key[LINESIZE];
2789 int action = ST_ADM_ACTION_NONE;
2790 int reprocess = 0;
2791
2792 int total_servers = 0;
2793 int altered_servers = 0;
2794
2795 char *first_param, *cur_param, *next_param, *end_params;
2796 char *st_cur_param = NULL;
2797 char *st_next_param = NULL;
2798
Christopher Fauleta3618372018-12-13 21:59:56 +01002799 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002800
Christopher Fauletb7f88902019-07-15 21:56:43 +02002801 struct htx *htx = htxbuf(&s->req.buf);
2802 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002803
Christopher Fauletb7f88902019-07-15 21:56:43 +02002804 /* we need more data */
2805 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2806 /* check if we can receive more */
2807 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2808 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2809 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002810 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002811 goto wait;
2812 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002813
Christopher Fauletb7f88902019-07-15 21:56:43 +02002814 /* The request was fully received. Copy data */
2815 blk = htx_get_head_blk(htx);
2816 while (blk) {
2817 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002818
Christopher Fauletb7f88902019-07-15 21:56:43 +02002819 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2820 break;
2821 if (type == HTX_BLK_DATA) {
2822 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002823
Christopher Fauletb7f88902019-07-15 21:56:43 +02002824 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002825 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2826 goto out;
2827 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002828 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002829 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002830 }
2831
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002832 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002833 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002834 cur_param = next_param = end_params;
2835 *end_params = '\0';
2836
2837 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2838
2839 /*
2840 * Parse the parameters in reverse order to only store the last value.
2841 * From the html form, the backend and the action are at the end.
2842 */
2843 while (cur_param > first_param) {
2844 char *value;
2845 int poffset, plen;
2846
2847 cur_param--;
2848
2849 if ((*cur_param == '&') || (cur_param == first_param)) {
2850 reprocess_servers:
2851 /* Parse the key */
2852 poffset = (cur_param != first_param ? 1 : 0);
2853 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2854 if ((plen > 0) && (plen <= sizeof(key))) {
2855 strncpy(key, cur_param + poffset, plen);
2856 key[plen - 1] = '\0';
2857 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002858 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002859 goto out;
2860 }
2861
2862 /* Parse the value */
2863 value = key;
2864 while (*value != '\0' && *value != '=') {
2865 value++;
2866 }
2867 if (*value == '=') {
2868 /* Ok, a value is found, we can mark the end of the key */
2869 *value++ = '\0';
2870 }
2871 if (url_decode(key) < 0 || url_decode(value) < 0)
2872 break;
2873
2874 /* Now we can check the key to see what to do */
2875 if (!px && (strcmp(key, "b") == 0)) {
2876 if ((px = proxy_be_by_name(value)) == NULL) {
2877 /* the backend name is unknown or ambiguous (duplicate names) */
2878 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2879 goto out;
2880 }
2881 }
2882 else if (!action && (strcmp(key, "action") == 0)) {
2883 if (strcmp(value, "ready") == 0) {
2884 action = ST_ADM_ACTION_READY;
2885 }
2886 else if (strcmp(value, "drain") == 0) {
2887 action = ST_ADM_ACTION_DRAIN;
2888 }
2889 else if (strcmp(value, "maint") == 0) {
2890 action = ST_ADM_ACTION_MAINT;
2891 }
2892 else if (strcmp(value, "shutdown") == 0) {
2893 action = ST_ADM_ACTION_SHUTDOWN;
2894 }
2895 else if (strcmp(value, "dhlth") == 0) {
2896 action = ST_ADM_ACTION_DHLTH;
2897 }
2898 else if (strcmp(value, "ehlth") == 0) {
2899 action = ST_ADM_ACTION_EHLTH;
2900 }
2901 else if (strcmp(value, "hrunn") == 0) {
2902 action = ST_ADM_ACTION_HRUNN;
2903 }
2904 else if (strcmp(value, "hnolb") == 0) {
2905 action = ST_ADM_ACTION_HNOLB;
2906 }
2907 else if (strcmp(value, "hdown") == 0) {
2908 action = ST_ADM_ACTION_HDOWN;
2909 }
2910 else if (strcmp(value, "dagent") == 0) {
2911 action = ST_ADM_ACTION_DAGENT;
2912 }
2913 else if (strcmp(value, "eagent") == 0) {
2914 action = ST_ADM_ACTION_EAGENT;
2915 }
2916 else if (strcmp(value, "arunn") == 0) {
2917 action = ST_ADM_ACTION_ARUNN;
2918 }
2919 else if (strcmp(value, "adown") == 0) {
2920 action = ST_ADM_ACTION_ADOWN;
2921 }
2922 /* else these are the old supported methods */
2923 else if (strcmp(value, "disable") == 0) {
2924 action = ST_ADM_ACTION_DISABLE;
2925 }
2926 else if (strcmp(value, "enable") == 0) {
2927 action = ST_ADM_ACTION_ENABLE;
2928 }
2929 else if (strcmp(value, "stop") == 0) {
2930 action = ST_ADM_ACTION_STOP;
2931 }
2932 else if (strcmp(value, "start") == 0) {
2933 action = ST_ADM_ACTION_START;
2934 }
2935 else {
2936 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2937 goto out;
2938 }
2939 }
2940 else if (strcmp(key, "s") == 0) {
2941 if (!(px && action)) {
2942 /*
2943 * Indicates that we'll need to reprocess the parameters
2944 * as soon as backend and action are known
2945 */
2946 if (!reprocess) {
2947 st_cur_param = cur_param;
2948 st_next_param = next_param;
2949 }
2950 reprocess = 1;
2951 }
2952 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002953 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002954 switch (action) {
2955 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002956 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002957 altered_servers++;
2958 total_servers++;
2959 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2960 }
2961 break;
2962 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002963 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002964 altered_servers++;
2965 total_servers++;
2966 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2967 }
2968 break;
2969 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002970 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002971 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2972 altered_servers++;
2973 total_servers++;
2974 }
2975 break;
2976 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002977 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002978 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2979 altered_servers++;
2980 total_servers++;
2981 }
2982 break;
2983 case ST_ADM_ACTION_DHLTH:
2984 if (sv->check.state & CHK_ST_CONFIGURED) {
2985 sv->check.state &= ~CHK_ST_ENABLED;
2986 altered_servers++;
2987 total_servers++;
2988 }
2989 break;
2990 case ST_ADM_ACTION_EHLTH:
2991 if (sv->check.state & CHK_ST_CONFIGURED) {
2992 sv->check.state |= CHK_ST_ENABLED;
2993 altered_servers++;
2994 total_servers++;
2995 }
2996 break;
2997 case ST_ADM_ACTION_HRUNN:
2998 if (!(sv->track)) {
2999 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003000 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003001 altered_servers++;
3002 total_servers++;
3003 }
3004 break;
3005 case ST_ADM_ACTION_HNOLB:
3006 if (!(sv->track)) {
3007 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003008 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003009 altered_servers++;
3010 total_servers++;
3011 }
3012 break;
3013 case ST_ADM_ACTION_HDOWN:
3014 if (!(sv->track)) {
3015 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003016 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003017 altered_servers++;
3018 total_servers++;
3019 }
3020 break;
3021 case ST_ADM_ACTION_DAGENT:
3022 if (sv->agent.state & CHK_ST_CONFIGURED) {
3023 sv->agent.state &= ~CHK_ST_ENABLED;
3024 altered_servers++;
3025 total_servers++;
3026 }
3027 break;
3028 case ST_ADM_ACTION_EAGENT:
3029 if (sv->agent.state & CHK_ST_CONFIGURED) {
3030 sv->agent.state |= CHK_ST_ENABLED;
3031 altered_servers++;
3032 total_servers++;
3033 }
3034 break;
3035 case ST_ADM_ACTION_ARUNN:
3036 if (sv->agent.state & CHK_ST_ENABLED) {
3037 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003038 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003039 altered_servers++;
3040 total_servers++;
3041 }
3042 break;
3043 case ST_ADM_ACTION_ADOWN:
3044 if (sv->agent.state & CHK_ST_ENABLED) {
3045 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003046 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003047 altered_servers++;
3048 total_servers++;
3049 }
3050 break;
3051 case ST_ADM_ACTION_READY:
3052 srv_adm_set_ready(sv);
3053 altered_servers++;
3054 total_servers++;
3055 break;
3056 case ST_ADM_ACTION_DRAIN:
3057 srv_adm_set_drain(sv);
3058 altered_servers++;
3059 total_servers++;
3060 break;
3061 case ST_ADM_ACTION_MAINT:
3062 srv_adm_set_maint(sv);
3063 altered_servers++;
3064 total_servers++;
3065 break;
3066 case ST_ADM_ACTION_SHUTDOWN:
3067 if (px->state != PR_STSTOPPED) {
3068 struct stream *sess, *sess_bck;
3069
3070 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3071 if (sess->srv_conn == sv)
3072 stream_shutdown(sess, SF_ERR_KILLED);
3073
3074 altered_servers++;
3075 total_servers++;
3076 }
3077 break;
3078 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003079 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003080 } else {
3081 /* the server name is unknown or ambiguous (duplicate names) */
3082 total_servers++;
3083 }
3084 }
3085 if (reprocess && px && action) {
3086 /* Now, we know the backend and the action chosen by the user.
3087 * We can safely restart from the first server parameter
3088 * to reprocess them
3089 */
3090 cur_param = st_cur_param;
3091 next_param = st_next_param;
3092 reprocess = 0;
3093 goto reprocess_servers;
3094 }
3095
3096 next_param = cur_param;
3097 }
3098 }
3099
3100 if (total_servers == 0) {
3101 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3102 }
3103 else if (altered_servers == 0) {
3104 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3105 }
3106 else if (altered_servers == total_servers) {
3107 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3108 }
3109 else {
3110 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3111 }
3112 out:
3113 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003114 wait:
3115 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3116 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003117}
3118
3119
Christopher Fauletb7f88902019-07-15 21:56:43 +02003120static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003121{
3122 struct stream *s = si_strm(si);
3123 struct uri_auth *uri = s->be->uri_auth;
3124 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003125 struct htx_sl *sl;
3126 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003127
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003128 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);
3129 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3130 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003131 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003132 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003133
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003134 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003135 goto full;
3136 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3137 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3138 goto full;
3139 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003140 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3141 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3142 goto full;
3143 }
Christopher Fauletef779222018-10-31 08:47:01 +01003144 else {
3145 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3146 goto full;
3147 }
3148
3149 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3150 const char *refresh = U2A(uri->refresh);
3151 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3152 goto full;
3153 }
3154
3155 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3156 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3157 goto full;
3158 }
3159
3160 if (!htx_add_endof(htx, HTX_BLK_EOH))
3161 goto full;
3162
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003163 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003164 return 1;
3165
3166 full:
3167 htx_reset(htx);
3168 si_rx_room_blk(si);
3169 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003170}
3171
Christopher Fauletef779222018-10-31 08:47:01 +01003172
Christopher Fauletb7f88902019-07-15 21:56:43 +02003173static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003174{
3175 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3176 struct stream *s = si_strm(si);
3177 struct uri_auth *uri = s->be->uri_auth;
3178 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003179 struct htx_sl *sl;
3180 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003181
3182 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3183 scope_txt[0] = 0;
3184 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003185 const char *scope_ptr = stats_scope_ptr(appctx, si);
3186
Christopher Fauletef779222018-10-31 08:47:01 +01003187 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003188 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003189 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3190 }
3191
3192 /* We don't want to land on the posted stats page because a refresh will
3193 * repost the data. We don't want this to happen on accident so we redirect
3194 * the browse to the stats page with a GET.
3195 */
3196 chunk_printf(&trash, "%s;st=%s%s%s%s",
3197 uri->uri_prefix,
3198 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3199 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3200 stat_status_codes[appctx->ctx.stats.st_code]) ?
3201 stat_status_codes[appctx->ctx.stats.st_code] :
3202 stat_status_codes[STAT_STATUS_UNKN],
3203 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3204 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3205 scope_txt);
3206
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003207 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3208 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3209 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003210 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003211 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003212
3213 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003214 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3215 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3216 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3217 goto full;
3218
3219 if (!htx_add_endof(htx, HTX_BLK_EOH))
3220 goto full;
3221
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003222 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003223 return 1;
3224
3225full:
3226 htx_reset(htx);
3227 si_rx_room_blk(si);
3228 return 0;
3229}
William Lallemand74c24fb2016-11-21 17:18:36 +01003230
Simon Horman05ee2132017-01-04 09:37:25 +01003231
William Lallemand74c24fb2016-11-21 17:18:36 +01003232/* This I/O handler runs as an applet embedded in a stream interface. It is
3233 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3234 * appctx->st0 contains the operation in progress (dump, done). The handler
3235 * automatically unregisters itself once transfer is complete.
3236 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003237static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003238{
3239 struct stream_interface *si = appctx->owner;
3240 struct stream *s = si_strm(si);
3241 struct channel *req = si_oc(si);
3242 struct channel *res = si_ic(si);
3243 struct htx *req_htx, *res_htx;
3244
3245 res_htx = htx_from_buf(&res->buf);
3246
3247 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3248 goto out;
3249
3250 /* Check if the input buffer is avalaible. */
3251 if (!b_size(&res->buf)) {
3252 si_rx_room_blk(si);
3253 goto out;
3254 }
3255
3256 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003257 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3258 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003259
3260 /* all states are processed in sequence */
3261 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003262 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003263 if (s->txn->meth == HTTP_METH_HEAD)
3264 appctx->st0 = STAT_HTTP_DONE;
3265 else
3266 appctx->st0 = STAT_HTTP_DUMP;
3267 }
3268 }
3269
3270 if (appctx->st0 == STAT_HTTP_DUMP) {
3271 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3272 appctx->st0 = STAT_HTTP_DONE;
3273 }
3274
3275 if (appctx->st0 == STAT_HTTP_POST) {
3276 if (stats_process_http_post(si))
3277 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003278 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003279 appctx->st0 = STAT_HTTP_DONE;
3280 }
3281
3282 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003283 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003284 appctx->st0 = STAT_HTTP_DONE;
3285 }
3286
3287 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003288 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003289 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3290 si_rx_room_blk(si);
3291 goto out;
3292 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003293 channel_add_input(&s->res, 1);
3294 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003295 }
3296
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003297 if (appctx->st0 == STAT_HTTP_END) {
3298 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003299 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003300 si_shutr(si);
3301 }
3302
3303 /* eat the whole request */
3304 if (co_data(req)) {
3305 req_htx = htx_from_buf(&req->buf);
3306 co_htx_skip(req, req_htx, co_data(req));
3307 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003308 }
3309 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003310
Christopher Fauletef779222018-10-31 08:47:01 +01003311 out:
3312 /* we have left the request in the buffer for the case where we
3313 * process a POST, and this automatically re-enables activity on
3314 * read. It's better to indicate that we want to stop reading when
3315 * we're sending, so that we know there's at most one direction
3316 * deciding to wake the applet up. It saves it from looping when
3317 * emitting large blocks into small TCP windows.
3318 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003319 htx_to_buf(res_htx, &res->buf);
3320 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003321 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003322}
3323
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003324/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003325static int stats_dump_info_fields(struct buffer *out,
3326 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003327{
3328 int field;
3329
3330 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3331 if (!field_format(info, field))
3332 continue;
3333
3334 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3335 return 0;
3336 if (!stats_emit_raw_data_field(out, &info[field]))
3337 return 0;
3338 if (!chunk_strcat(out, "\n"))
3339 return 0;
3340 }
3341 return 1;
3342}
3343
3344/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003345static int stats_dump_typed_info_fields(struct buffer *out,
3346 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003347{
3348 int field;
3349
3350 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3351 if (!field_format(info, field))
3352 continue;
3353
3354 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3355 return 0;
3356 if (!stats_emit_field_tags(out, &info[field], ':'))
3357 return 0;
3358 if (!stats_emit_typed_data_field(out, &info[field]))
3359 return 0;
3360 if (!chunk_strcat(out, "\n"))
3361 return 0;
3362 }
3363 return 1;
3364}
3365
3366/* Fill <info> with HAProxy global info. <info> is preallocated
3367 * array of length <len>. The length of the aray must be
3368 * INF_TOTAL_FIELDS. If this length is less then this value, the
3369 * function returns 0, otherwise, it returns 1.
3370 */
3371int stats_fill_info(struct field *info, int len)
3372{
3373 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003374 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003375
3376#ifdef USE_OPENSSL
3377 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3378 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3379 int ssl_reuse = 0;
3380
3381 if (ssl_key_rate < ssl_sess_rate) {
3382 /* count the ssl reuse ratio and avoid overflows in both directions */
3383 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3384 }
3385#endif
3386
3387 if (len < INF_TOTAL_FIELDS)
3388 return 0;
3389
3390 chunk_reset(out);
3391 memset(info, 0, sizeof(*info) * len);
3392
3393 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003394 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3395 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003396
Yves Lafon95317282018-02-26 11:10:37 +01003397 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003398 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3399 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3400 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3401
3402 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3403 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3404
3405 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3406 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3407 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3408 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3409 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3410 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3411 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3412 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3413 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3414 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3415 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3416 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3417#ifdef USE_OPENSSL
3418 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3419 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3420 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3421#endif
3422 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3423 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3424 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3425 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3426 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3427 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3428 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3429 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3430 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3431
3432#ifdef USE_OPENSSL
3433 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3434 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3435 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3436 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3437 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3438 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3439 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3440 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3441 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3442 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3443#endif
3444 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3445 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3446 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3447#ifdef USE_ZLIB
3448 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3449 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3450#endif
3451 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003452 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003453 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003454 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3455 if (global.desc)
3456 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003457 info[INF_STOPPING] = mkf_u32(0, stopping);
3458 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003459 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003460 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003461 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003462 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003463 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003464 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003465 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003466 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3467 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 +01003468
3469 return 1;
3470}
3471
3472/* This function dumps information onto the stream interface's read buffer.
3473 * It returns 0 as long as it does not complete, non-zero upon completion.
3474 * No state is used.
3475 */
3476static int stats_dump_info_to_buffer(struct stream_interface *si)
3477{
3478 struct appctx *appctx = __objt_appctx(si->end);
3479
3480 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3481 return 0;
3482
3483 chunk_reset(&trash);
3484
3485 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3486 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003487 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3488 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003489 else
3490 stats_dump_info_fields(&trash, info);
3491
Willy Tarreau06d80a92017-10-19 14:32:15 +02003492 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003493 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003494 return 0;
3495 }
3496
3497 return 1;
3498}
3499
Simon Horman6f6bb382017-01-04 09:37:26 +01003500/* This function dumps the schema onto the stream interface's read buffer.
3501 * It returns 0 as long as it does not complete, non-zero upon completion.
3502 * No state is used.
3503 *
3504 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3505 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3506 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003507static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003508{
3509
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003510 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003511
3512 chunk_strcat(out,
3513 "{"
3514 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3515 "\"oneOf\":["
3516 "{"
3517 "\"title\":\"Info\","
3518 "\"type\":\"array\","
3519 "\"items\":{"
3520 "\"properties\":{"
3521 "\"title\":\"InfoItem\","
3522 "\"type\":\"object\","
3523 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3524 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3525 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3526 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3527 "},"
3528 "\"required\":[\"field\",\"processNum\",\"tags\","
3529 "\"value\"]"
3530 "}"
3531 "},"
3532 "{"
3533 "\"title\":\"Stat\","
3534 "\"type\":\"array\","
3535 "\"items\":{"
3536 "\"title\":\"InfoItem\","
3537 "\"type\":\"object\","
3538 "\"properties\":{"
3539 "\"objType\":{"
3540 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3541 "\"Server\",\"Unknown\"]"
3542 "},"
3543 "\"proxyId\":{"
3544 "\"type\":\"integer\","
3545 "\"minimum\":0"
3546 "},"
3547 "\"id\":{"
3548 "\"type\":\"integer\","
3549 "\"minimum\":0"
3550 "},"
3551 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3552 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3553 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3554 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3555 "},"
3556 "\"required\":[\"objType\",\"proxyId\",\"id\","
3557 "\"field\",\"processNum\",\"tags\","
3558 "\"value\"]"
3559 "}"
3560 "},"
3561 "{"
3562 "\"title\":\"Error\","
3563 "\"type\":\"object\","
3564 "\"properties\":{"
3565 "\"errorStr\":{"
3566 "\"type\":\"string\""
3567 "},"
3568 "\"required\":[\"errorStr\"]"
3569 "}"
3570 "}"
3571 "],"
3572 "\"definitions\":{"
3573 "\"field\":{"
3574 "\"type\":\"object\","
3575 "\"pos\":{"
3576 "\"type\":\"integer\","
3577 "\"minimum\":0"
3578 "},"
3579 "\"name\":{"
3580 "\"type\":\"string\""
3581 "},"
3582 "\"required\":[\"pos\",\"name\"]"
3583 "},"
3584 "\"processNum\":{"
3585 "\"type\":\"integer\","
3586 "\"minimum\":1"
3587 "},"
3588 "\"tags\":{"
3589 "\"type\":\"object\","
3590 "\"origin\":{"
3591 "\"type\":\"string\","
3592 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3593 "\"Config\",\"Product\",\"Unknown\"]"
3594 "},"
3595 "\"nature\":{"
3596 "\"type\":\"string\","
3597 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3598 "\"Rate\",\"Counter\",\"Duration\","
3599 "\"Age\",\"Time\",\"Name\",\"Output\","
3600 "\"Avg\", \"Unknown\"]"
3601 "},"
3602 "\"scope\":{"
3603 "\"type\":\"string\","
3604 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3605 "\"System\",\"Unknown\"]"
3606 "},"
3607 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3608 "},"
3609 "\"typedValue\":{"
3610 "\"type\":\"object\","
3611 "\"oneOf\":["
3612 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3613 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3614 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3615 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3616 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3617 "],"
3618 "\"definitions\":{"
3619 "\"s32Value\":{"
3620 "\"properties\":{"
3621 "\"type\":{"
3622 "\"type\":\"string\","
3623 "\"enum\":[\"s32\"]"
3624 "},"
3625 "\"value\":{"
3626 "\"type\":\"integer\","
3627 "\"minimum\":-2147483648,"
3628 "\"maximum\":2147483647"
3629 "}"
3630 "},"
3631 "\"required\":[\"type\",\"value\"]"
3632 "},"
3633 "\"s64Value\":{"
3634 "\"properties\":{"
3635 "\"type\":{"
3636 "\"type\":\"string\","
3637 "\"enum\":[\"s64\"]"
3638 "},"
3639 "\"value\":{"
3640 "\"type\":\"integer\","
3641 "\"minimum\":-9007199254740991,"
3642 "\"maximum\":9007199254740991"
3643 "}"
3644 "},"
3645 "\"required\":[\"type\",\"value\"]"
3646 "},"
3647 "\"u32Value\":{"
3648 "\"properties\":{"
3649 "\"type\":{"
3650 "\"type\":\"string\","
3651 "\"enum\":[\"u32\"]"
3652 "},"
3653 "\"value\":{"
3654 "\"type\":\"integer\","
3655 "\"minimum\":0,"
3656 "\"maximum\":4294967295"
3657 "}"
3658 "},"
3659 "\"required\":[\"type\",\"value\"]"
3660 "},"
3661 "\"u64Value\":{"
3662 "\"properties\":{"
3663 "\"type\":{"
3664 "\"type\":\"string\","
3665 "\"enum\":[\"u64\"]"
3666 "},"
3667 "\"value\":{"
3668 "\"type\":\"integer\","
3669 "\"minimum\":0,"
3670 "\"maximum\":9007199254740991"
3671 "}"
3672 "},"
3673 "\"required\":[\"type\",\"value\"]"
3674 "},"
3675 "\"strValue\":{"
3676 "\"properties\":{"
3677 "\"type\":{"
3678 "\"type\":\"string\","
3679 "\"enum\":[\"str\"]"
3680 "},"
3681 "\"value\":{\"type\":\"string\"}"
3682 "},"
3683 "\"required\":[\"type\",\"value\"]"
3684 "},"
3685 "\"unknownValue\":{"
3686 "\"properties\":{"
3687 "\"type\":{"
3688 "\"type\":\"integer\","
3689 "\"minimum\":0"
3690 "},"
3691 "\"value\":{"
3692 "\"type\":\"string\","
3693 "\"enum\":[\"unknown\"]"
3694 "}"
3695 "},"
3696 "\"required\":[\"type\",\"value\"]"
3697 "}"
3698 "}"
3699 "}"
3700 "}"
3701 "}");
3702
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003703 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003704 chunk_reset(out);
3705 chunk_appendf(out,
3706 "{\"errorStr\":\"output buffer too short\"}");
3707 }
3708}
3709
3710/* This function dumps the schema onto the stream interface's read buffer.
3711 * It returns 0 as long as it does not complete, non-zero upon completion.
3712 * No state is used.
3713 */
3714static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3715{
3716 chunk_reset(&trash);
3717
3718 stats_dump_json_schema(&trash);
3719
Willy Tarreau06d80a92017-10-19 14:32:15 +02003720 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003721 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003722 return 0;
3723 }
3724
3725 return 1;
3726}
3727
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003728static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003729{
3730 struct proxy *px;
3731 struct server *sv;
3732 struct listener *li;
3733 int clrall = 0;
3734
3735 if (strcmp(args[2], "all") == 0)
3736 clrall = 1;
3737
3738 /* check permissions */
3739 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3740 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3741 return 1;
3742
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003743 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003744 if (clrall) {
3745 memset(&px->be_counters, 0, sizeof(px->be_counters));
3746 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3747 }
3748 else {
3749 px->be_counters.conn_max = 0;
3750 px->be_counters.p.http.rps_max = 0;
3751 px->be_counters.sps_max = 0;
3752 px->be_counters.cps_max = 0;
3753 px->be_counters.nbpend_max = 0;
3754
3755 px->fe_counters.conn_max = 0;
3756 px->fe_counters.p.http.rps_max = 0;
3757 px->fe_counters.sps_max = 0;
3758 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003759 }
3760
3761 for (sv = px->srv; sv; sv = sv->next)
3762 if (clrall)
3763 memset(&sv->counters, 0, sizeof(sv->counters));
3764 else {
3765 sv->counters.cur_sess_max = 0;
3766 sv->counters.nbpend_max = 0;
3767 sv->counters.sps_max = 0;
3768 }
3769
3770 list_for_each_entry(li, &px->conf.listeners, by_fe)
3771 if (li->counters) {
3772 if (clrall)
3773 memset(li->counters, 0, sizeof(*li->counters));
3774 else
3775 li->counters->conn_max = 0;
3776 }
3777 }
3778
3779 global.cps_max = 0;
3780 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003781 global.ssl_max = 0;
3782 global.ssl_fe_keys_max = 0;
3783 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003784
3785 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003786 return 1;
3787}
3788
3789
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003790static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003791{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003792 appctx->ctx.stats.scope_str = 0;
3793 appctx->ctx.stats.scope_len = 0;
3794 appctx->ctx.stats.flags = 0;
3795
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003796 if (strcmp(args[2], "typed") == 0)
3797 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003798 else if (strcmp(args[2], "json") == 0)
3799 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003800 return 0;
3801}
3802
3803
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003804static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003805{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003806 appctx->ctx.stats.scope_str = 0;
3807 appctx->ctx.stats.scope_len = 0;
3808 appctx->ctx.stats.flags = 0;
3809
Willy Tarreau2b812e22016-11-22 16:18:05 +01003810 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003811 struct proxy *px;
3812
3813 px = proxy_find_by_name(args[2], 0, 0);
3814 if (px)
3815 appctx->ctx.stats.iid = px->uuid;
3816 else
3817 appctx->ctx.stats.iid = atoi(args[2]);
3818
Willy Tarreau9d008692019-08-09 11:21:01 +02003819 if (!appctx->ctx.stats.iid)
3820 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003821
Willy Tarreau2b812e22016-11-22 16:18:05 +01003822 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003823 appctx->ctx.stats.type = atoi(args[3]);
3824 appctx->ctx.stats.sid = atoi(args[4]);
3825 if (strcmp(args[5], "typed") == 0)
3826 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003827 else if (strcmp(args[5], "json") == 0)
3828 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003829 }
3830 else if (strcmp(args[2], "typed") == 0)
3831 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003832 else if (strcmp(args[2], "json") == 0)
3833 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003834
Willy Tarreau2b812e22016-11-22 16:18:05 +01003835 return 0;
3836}
3837
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003838static int cli_io_handler_dump_info(struct appctx *appctx)
3839{
3840 return stats_dump_info_to_buffer(appctx->owner);
3841}
3842
Willy Tarreau2b812e22016-11-22 16:18:05 +01003843/* This I/O handler runs as an applet embedded in a stream interface. It is
3844 * used to send raw stats over a socket.
3845 */
3846static int cli_io_handler_dump_stat(struct appctx *appctx)
3847{
Christopher Fauletef779222018-10-31 08:47:01 +01003848 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003849}
3850
Simon Horman6f6bb382017-01-04 09:37:26 +01003851static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3852{
3853 return stats_dump_json_schema_to_buffer(appctx->owner);
3854}
3855
Willy Tarreau2b812e22016-11-22 16:18:05 +01003856/* register cli keywords */
3857static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003858 { { "clear", "counters", NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003859 { { "show", "info", NULL }, "show info : report information about the running process", cli_parse_show_info, cli_io_handler_dump_info, NULL },
Willy Tarreau2b812e22016-11-22 16:18:05 +01003860 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01003861 { { "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 +01003862 {{},}
3863}};
3864
Willy Tarreau0108d902018-11-25 19:14:37 +01003865INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3866
William Lallemand74c24fb2016-11-21 17:18:36 +01003867struct applet http_stats_applet = {
3868 .obj_type = OBJ_TYPE_APPLET,
3869 .name = "<STATS>", /* used for logging */
3870 .fct = http_stats_io_handler,
3871 .release = NULL,
3872};
3873
William Lallemand74c24fb2016-11-21 17:18:36 +01003874/*
3875 * Local variables:
3876 * c-indent-level: 8
3877 * c-basic-offset: 8
3878 * End:
3879 */