blob: dae6921fcae009da6e23b079abdcca78a0c9e535 [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>
William Lallemand74c24fb2016-11-21 17:18:36 +010016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <pwd.h>
20#include <grp.h>
21
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020027#include <haproxy/activity.h>
Willy Tarreau91cefca2022-05-03 17:08:29 +020028#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020029#include <haproxy/backend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020031#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020032#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020033#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau55542642021-10-08 09:33:24 +020035#include <haproxy/clock.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020036#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020037#include <haproxy/debug.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020038#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/fd.h>
40#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020041#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020042#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020043#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020044#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020045#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020046#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020047#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020048#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020049#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020050#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020051#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020052#include <haproxy/pipe.h>
53#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020054#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010055#include <haproxy/resolvers.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020056#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020057#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020058#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020059#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020060#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020061#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020062#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020063#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020064#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020065#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020066#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020067#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010068
William Lallemand74c24fb2016-11-21 17:18:36 +010069
Christopher Faulet2b9b6782019-02-27 16:42:58 +010070/* status codes available for the stats admin page (strictly 4 chars length) */
71const char *stat_status_codes[STAT_STATUS_SIZE] = {
72 [STAT_STATUS_DENY] = "DENY",
73 [STAT_STATUS_DONE] = "DONE",
74 [STAT_STATUS_ERRP] = "ERRP",
75 [STAT_STATUS_EXCD] = "EXCD",
76 [STAT_STATUS_NONE] = "NONE",
77 [STAT_STATUS_PART] = "PART",
78 [STAT_STATUS_UNKN] = "UNKN",
79 [STAT_STATUS_IVAL] = "IVAL",
80};
81
Willy Tarreau0baac8c2016-11-22 16:36:53 +010082/* These are the field names for each INF_* field position. Please pay attention
83 * to always use the exact same name except that the strings for new names must
84 * be lower case or CamelCase while the enum entries must be upper case.
85 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020086const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020087 [INF_NAME] = { .name = "Name", .desc = "Product name" },
88 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
89 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
90 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
Willy Tarreau91358592021-06-15 08:08:04 +020091 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (historical, always 1)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +020092 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020093 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
94 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
95 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
William Dauchydefd1562021-01-15 22:41:38 +010096 [INF_START_TIME_SEC] = { .name = "Start_time_sec", .desc = "Start time in seconds" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020097 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
William Dauchya8766cf2021-01-15 22:41:37 +010098 [INF_MEMMAX_BYTES] = { .name = "Memmax_bytes", .desc = "Worker process's hard limit on memory usage in byes (-m on command line)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020099 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100100 [INF_POOL_ALLOC_BYTES] = { .name = "PoolAlloc_bytes", .desc = "Amount of memory allocated in pools (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200101 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100102 [INF_POOL_USED_BYTES] = { .name = "PoolUsed_bytes", .desc = "Amount of pool memory currently used (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200103 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
104 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
105 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
106 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
107 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
108 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
109 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
110 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
111 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
112 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
113 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
114 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
115 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
116 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
117 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
118 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
119 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
120 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
121 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
122 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
123 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
124 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
125 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
126 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
127 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
128 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
129 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
130 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
131 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
132 [INF_SSL_CACHE_MISSES] = { .name = "SslCacheMisses", .desc = "Total number of SSL session ID lookups that didn't find a session in the SSL session cache on this worker since started" },
133 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
134 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
135 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
136 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
137 [INF_MAX_ZLIB_MEM_USAGE] = { .name = "MaxZlibMemUsage", .desc = "Limit on the amount of memory used by HTTP compression above which it is automatically disabled (in bytes, see global.maxzlibmem)" },
138 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
139 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
140 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
141 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
142 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
143 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
144 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
145 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
146 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
147 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
148 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
149 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
150 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
151 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
152 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
Tim Duesterhus0ababda2023-03-27 15:23:44 +0200153 [INF_TOTAL_SPLICED_BYTES_OUT] = { .name = "TotalSplicedBytesOut", .desc = "Total number of bytes emitted by current worker process through a kernel pipe since started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200154 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200155 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Emeric Brun45c457a2020-07-09 23:23:34 +0200156 [INF_CUM_LOG_MSGS] = { .name = "CumRecvLogs", .desc = "Total number of log messages received by log-forwarding listeners on this worker process since started" },
Adis Nezirovicb62b78b2021-01-15 13:12:33 +0100157 [INF_BUILD_INFO] = { .name = "Build info", .desc = "Build info" },
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +0200158 [INF_TAINTED] = { .name = "Tainted", .desc = "Experimental features used" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100159};
160
Willy Tarreaueaa55372019-10-09 07:39:11 +0200161const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200162 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
163 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100164 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
165 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100166 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
167 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200168 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
169 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
170 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
171 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
172 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
173 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
174 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
175 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
176 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
177 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
178 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
179 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
Willy Tarreaubd715102020-10-23 22:44:30 +0200180 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server's effective weight, or sum of active servers' effective weights for a backend" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200181 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
182 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
183 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
184 [ST_F_CHKDOWN] = { .name = "chkdown", .desc = "Total number of failed checks causing UP to DOWN server transitions, per server/backend, since the worker process started" },
185 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
186 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
187 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +0200188 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200189 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
190 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
191 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
192 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
193 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
194 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
195 [ST_F_RATE] = { .name = "rate", .desc = "Total number of sessions processed by this object over the last second (sessions for listeners/frontends, requests for backends/servers)" },
196 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100197 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of sessions per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200198 [ST_F_CHECK_STATUS] = { .name = "check_status", .desc = "Status report of the server's latest health check, prefixed with '*' if a check is currently in progress" },
199 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
200 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
201 [ST_F_HRSP_1XX] = { .name = "hrsp_1xx", .desc = "Total number of HTTP responses with status 100-199 returned by this object since the worker process started" },
202 [ST_F_HRSP_2XX] = { .name = "hrsp_2xx", .desc = "Total number of HTTP responses with status 200-299 returned by this object since the worker process started" },
203 [ST_F_HRSP_3XX] = { .name = "hrsp_3xx", .desc = "Total number of HTTP responses with status 300-399 returned by this object since the worker process started" },
204 [ST_F_HRSP_4XX] = { .name = "hrsp_4xx", .desc = "Total number of HTTP responses with status 400-499 returned by this object since the worker process started" },
205 [ST_F_HRSP_5XX] = { .name = "hrsp_5xx", .desc = "Total number of HTTP responses with status 500-599 returned by this object since the worker process started" },
206 [ST_F_HRSP_OTHER] = { .name = "hrsp_other", .desc = "Total number of HTTP responses with status <100, >599 returned by this object since the worker process started (error -1 included)" },
207 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
208 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100209 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of http requests observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200210 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
211 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
212 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
213 [ST_F_COMP_IN] = { .name = "comp_in", .desc = "Total number of bytes submitted to the HTTP compressor for this object since the worker process started" },
214 [ST_F_COMP_OUT] = { .name = "comp_out", .desc = "Total number of bytes emitted by the HTTP compressor for this object since the worker process started" },
215 [ST_F_COMP_BYP] = { .name = "comp_byp", .desc = "Total number of bytes that bypassed HTTP compression for this object since the worker process started (CPU/memory/bandwidth limitation)" },
216 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
217 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
218 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
219 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
220 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
221 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
222 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
223 [ST_F_TTIME] = { .name = "ttime", .desc = "Total request+response time (request+queue+connect+response+processing), in milliseconds, averaged over the 1024 last requests (backend/server)" },
224 [ST_F_AGENT_STATUS] = { .name = "agent_status", .desc = "Status report of the server's latest agent check, prefixed with '*' if a check is currently in progress" },
225 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
226 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
227 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
228 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
229 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
230 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
231 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
232 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
233 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
234 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
235 [ST_F_ADDR] = { .name = "addr", .desc = "Server's address:port, shown only if show-legends is set, or at levels oper/admin for the CLI" },
236 [ST_F_COOKIE] = { .name = "cookie", .desc = "Backend's cookie name or Server's cookie value, shown only if show-legends is set, or at levels oper/admin for the CLI" },
237 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
238 [ST_F_ALGO] = { .name = "algo", .desc = "Backend's load balancing algorithm, shown only if show-legends is set, or at levels oper/admin for the CLI" },
239 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100240 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of connections per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200241 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
242 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
243 [ST_F_DCON] = { .name = "dcon", .desc = "Total number of incoming connections blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
244 [ST_F_DSES] = { .name = "dses", .desc = "Total number of incoming sessions blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
245 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
246 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
247 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
248 [ST_F_CACHE_LOOKUPS] = { .name = "cache_lookups", .desc = "Total number of HTTP requests looked up in the cache on this frontend/backend since the worker process started" },
249 [ST_F_CACHE_HITS] = { .name = "cache_hits", .desc = "Total number of HTTP requests not found in the cache on this frontend/backend since the worker process started" },
250 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
251 [ST_F_SRV_ILIM] = { .name = "src_ilim", .desc = "Limit on the number of available idle connections on this server (server 'pool_max_conn' directive)" },
Christopher Faulet0d1c2a62019-11-08 14:59:51 +0100252 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
253 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
254 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
255 [ST_F_TT_MAX] = { .name = "ttime_max", .desc = "Maximum observed total request+response time (request+queue+connect+response+processing), in milliseconds (backend/server)" },
Christopher Faulet0159ee42019-12-16 14:40:39 +0100256 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200257 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
258 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
259 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200260 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200261 [ST_F_UWEIGHT] = { .name = "uweight", .desc = "Server's user weight, or sum of active servers' user weights for a backend" },
Cedric Paillet7d6644e2022-12-08 09:17:00 +0000262 [ST_F_AGG_SRV_CHECK_STATUS] = { .name = "agg_server_check_status", .desc = "[DEPRECATED] Backend's aggregated gauge of servers' status" },
263 [ST_F_AGG_SRV_STATUS ] = { .name = "agg_server_status", .desc = "Backend's aggregated gauge of servers' status" },
Cedric Paillete06e31e2022-12-08 09:17:01 +0000264 [ST_F_AGG_CHECK_STATUS] = { .name = "agg_check_status", .desc = "Backend's aggregated gauge of servers' state check status" },
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +0100265 [ST_F_SRID] = { .name = "srid", .desc = "Server id revision, to prevent server id reuse mixups" },
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100266 [ST_F_SESS_OTHER] = { .name = "sess_other", .desc = "Total number of sessions other than HTTP since process started" },
267 [ST_F_H1SESS] = { .name = "h1sess", .desc = "Total number of HTTP/1 sessions since process started" },
268 [ST_F_H2SESS] = { .name = "h2sess", .desc = "Total number of HTTP/2 sessions since process started" },
269 [ST_F_H3SESS] = { .name = "h3sess", .desc = "Total number of HTTP/3 sessions since process started" },
270 [ST_F_REQ_OTHER] = { .name = "req_other", .desc = "Total number of sessions other than HTTP processed by this object since the worker process started" },
271 [ST_F_H1REQ] = { .name = "h1req", .desc = "Total number of HTTP/1 sessions processed by this object since the worker process started" },
272 [ST_F_H2REQ] = { .name = "h2req", .desc = "Total number of hTTP/2 sessions processed by this object since the worker process started" },
273 [ST_F_H3REQ] = { .name = "h3req", .desc = "Total number of HTTP/3 sessions processed by this object since the worker process started" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100274};
275
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100276/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100277THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200278
279/* description of statistics (static and dynamic) */
280static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
281static size_t stat_count[STATS_DOMAIN_COUNT];
282
283/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100284THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100285
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200286/* list of all registered stats module */
287static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
288 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Emeric Brunf8642ee2021-10-29 17:59:18 +0200289 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_RESOLVERS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200290};
291
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100292THREAD_LOCAL void *trash_counters;
Christopher Fauleta8b76842022-12-16 15:08:36 +0100293static THREAD_LOCAL struct buffer trash_chunk = BUF_NULL;
294
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100295
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200296static inline uint8_t stats_get_domain(uint32_t domain)
297{
298 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
299}
300
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200301static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
302{
303 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
304}
305
Christopher Faulet6338a082019-09-09 15:50:54 +0200306static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100307
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200308int stats_putchk(struct appctx *appctx, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +0100309{
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200310 struct stconn *sc = appctx_sc(appctx);
311 struct channel *chn = sc_ic(sc);
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +0100312 struct buffer *chk = &trash_chunk;
313
Christopher Fauletef779222018-10-31 08:47:01 +0100314 if (htx) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200315 if (chk->data >= channel_htx_recv_max(chn, htx)) {
316 sc_need_room(sc, chk->data);
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100317 return 0;
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200318 }
319 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data))) {
320 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +0100321 return 0;
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200322 }
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100323 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100324 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100325 }
326 else {
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200327 if (applet_putchk(appctx, chk) == -1)
Christopher Fauletef779222018-10-31 08:47:01 +0100328 return 0;
329 }
330 return 1;
331}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100332
Willy Tarreaucaff6312022-05-27 10:17:46 +0200333static const char *stats_scope_ptr(struct appctx *appctx, struct stconn *sc)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100334{
Willy Tarreau91cefca2022-05-03 17:08:29 +0200335 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +0200336 struct channel *req = sc_oc(sc);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200337 struct htx *htx = htxbuf(&req->buf);
338 struct htx_blk *blk;
339 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100340
Christopher Fauletb7f88902019-07-15 21:56:43 +0200341 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100342 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200343 ALREADY_CHECKED(blk);
344 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Willy Tarreau91cefca2022-05-03 17:08:29 +0200345 return uri.ptr + ctx->scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100346}
347
William Lallemand74c24fb2016-11-21 17:18:36 +0100348/*
349 * http_stats_io_handler()
350 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
351 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100352 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100353 * -> stats_dump_html_head() // emits the HTML headers
354 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
355 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
356 * -> stats_dump_html_px_hdr()
357 * -> stats_dump_fe_stats()
358 * -> stats_dump_li_stats()
359 * -> stats_dump_sv_stats()
360 * -> stats_dump_be_stats()
361 * -> stats_dump_html_px_end()
362 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100363 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100364 */
365
366
Christopher Fauleta8b76842022-12-16 15:08:36 +0100367/* Dumps the stats CSV header to the local trash buffer. The caller is
368 * responsible for clearing it if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +0100369 * NOTE: Some tools happen to rely on the field position instead of its name,
370 * so please only append new fields at the end, never in the middle.
371 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200372static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100373{
374 int field;
375
Christopher Fauleta8b76842022-12-16 15:08:36 +0100376 chunk_appendf(&trash_chunk, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200377 if (stat_f[domain]) {
378 for (field = 0; field < stat_count[domain]; ++field) {
Christopher Fauleta8b76842022-12-16 15:08:36 +0100379 chunk_appendf(&trash_chunk, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100380
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200381 /* print special delimiter on proxy stats to mark end of
382 static fields */
383 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
Christopher Fauleta8b76842022-12-16 15:08:36 +0100384 chunk_appendf(&trash_chunk, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200385 }
386 }
387
Christopher Fauleta8b76842022-12-16 15:08:36 +0100388 chunk_appendf(&trash_chunk, "\n");
William Lallemand74c24fb2016-11-21 17:18:36 +0100389}
390
William Lallemand74c24fb2016-11-21 17:18:36 +0100391/* Emits a stats field without any surrounding element and properly encoded to
392 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
393 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200394int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100395{
396 switch (field_format(f, 0)) {
397 case FF_EMPTY: return 1;
398 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
399 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
400 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
401 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200402 case FF_FLT: {
403 size_t prev_data = out->data;
404 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
405 return out->data;
406 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100407 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
408 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
409 }
410}
411
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200412const char *field_to_html_str(const struct field *f)
413{
414 switch (field_format(f, 0)) {
415 case FF_S32: return U2H(f->u.s32);
416 case FF_S64: return U2H(f->u.s64);
417 case FF_U64: return U2H(f->u.u64);
418 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200419 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200420 case FF_STR: return field_str(f, 0);
421 case FF_EMPTY:
422 default:
423 return "";
424 }
425}
426
William Lallemand74c24fb2016-11-21 17:18:36 +0100427/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
428 * output is supposed to be used on its own line. Returns non-zero on success, 0
429 * if the buffer is full.
430 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200431int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100432{
433 switch (field_format(f, 0)) {
434 case FF_EMPTY: return 1;
435 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
436 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
437 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
438 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200439 case FF_FLT: {
440 size_t prev_data = out->data;
441 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
442 return out->data;
443 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100444 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
445 default: return chunk_appendf(out, "%08x:?", f->type);
446 }
447}
448
Simon Horman05ee2132017-01-04 09:37:25 +0100449/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
450 * the recommendation for interoperable integers in section 6 of RFC 7159.
451 */
452#define JSON_INT_MAX ((1ULL << 53) - 1)
453#define JSON_INT_MIN (0 - JSON_INT_MAX)
454
455/* Emits a stats field value and its type in JSON.
456 * Returns non-zero on success, 0 on error.
457 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200458int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100459{
460 int old_len;
461 char buf[20];
462 const char *type, *value = buf, *quote = "";
463
464 switch (field_format(f, 0)) {
465 case FF_EMPTY: return 1;
466 case FF_S32: type = "\"s32\"";
467 snprintf(buf, sizeof(buf), "%d", f->u.s32);
468 break;
469 case FF_U32: type = "\"u32\"";
470 snprintf(buf, sizeof(buf), "%u", f->u.u32);
471 break;
472 case FF_S64: type = "\"s64\"";
473 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
474 return 0;
475 type = "\"s64\"";
476 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
477 break;
478 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
479 return 0;
480 type = "\"u64\"";
481 snprintf(buf, sizeof(buf), "%llu",
482 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200483 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200484 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200485 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100486 break;
487 case FF_STR: type = "\"str\"";
488 value = field_str(f, 0);
489 quote = "\"";
490 break;
491 default: snprintf(buf, sizeof(buf), "%u", f->type);
492 type = buf;
493 value = "unknown";
494 quote = "\"";
495 break;
496 }
497
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200498 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100499 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
500 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100502}
503
William Lallemand74c24fb2016-11-21 17:18:36 +0100504/* Emits an encoding of the field type on 3 characters followed by a delimiter.
505 * Returns non-zero on success, 0 if the buffer is full.
506 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200507int stats_emit_field_tags(struct buffer *out, const struct field *f,
508 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100509{
510 char origin, nature, scope;
511
512 switch (field_origin(f, 0)) {
513 case FO_METRIC: origin = 'M'; break;
514 case FO_STATUS: origin = 'S'; break;
515 case FO_KEY: origin = 'K'; break;
516 case FO_CONFIG: origin = 'C'; break;
517 case FO_PRODUCT: origin = 'P'; break;
518 default: origin = '?'; break;
519 }
520
521 switch (field_nature(f, 0)) {
522 case FN_GAUGE: nature = 'G'; break;
523 case FN_LIMIT: nature = 'L'; break;
524 case FN_MIN: nature = 'm'; break;
525 case FN_MAX: nature = 'M'; break;
526 case FN_RATE: nature = 'R'; break;
527 case FN_COUNTER: nature = 'C'; break;
528 case FN_DURATION: nature = 'D'; break;
529 case FN_AGE: nature = 'A'; break;
530 case FN_TIME: nature = 'T'; break;
531 case FN_NAME: nature = 'N'; break;
532 case FN_OUTPUT: nature = 'O'; break;
533 case FN_AVG: nature = 'a'; break;
534 default: nature = '?'; break;
535 }
536
537 switch (field_scope(f, 0)) {
538 case FS_PROCESS: scope = 'P'; break;
539 case FS_SERVICE: scope = 'S'; break;
540 case FS_SYSTEM: scope = 's'; break;
541 case FS_CLUSTER: scope = 'C'; break;
542 default: scope = '?'; break;
543 }
544
545 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
546}
547
Simon Horman05ee2132017-01-04 09:37:25 +0100548/* Emits an encoding of the field type as JSON.
549 * Returns non-zero on success, 0 if the buffer is full.
550 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200551int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100552{
553 const char *origin, *nature, *scope;
554 int old_len;
555
556 switch (field_origin(f, 0)) {
557 case FO_METRIC: origin = "Metric"; break;
558 case FO_STATUS: origin = "Status"; break;
559 case FO_KEY: origin = "Key"; break;
560 case FO_CONFIG: origin = "Config"; break;
561 case FO_PRODUCT: origin = "Product"; break;
562 default: origin = "Unknown"; break;
563 }
564
565 switch (field_nature(f, 0)) {
566 case FN_GAUGE: nature = "Gauge"; break;
567 case FN_LIMIT: nature = "Limit"; break;
568 case FN_MIN: nature = "Min"; break;
569 case FN_MAX: nature = "Max"; break;
570 case FN_RATE: nature = "Rate"; break;
571 case FN_COUNTER: nature = "Counter"; break;
572 case FN_DURATION: nature = "Duration"; break;
573 case FN_AGE: nature = "Age"; break;
574 case FN_TIME: nature = "Time"; break;
575 case FN_NAME: nature = "Name"; break;
576 case FN_OUTPUT: nature = "Output"; break;
577 case FN_AVG: nature = "Avg"; break;
578 default: nature = "Unknown"; break;
579 }
580
581 switch (field_scope(f, 0)) {
582 case FS_PROCESS: scope = "Process"; break;
583 case FS_SERVICE: scope = "Service"; break;
584 case FS_SYSTEM: scope = "System"; break;
585 case FS_CLUSTER: scope = "Cluster"; break;
586 default: scope = "Unknown"; break;
587 }
588
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200589 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100590 chunk_appendf(out, "\"tags\":{"
591 "\"origin\":\"%s\","
592 "\"nature\":\"%s\","
593 "\"scope\":\"%s\""
594 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200595 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100596}
William Lallemand74c24fb2016-11-21 17:18:36 +0100597
598/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200599static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200600 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100601 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100602{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100603 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100604 int field;
605
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200606 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100607 if (!stats_emit_raw_data_field(out, &stats[field]))
608 return 0;
609 if (!chunk_strcat(out, ","))
610 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200611
612 /* print special delimiter on proxy stats to mark end of
613 static fields */
614 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
615 if (!chunk_strcat(out, "-,"))
616 return 0;
617 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100618 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200619
William Lallemand74c24fb2016-11-21 17:18:36 +0100620 chunk_strcat(out, "\n");
621 return 1;
622}
623
624/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200625static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200626 const struct field *stats,
627 size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100628 struct show_stat_ctx * ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100629{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100630 int flags = ctx->flags;
631 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100632 int field;
633
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200634 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100635 if (!stats[field].type)
636 continue;
637
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200638 switch (domain) {
639 case STATS_DOMAIN_PROXY:
640 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
641 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
642 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
643 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
644 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
645 '?',
646 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
647 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200648 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200649 stats[ST_F_PID].u.u32);
650 break;
651
Emeric Brunf8642ee2021-10-29 17:59:18 +0200652 case STATS_DOMAIN_RESOLVERS:
653 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200654 stat_f[domain][field].name);
655 break;
656
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200657 default:
658 break;
659 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100660
661 if (!stats_emit_field_tags(out, &stats[field], ':'))
662 return 0;
663 if (!stats_emit_typed_data_field(out, &stats[field]))
664 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200665
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200666 if (flags & STAT_SHOW_FDESC &&
667 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200668 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200669 }
670
William Lallemand74c24fb2016-11-21 17:18:36 +0100671 if (!chunk_strcat(out, "\n"))
672 return 0;
673 }
674 return 1;
675}
676
Simon Horman05ee2132017-01-04 09:37:25 +0100677/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200678static int stats_dump_json_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100679 const struct field *info,
680 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100681{
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100682 int started = (ctx->field) ? 1 : 0;
683 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100684
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100685 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100686 return 0;
687
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100688 for (; ctx->field < INF_TOTAL_FIELDS; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100689 int old_len;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100690 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100691
692 if (!field_format(info, field))
693 continue;
694
695 if (started && !chunk_strcat(out, ","))
696 goto err;
697 started = 1;
698
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200699 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100700 chunk_appendf(out,
701 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
702 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200703 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100704 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200705 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100706 goto err;
707
708 if (!stats_emit_json_field_tags(out, &info[field]))
709 goto err;
710
711 if (!stats_emit_json_data_field(out, &info[field]))
712 goto err;
713
714 if (!chunk_strcat(out, "}"))
715 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100716 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100717 }
718
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200719 if (!chunk_strcat(out, "]\n"))
Simon Horman05ee2132017-01-04 09:37:25 +0100720 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100721 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100722 return 1;
723
724err:
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100725 if (!ready_data) {
726 /* not enough buffer space for a single entry.. */
727 chunk_reset(out);
728 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
729 return 0; /* hard error */
730 }
731 /* push ready data and wait for a new buffer to complete the dump */
732 out->data = ready_data;
733 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100734}
735
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200736static void stats_print_proxy_field_json(struct buffer *out,
737 const struct field *stat,
738 const char *name,
739 int pos,
740 uint32_t field_type,
741 uint32_t iid,
742 uint32_t sid,
743 uint32_t pid)
744{
745 const char *obj_type;
746 switch (field_type) {
747 case STATS_TYPE_FE: obj_type = "Frontend"; break;
748 case STATS_TYPE_BE: obj_type = "Backend"; break;
749 case STATS_TYPE_SO: obj_type = "Listener"; break;
750 case STATS_TYPE_SV: obj_type = "Server"; break;
751 default: obj_type = "Unknown"; break;
752 }
753
754 chunk_appendf(out,
755 "{"
756 "\"objType\":\"%s\","
757 "\"proxyId\":%u,"
758 "\"id\":%u,"
759 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
760 "\"processNum\":%u,",
761 obj_type, iid, sid, pos, name, pid);
762}
763
Emeric Brunf8642ee2021-10-29 17:59:18 +0200764static void stats_print_rslv_field_json(struct buffer *out,
765 const struct field *stat,
766 const char *name,
767 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200768{
769 chunk_appendf(out,
770 "{"
771 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
772 pos, name);
773}
774
775
Simon Horman05ee2132017-01-04 09:37:25 +0100776/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200777static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200778 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100779 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100780{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100781 int flags = ctx->flags;
782 int domain = ctx->domain;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100783 int started = (ctx->field) ? 1 : 0;
784 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100785
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100786 if (!started && (flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100787 return 0;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100788 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100789 return 0;
790
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100791 for (; ctx->field < stats_count; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100792 int old_len;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100793 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100794
795 if (!stats[field].type)
796 continue;
797
798 if (started && !chunk_strcat(out, ","))
799 goto err;
800 started = 1;
801
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200802 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200803 if (domain == STATS_DOMAIN_PROXY) {
804 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200805 stat_f[domain][field].name,
806 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200807 stats[ST_F_TYPE].u.u32,
808 stats[ST_F_IID].u.u32,
809 stats[ST_F_SID].u.u32,
810 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200811 } else if (domain == STATS_DOMAIN_RESOLVERS) {
812 stats_print_rslv_field_json(out, &stats[field],
813 stat_f[domain][field].name,
814 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200815 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200816
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200817 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100818 goto err;
819
820 if (!stats_emit_json_field_tags(out, &stats[field]))
821 goto err;
822
823 if (!stats_emit_json_data_field(out, &stats[field]))
824 goto err;
825
826 if (!chunk_strcat(out, "}"))
827 goto err;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100828 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100829 }
830
831 if (!chunk_strcat(out, "]"))
832 goto err;
833
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100834 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100835 return 1;
836
837err:
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100838 if (!ready_data) {
839 /* not enough buffer space for a single entry.. */
840 chunk_reset(out);
841 if (ctx->flags & STAT_STARTED)
842 chunk_strcat(out, ",");
843 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
844 return 0; /* hard error */
845 }
846 /* push ready data and wait for a new buffer to complete the dump */
847 out->data = ready_data;
848 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100849}
850
William Lallemand74c24fb2016-11-21 17:18:36 +0100851/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200852 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200853 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
854 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
855 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100856 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200857static int stats_dump_fields_html(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100858 const struct field *stats,
859 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100860{
Willy Tarreau83061a82018-07-13 11:56:34 +0200861 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200862 struct stats_module *mod;
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100863 int flags = ctx->flags;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200864 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100865
866 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
867 chunk_appendf(out,
868 /* name, queue */
869 "<tr class=\"frontend\">");
870
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200871 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100872 /* Column sub-heading for Enable or Disable server */
873 chunk_appendf(out, "<td></td>");
874 }
875
876 chunk_appendf(out,
877 "<td class=ac>"
878 "<a name=\"%s/Frontend\"></a>"
879 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
880 "<td colspan=3></td>"
881 "",
882 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
883
884 chunk_appendf(out,
885 /* sessions rate : current */
886 "<td><u>%s<div class=tips><table class=det>"
887 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
888 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
889 "",
890 U2H(stats[ST_F_RATE].u.u32),
891 U2H(stats[ST_F_CONN_RATE].u.u32),
892 U2H(stats[ST_F_RATE].u.u32));
893
894 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
895 chunk_appendf(out,
896 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
897 U2H(stats[ST_F_REQ_RATE].u.u32));
898
899 chunk_appendf(out,
900 "</table></div></u></td>"
901 /* sessions rate : max */
902 "<td><u>%s<div class=tips><table class=det>"
903 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
904 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
905 "",
906 U2H(stats[ST_F_RATE_MAX].u.u32),
907 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
908 U2H(stats[ST_F_RATE_MAX].u.u32));
909
910 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
911 chunk_appendf(out,
912 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
913 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
914
915 chunk_appendf(out,
916 "</table></div></u></td>"
917 /* sessions rate : limit */
918 "<td>%s</td>",
919 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
920
921 chunk_appendf(out,
922 /* sessions: current, max, limit, total */
923 "<td>%s</td><td>%s</td><td>%s</td>"
924 "<td><u>%s<div class=tips><table class=det>"
925 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
926 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
927 "",
928 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
929 U2H(stats[ST_F_STOT].u.u64),
930 U2H(stats[ST_F_CONN_TOT].u.u64),
931 U2H(stats[ST_F_STOT].u.u64));
932
933 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
934 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
935 chunk_appendf(out,
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100936 "<tr><th>- HTTP/1 sessions:</th><td>%s</td></tr>"
937 "<tr><th>- HTTP/2 sessions:</th><td>%s</td></tr>"
938 "<tr><th>- HTTP/3 sessions:</th><td>%s</td></tr>"
939 "<tr><th>- other sessions:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100940 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100941 "<tr><th>- HTTP/1 requests:</th><td>%s</td></tr>"
942 "<tr><th>- HTTP/2 requests:</th><td>%s</td></tr>"
943 "<tr><th>- HTTP/3 requests:</th><td>%s</td></tr>"
944 "<tr><th>- other requests:</th><td>%s</td></tr>"
945 "",
946 U2H(stats[ST_F_H1SESS].u.u64),
947 U2H(stats[ST_F_H2SESS].u.u64),
948 U2H(stats[ST_F_H3SESS].u.u64),
949 U2H(stats[ST_F_SESS_OTHER].u.u64),
950 U2H(stats[ST_F_REQ_TOT].u.u64),
951 U2H(stats[ST_F_H1REQ].u.u64),
952 U2H(stats[ST_F_H2REQ].u.u64),
953 U2H(stats[ST_F_H3REQ].u.u64),
954 U2H(stats[ST_F_REQ_OTHER].u.u64));
955
956 chunk_appendf(out,
William Lallemand74c24fb2016-11-21 17:18:36 +0100957 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
958 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
959 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
960 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
961 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
962 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
963 "<tr><th>- other responses:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100964 "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100965 U2H(stats[ST_F_HRSP_1XX].u.u64),
966 U2H(stats[ST_F_HRSP_2XX].u.u64),
967 U2H(stats[ST_F_COMP_RSP].u.u64),
968 stats[ST_F_HRSP_2XX].u.u64 ?
969 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
970 U2H(stats[ST_F_HRSP_3XX].u.u64),
971 U2H(stats[ST_F_HRSP_4XX].u.u64),
972 U2H(stats[ST_F_HRSP_5XX].u.u64),
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100973 U2H(stats[ST_F_HRSP_OTHER].u.u64));
974
975 chunk_appendf(out,
976 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
977 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
978 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
979 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
980 "<tr><th>Internal errors:</th><td>%s</td></tr>"
981 "",
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200982 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100983 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
984 U2H(stats[ST_F_CACHE_HITS].u.u64),
985 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
986 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100987 U2H(stats[ST_F_WREW].u.u64),
988 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100989 }
990
991 chunk_appendf(out,
992 "</table></div></u></td>"
993 /* sessions: lbtot, lastsess */
994 "<td></td><td></td>"
995 /* bytes : in */
996 "<td>%s</td>"
997 "",
998 U2H(stats[ST_F_BIN].u.u64));
999
1000 chunk_appendf(out,
1001 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1002 "<td>%s%s<div class=tips><table class=det>"
1003 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1004 "<tr><th>Compression in:</th><td>%s</td></tr>"
1005 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1006 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1007 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1008 "</table></div>%s</td>",
1009 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1010 U2H(stats[ST_F_BOUT].u.u64),
1011 U2H(stats[ST_F_BOUT].u.u64),
1012 U2H(stats[ST_F_COMP_IN].u.u64),
1013 U2H(stats[ST_F_COMP_OUT].u.u64),
1014 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1015 U2H(stats[ST_F_COMP_BYP].u.u64),
1016 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1017 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,
1018 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1019
1020 chunk_appendf(out,
1021 /* denied: req, resp */
1022 "<td>%s</td><td>%s</td>"
1023 /* errors : request, connect, response */
1024 "<td>%s</td><td></td><td></td>"
1025 /* warnings: retries, redispatches */
1026 "<td></td><td></td>"
1027 /* server status : reflect frontend status */
1028 "<td class=ac>%s</td>"
1029 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001030 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001031 "",
1032 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1033 U2H(stats[ST_F_EREQ].u.u64),
1034 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001035
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001036 if (flags & STAT_SHMODULES) {
1037 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001038 chunk_appendf(out, "<td>");
1039
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001040 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001041 chunk_appendf(out,
1042 "<u>%s<div class=tips><table class=det>",
1043 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001044 for (j = 0; j < mod->stats_count; ++j) {
1045 chunk_appendf(out,
1046 "<tr><th>%s</th><td>%s</td></tr>",
1047 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1048 ++i;
1049 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001050 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001051 } else {
1052 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001053 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001054
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001055 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001056 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001057 }
1058
1059 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001060 }
1061 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
1062 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001063 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001064 /* Column sub-heading for Enable or Disable server */
1065 chunk_appendf(out, "<td></td>");
1066 }
1067
1068 chunk_appendf(out,
1069 /* frontend name, listener name */
1070 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1071 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1072 "",
1073 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001074 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001075 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1076
Willy Tarreau708c4162019-10-09 10:19:16 +02001077 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001078 chunk_appendf(out, "<div class=tips>");
1079
Willy Tarreau90807112020-02-25 08:16:33 +01001080 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001081 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1082 else if (*field_str(stats, ST_F_ADDR) == '[')
1083 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1084 else if (*field_str(stats, ST_F_ADDR))
1085 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1086
1087 /* id */
1088 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1089 }
1090
1091 chunk_appendf(out,
1092 /* queue */
1093 "%s</td><td colspan=3></td>"
1094 /* sessions rate: current, max, limit */
1095 "<td colspan=3>&nbsp;</td>"
1096 /* sessions: current, max, limit, total, lbtot, lastsess */
1097 "<td>%s</td><td>%s</td><td>%s</td>"
1098 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1099 /* bytes: in, out */
1100 "<td>%s</td><td>%s</td>"
1101 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001102 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001103 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1104 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1105
1106 chunk_appendf(out,
1107 /* denied: req, resp */
1108 "<td>%s</td><td>%s</td>"
1109 /* errors: request, connect, response */
1110 "<td>%s</td><td></td><td></td>"
1111 /* warnings: retries, redispatches */
1112 "<td></td><td></td>"
1113 /* server status: reflect listener status */
1114 "<td class=ac>%s</td>"
1115 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001116 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001117 "",
1118 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1119 U2H(stats[ST_F_EREQ].u.u64),
1120 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001121
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001122 if (flags & STAT_SHMODULES) {
1123 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001124 chunk_appendf(out, "<td>");
1125
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001126 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001127 chunk_appendf(out,
1128 "<u>%s<div class=tips><table class=det>",
1129 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001130 for (j = 0; j < mod->stats_count; ++j) {
1131 chunk_appendf(out,
1132 "<tr><th>%s</th><td>%s</td></tr>",
1133 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1134 ++i;
1135 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001136 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001137 } else {
1138 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001139 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001140
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001141 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001142 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001143 }
1144
1145 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001146 }
1147 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1148 const char *style;
1149
1150 /* determine the style to use depending on the server's state,
1151 * its health and weight. There isn't a 1-to-1 mapping between
1152 * state and styles for the cases where the server is (still)
1153 * up. The reason is that we don't want to report nolb and
1154 * drain with the same color.
1155 */
1156
1157 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1158 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1159 style = "down";
1160 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001161 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001162 style = "going_up";
1163 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001164 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1165 style = "draining";
1166 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001167 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001168 style = "going_down";
1169 }
1170 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1171 style = "nolb";
1172 }
1173 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1174 style = "no_check";
1175 }
1176 else if (!stats[ST_F_CHKFAIL].type ||
1177 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1178 /* no check or max health = UP */
1179 if (stats[ST_F_WEIGHT].u.u32)
1180 style = "up";
1181 else
1182 style = "draining";
1183 }
1184 else {
1185 style = "going_down";
1186 }
1187
Willy Tarreau7b524852020-08-11 10:26:36 +02001188 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001189 chunk_appendf(out, "<tr class=\"maintain\">");
1190 else
1191 chunk_appendf(out,
1192 "<tr class=\"%s_%s\">",
1193 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1194
1195
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001196 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001197 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001198 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1199 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001200 field_str(stats, ST_F_SVNAME));
1201
1202 chunk_appendf(out,
1203 "<td class=ac><a name=\"%s/%s\"></a>%s"
1204 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1205 "",
1206 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001207 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001208 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1209
Willy Tarreau708c4162019-10-09 10:19:16 +02001210 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001211 chunk_appendf(out, "<div class=tips>");
1212
Willy Tarreau90807112020-02-25 08:16:33 +01001213 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001214 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1215 else if (*field_str(stats, ST_F_ADDR) == '[')
1216 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1217 else if (*field_str(stats, ST_F_ADDR))
1218 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1219
1220 /* id */
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01001221 chunk_appendf(out, "id: %d, rid: %d", stats[ST_F_SID].u.u32, stats[ST_F_SRID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001222
1223 /* cookie */
1224 if (stats[ST_F_COOKIE].type) {
1225 chunk_appendf(out, ", cookie: '");
1226 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1227 chunk_htmlencode(out, &src);
1228 chunk_appendf(out, "'");
1229 }
1230
1231 chunk_appendf(out, "</div>");
1232 }
1233
1234 chunk_appendf(out,
1235 /* queue : current, max, limit */
1236 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1237 /* sessions rate : current, max, limit */
1238 "<td>%s</td><td>%s</td><td></td>"
1239 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001240 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001241 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1242 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1243
1244 chunk_appendf(out,
1245 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001246 "<td><u>%s<div class=tips>"
1247 "<table class=det>"
1248 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001249 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001250 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001251 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1252 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001253 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001254 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1255 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1256 "</table></div></u>"
1257 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001258 "<td><u>%s<div class=tips><table class=det>"
1259 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1260 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001261 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001262 U2H(stats[ST_F_SCUR].u.u32),
1263 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1264 U2H(stats[ST_F_SRV_ICUR].u.u32),
1265 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1266 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001267 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001268
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001269 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1270 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1271 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001272 U2H(stats[ST_F_STOT].u.u64),
1273 U2H(stats[ST_F_STOT].u.u64));
1274
1275 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1276 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001277 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001278 "<tr><th>New connections:</th><td>%s</td></tr>"
1279 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001280 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001281 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1282 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1283 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1284 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1285 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1286 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001287 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001288 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001289 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001290 U2H(stats[ST_F_CONNECT].u.u64),
1291 U2H(stats[ST_F_REUSE].u.u64),
1292 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1293 (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0,
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001294 U2H(stats[ST_F_REQ_TOT].u.u64),
1295 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1296 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1297 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1298 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1299 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1300 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1301 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1302 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1303 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1304 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1305 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1306 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001307 U2H(stats[ST_F_WREW].u.u64),
1308 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001309 }
1310
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001311 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1312 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1313 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1314 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1315 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001316 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001317 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1318 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1319 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1320 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001321
1322 chunk_appendf(out,
1323 "</table></div></u></td>"
1324 /* sessions: lbtot, last */
1325 "<td>%s</td><td>%s</td>",
1326 U2H(stats[ST_F_LBTOT].u.u64),
1327 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1328
1329 chunk_appendf(out,
1330 /* bytes : in, out */
1331 "<td>%s</td><td>%s</td>"
1332 /* denied: req, resp */
1333 "<td></td><td>%s</td>"
1334 /* errors : request, connect */
1335 "<td></td><td>%s</td>"
1336 /* errors : response */
1337 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1338 /* warnings: retries, redispatches */
1339 "<td>%lld</td><td>%lld</td>"
1340 "",
1341 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1342 U2H(stats[ST_F_DRESP].u.u64),
1343 U2H(stats[ST_F_ECON].u.u64),
1344 U2H(stats[ST_F_ERESP].u.u64),
1345 (long long)stats[ST_F_CLI_ABRT].u.u64,
1346 (long long)stats[ST_F_SRV_ABRT].u.u64,
1347 (long long)stats[ST_F_WRETR].u.u64,
1348 (long long)stats[ST_F_WREDIS].u.u64);
1349
1350 /* status, last change */
1351 chunk_appendf(out, "<td class=ac>");
1352
1353 /* FIXME!!!!
1354 * LASTCHG should contain the last change for *this* server and must be computed
1355 * properly above, as was done below, ie: this server if maint, otherwise ref server
1356 * if tracking. Note that ref is either local or remote depending on tracking.
1357 */
1358
1359
Willy Tarreau7b524852020-08-11 10:26:36 +02001360 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001361 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1362 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001363 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001364 chunk_strcat(out, "<i>no check</i>");
1365 }
1366 else {
1367 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
Willy Tarreau7b524852020-08-11 10:26:36 +02001368 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001369 if (stats[ST_F_CHECK_HEALTH].u.u32)
1370 chunk_strcat(out, " &uarr;");
1371 }
1372 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1373 chunk_strcat(out, " &darr;");
1374 }
1375
Willy Tarreau7b524852020-08-11 10:26:36 +02001376 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001377 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1378 chunk_appendf(out,
1379 "</td><td class=ac><u> %s",
1380 field_str(stats, ST_F_AGENT_STATUS));
1381
1382 if (stats[ST_F_AGENT_CODE].type)
1383 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1384
1385 if (stats[ST_F_AGENT_DURATION].type)
1386 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1387
1388 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1389
1390 if (*field_str(stats, ST_F_LAST_AGT)) {
1391 chunk_appendf(out, ": ");
1392 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1393 chunk_htmlencode(out, &src);
1394 }
1395 chunk_appendf(out, "</div></u>");
1396 }
1397 else if (stats[ST_F_CHECK_STATUS].type) {
1398 chunk_appendf(out,
1399 "</td><td class=ac><u> %s",
1400 field_str(stats, ST_F_CHECK_STATUS));
1401
1402 if (stats[ST_F_CHECK_CODE].type)
1403 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1404
1405 if (stats[ST_F_CHECK_DURATION].type)
1406 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1407
1408 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1409
1410 if (*field_str(stats, ST_F_LAST_CHK)) {
1411 chunk_appendf(out, ": ");
1412 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1413 chunk_htmlencode(out, &src);
1414 }
1415 chunk_appendf(out, "</div></u>");
1416 }
1417 else
1418 chunk_appendf(out, "</td><td>");
1419
1420 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001421 /* weight / uweight */
1422 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001423 /* act, bck */
1424 "<td class=ac>%s</td><td class=ac>%s</td>"
1425 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001426 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001427 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1428 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1429
1430 /* check failures: unique, fatal, down time */
1431 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1432 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1433 }
1434 else if (stats[ST_F_CHKFAIL].type) {
1435 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1436
1437 if (stats[ST_F_HANAFAIL].type)
1438 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1439
1440 chunk_appendf(out,
1441 "<div class=tips>Failed Health Checks%s</div></u></td>"
1442 "<td>%lld</td><td>%s</td>"
1443 "",
1444 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1445 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1446 }
1447 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1448 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1449 chunk_appendf(out,
1450 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1451 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1452 }
1453 else
1454 chunk_appendf(out, "<td colspan=3></td>");
1455
1456 /* throttle */
1457 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001458 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001459 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001460 chunk_appendf(out, "<td class=ac>-</td>");
1461
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001462 if (flags & STAT_SHMODULES) {
1463 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001464 chunk_appendf(out, "<td>");
1465
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001466 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001467 chunk_appendf(out,
1468 "<u>%s<div class=tips><table class=det>",
1469 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001470 for (j = 0; j < mod->stats_count; ++j) {
1471 chunk_appendf(out,
1472 "<tr><th>%s</th><td>%s</td></tr>",
1473 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1474 ++i;
1475 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001476 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001477 } else {
1478 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001479 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001480
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001481 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001482 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001483 }
1484
1485 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001486 }
1487 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1488 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001489 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001490 /* Column sub-heading for Enable or Disable server */
1491 chunk_appendf(out, "<td></td>");
1492 }
1493 chunk_appendf(out,
1494 "<td class=ac>"
1495 /* name */
1496 "%s<a name=\"%s/Backend\"></a>"
1497 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1498 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001499 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001500 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1501
Willy Tarreau708c4162019-10-09 10:19:16 +02001502 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001503 /* balancing */
1504 chunk_appendf(out, "<div class=tips>balancing: %s",
1505 field_str(stats, ST_F_ALGO));
1506
1507 /* cookie */
1508 if (stats[ST_F_COOKIE].type) {
1509 chunk_appendf(out, ", cookie: '");
1510 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1511 chunk_htmlencode(out, &src);
1512 chunk_appendf(out, "'");
1513 }
1514 chunk_appendf(out, "</div>");
1515 }
1516
1517 chunk_appendf(out,
1518 "%s</td>"
1519 /* queue : current, max */
1520 "<td>%s</td><td>%s</td><td></td>"
1521 /* sessions rate : current, max, limit */
1522 "<td>%s</td><td>%s</td><td></td>"
1523 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001524 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001525 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1526 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1527
1528 chunk_appendf(out,
1529 /* sessions: current, max, limit, total */
1530 "<td>%s</td><td>%s</td><td>%s</td>"
1531 "<td><u>%s<div class=tips><table class=det>"
1532 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1533 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001534 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 +01001535 U2H(stats[ST_F_STOT].u.u64),
1536 U2H(stats[ST_F_STOT].u.u64));
1537
1538 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1539 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1540 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001541 "<tr><th>New connections:</th><td>%s</td></tr>"
1542 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001543 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1544 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1545 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1546 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1547 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1548 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1549 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1550 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001551 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1552 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001553 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001554 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001555 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001556 U2H(stats[ST_F_CONNECT].u.u64),
1557 U2H(stats[ST_F_REUSE].u.u64),
1558 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1559 (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 +01001560 U2H(stats[ST_F_REQ_TOT].u.u64),
1561 U2H(stats[ST_F_HRSP_1XX].u.u64),
1562 U2H(stats[ST_F_HRSP_2XX].u.u64),
1563 U2H(stats[ST_F_COMP_RSP].u.u64),
1564 stats[ST_F_HRSP_2XX].u.u64 ?
1565 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1566 U2H(stats[ST_F_HRSP_3XX].u.u64),
1567 U2H(stats[ST_F_HRSP_4XX].u.u64),
1568 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001569 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001570 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1571 U2H(stats[ST_F_CACHE_HITS].u.u64),
1572 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1573 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001574 U2H(stats[ST_F_WREW].u.u64),
1575 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001576 }
1577
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001578 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1579 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1580 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1581 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1582 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001583 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001584 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1585 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1586 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1587 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001588
1589 chunk_appendf(out,
1590 "</table></div></u></td>"
1591 /* sessions: lbtot, last */
1592 "<td>%s</td><td>%s</td>"
1593 /* bytes: in */
1594 "<td>%s</td>"
1595 "",
1596 U2H(stats[ST_F_LBTOT].u.u64),
1597 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1598 U2H(stats[ST_F_BIN].u.u64));
1599
1600 chunk_appendf(out,
1601 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1602 "<td>%s%s<div class=tips><table class=det>"
1603 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1604 "<tr><th>Compression in:</th><td>%s</td></tr>"
1605 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1606 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1607 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1608 "</table></div>%s</td>",
1609 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1610 U2H(stats[ST_F_BOUT].u.u64),
1611 U2H(stats[ST_F_BOUT].u.u64),
1612 U2H(stats[ST_F_COMP_IN].u.u64),
1613 U2H(stats[ST_F_COMP_OUT].u.u64),
1614 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1615 U2H(stats[ST_F_COMP_BYP].u.u64),
1616 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1617 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,
1618 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1619
1620 chunk_appendf(out,
1621 /* denied: req, resp */
1622 "<td>%s</td><td>%s</td>"
1623 /* errors : request, connect */
1624 "<td></td><td>%s</td>"
1625 /* errors : response */
1626 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1627 /* warnings: retries, redispatches */
1628 "<td>%lld</td><td>%lld</td>"
1629 /* backend status: reflect backend status (up/down): we display UP
1630 * if the backend has known working servers or if it has no server at
1631 * all (eg: for stats). Then we display the total weight, number of
1632 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001633 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001634 "<td class=ac>%d</td><td class=ac>%d</td>"
1635 "",
1636 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1637 U2H(stats[ST_F_ECON].u.u64),
1638 U2H(stats[ST_F_ERESP].u.u64),
1639 (long long)stats[ST_F_CLI_ABRT].u.u64,
1640 (long long)stats[ST_F_SRV_ABRT].u.u64,
1641 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1642 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1643 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreaubd715102020-10-23 22:44:30 +02001644 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001645 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1646
1647 chunk_appendf(out,
1648 /* rest of backend: nothing, down transitions, total downtime, throttle */
1649 "<td class=ac>&nbsp;</td><td>%d</td>"
1650 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001651 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001652 stats[ST_F_CHKDOWN].u.u32,
1653 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001654
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001655 if (flags & STAT_SHMODULES) {
1656 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001657 chunk_appendf(out, "<td>");
1658
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001659 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001660 chunk_appendf(out,
1661 "<u>%s<div class=tips><table class=det>",
1662 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001663 for (j = 0; j < mod->stats_count; ++j) {
1664 chunk_appendf(out,
1665 "<tr><th>%s</th><td>%s</td></tr>",
1666 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1667 ++i;
1668 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001669 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001670 } else {
1671 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001672 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001673
1674 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001675 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001676 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001677
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001678 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001679 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001680
William Lallemand74c24fb2016-11-21 17:18:36 +01001681 return 1;
1682}
1683
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001684int stats_dump_one_line(const struct field *stats, size_t stats_count,
1685 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001686{
Willy Tarreau91cefca2022-05-03 17:08:29 +02001687 struct show_stat_ctx *ctx = appctx->svcctx;
Simon Horman05ee2132017-01-04 09:37:25 +01001688 int ret;
1689
Willy Tarreau91cefca2022-05-03 17:08:29 +02001690 if (ctx->flags & STAT_FMT_HTML)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001691 ret = stats_dump_fields_html(&trash_chunk, stats, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001692 else if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001693 ret = stats_dump_fields_typed(&trash_chunk, stats, stats_count, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001694 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001695 ret = stats_dump_fields_json(&trash_chunk, stats, stats_count, ctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001696 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01001697 ret = stats_dump_fields_csv(&trash_chunk, stats, stats_count, ctx);
Simon Horman05ee2132017-01-04 09:37:25 +01001698
Simon Horman05ee2132017-01-04 09:37:25 +01001699 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001700}
1701
William Dauchy0ef54392021-01-17 18:27:45 +01001702/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001703 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1704 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1705 * this value, or if the selected field is not implemented for frontends, the
1706 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001707 */
William Dauchy0ef54392021-01-17 18:27:45 +01001708int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1709 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001710{
William Dauchy0ef54392021-01-17 18:27:45 +01001711 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001712
William Lallemand74c24fb2016-11-21 17:18:36 +01001713 if (len < ST_F_TOTAL_FIELDS)
1714 return 0;
1715
William Dauchy0ef54392021-01-17 18:27:45 +01001716 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001717 struct field metric = { 0 };
1718
William Dauchy0ef54392021-01-17 18:27:45 +01001719 switch (current_field) {
1720 case ST_F_PXNAME:
1721 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1722 break;
1723 case ST_F_SVNAME:
1724 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1725 break;
1726 case ST_F_MODE:
1727 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1728 break;
1729 case ST_F_SCUR:
1730 metric = mkf_u32(0, px->feconn);
1731 break;
1732 case ST_F_SMAX:
1733 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1734 break;
1735 case ST_F_SLIM:
1736 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1737 break;
1738 case ST_F_STOT:
1739 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1740 break;
1741 case ST_F_BIN:
1742 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1743 break;
1744 case ST_F_BOUT:
1745 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1746 break;
1747 case ST_F_DREQ:
1748 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1749 break;
1750 case ST_F_DRESP:
1751 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1752 break;
1753 case ST_F_EREQ:
1754 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1755 break;
1756 case ST_F_DCON:
1757 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1758 break;
1759 case ST_F_DSES:
1760 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1761 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001762 case ST_F_STATUS: {
1763 const char *state;
1764
1765 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1766 state = "STOP";
1767 else if (px->flags & PR_FL_PAUSED)
1768 state = "PAUSED";
1769 else
1770 state = "OPEN";
1771 metric = mkf_str(FO_STATUS, state);
William Dauchy0ef54392021-01-17 18:27:45 +01001772 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001773 }
William Dauchy0ef54392021-01-17 18:27:45 +01001774 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001775 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001776 break;
1777 case ST_F_IID:
1778 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1779 break;
1780 case ST_F_SID:
1781 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1782 break;
1783 case ST_F_TYPE:
1784 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1785 break;
1786 case ST_F_RATE:
1787 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1788 break;
1789 case ST_F_RATE_LIM:
1790 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1791 break;
1792 case ST_F_RATE_MAX:
1793 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1794 break;
1795 case ST_F_WREW:
1796 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1797 break;
1798 case ST_F_EINT:
1799 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1800 break;
1801 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001802 if (px->mode == PR_MODE_HTTP)
1803 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001804 break;
1805 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001806 if (px->mode == PR_MODE_HTTP)
1807 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001808 break;
1809 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001810 if (px->mode == PR_MODE_HTTP)
1811 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001812 break;
1813 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001814 if (px->mode == PR_MODE_HTTP)
1815 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001816 break;
1817 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001818 if (px->mode == PR_MODE_HTTP)
1819 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001820 break;
1821 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001822 if (px->mode == PR_MODE_HTTP)
1823 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001824 break;
1825 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001826 if (px->mode == PR_MODE_HTTP)
1827 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001828 break;
1829 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001830 if (px->mode == PR_MODE_HTTP)
1831 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001832 break;
1833 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001834 if (px->mode == PR_MODE_HTTP)
1835 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001836 break;
1837 case ST_F_REQ_RATE:
1838 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1839 break;
1840 case ST_F_REQ_RATE_MAX:
1841 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1842 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001843 case ST_F_REQ_TOT: {
1844 int i;
1845 uint64_t total_req;
1846 size_t nb_reqs =
1847 sizeof(px->fe_counters.p.http.cum_req) / sizeof(*px->fe_counters.p.http.cum_req);
1848
1849 total_req = 0;
1850 for (i = 0; i < nb_reqs; i++)
1851 total_req += px->fe_counters.p.http.cum_req[i];
1852 metric = mkf_u64(FN_COUNTER, total_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001853 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001854 }
William Dauchy0ef54392021-01-17 18:27:45 +01001855 case ST_F_COMP_IN:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001856 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001857 break;
1858 case ST_F_COMP_OUT:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001859 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001860 break;
1861 case ST_F_COMP_BYP:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001862 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001863 break;
1864 case ST_F_COMP_RSP:
1865 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1866 break;
1867 case ST_F_CONN_RATE:
1868 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1869 break;
1870 case ST_F_CONN_RATE_MAX:
1871 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1872 break;
1873 case ST_F_CONN_TOT:
1874 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1875 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001876 case ST_F_SESS_OTHER: {
1877 int i;
1878 uint64_t total_sess;
1879 size_t nb_sess =
1880 sizeof(px->fe_counters.cum_sess_ver) / sizeof(*px->fe_counters.cum_sess_ver);
1881
1882 total_sess = px->fe_counters.cum_sess;
1883 for (i = 0; i < nb_sess; i++)
1884 total_sess -= px->fe_counters.cum_sess_ver[i];
Frédéric Lécailled97d1d72023-02-06 09:23:56 +01001885 total_sess = (int64_t)total_sess < 0 ? 0 : total_sess;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001886 metric = mkf_u64(FN_COUNTER, total_sess);
1887 break;
1888 }
1889 case ST_F_H1SESS:
1890 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[0]);
1891 break;
1892 case ST_F_H2SESS:
1893 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[1]);
1894 break;
1895 case ST_F_H3SESS:
1896 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[2]);
1897 break;
1898 case ST_F_REQ_OTHER:
1899 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[0]);
1900 break;
1901 case ST_F_H1REQ:
1902 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[1]);
1903 break;
1904 case ST_F_H2REQ:
1905 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[2]);
1906 break;
1907 case ST_F_H3REQ:
1908 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[3]);
1909 break;
William Dauchy0ef54392021-01-17 18:27:45 +01001910 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001911 /* not used for frontends. If a specific metric
1912 * is requested, return an error. Otherwise continue.
1913 */
1914 if (selected_field != NULL)
1915 return 0;
1916 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001917 }
1918 stats[current_field] = metric;
1919 if (selected_field != NULL)
1920 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001921 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001922 return 1;
1923}
1924
Christopher Fauleta8b76842022-12-16 15:08:36 +01001925/* Dumps a frontend's line to the local trash buffer for the current proxy <px>
1926 * and uses the state from stream connector <sc>. The caller is responsible for
1927 * clearing the local trash buffer if needed. Returns non-zero if it emits
1928 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001929 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001930static int stats_dump_fe_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001931{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001932 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001933 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001934 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1935 struct stats_module *mod;
1936 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001937
1938 if (!(px->cap & PR_CAP_FE))
1939 return 0;
1940
Willy Tarreau91cefca2022-05-03 17:08:29 +02001941 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_FE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001942 return 0;
1943
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001944 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1945
William Dauchy0ef54392021-01-17 18:27:45 +01001946 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001947 return 0;
1948
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001949 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1950 void *counters;
1951
1952 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1953 stats_count += mod->stats_count;
1954 continue;
1955 }
1956
1957 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1958 mod->fill_stats(counters, stats + stats_count);
1959 stats_count += mod->stats_count;
1960 }
1961
1962 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001963}
1964
William Dauchy655e14e2021-02-14 23:22:54 +01001965/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1966 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1967 * this length is less then this value, the function returns 0, otherwise, it
1968 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1969 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001970 */
1971int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001972 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001973{
William Dauchy655e14e2021-02-14 23:22:54 +01001974 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001975 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001976
1977 if (len < ST_F_TOTAL_FIELDS)
1978 return 0;
1979
1980 if (!l->counters)
1981 return 0;
1982
1983 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001984
William Dauchy655e14e2021-02-14 23:22:54 +01001985 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1986 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001987
William Dauchy655e14e2021-02-14 23:22:54 +01001988 switch (current_field) {
1989 case ST_F_PXNAME:
1990 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1991 break;
1992 case ST_F_SVNAME:
1993 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1994 break;
1995 case ST_F_MODE:
1996 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1997 break;
1998 case ST_F_SCUR:
1999 metric = mkf_u32(0, l->nbconn);
2000 break;
2001 case ST_F_SMAX:
2002 metric = mkf_u32(FN_MAX, l->counters->conn_max);
2003 break;
2004 case ST_F_SLIM:
Willy Tarreau758c69d2023-01-12 18:59:37 +01002005 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->bind_conf->maxconn);
William Dauchy655e14e2021-02-14 23:22:54 +01002006 break;
2007 case ST_F_STOT:
2008 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
2009 break;
2010 case ST_F_BIN:
2011 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
2012 break;
2013 case ST_F_BOUT:
2014 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
2015 break;
2016 case ST_F_DREQ:
2017 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
2018 break;
2019 case ST_F_DRESP:
2020 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
2021 break;
2022 case ST_F_EREQ:
2023 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
2024 break;
2025 case ST_F_DCON:
2026 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
2027 break;
2028 case ST_F_DSES:
2029 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
2030 break;
2031 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01002032 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01002033 break;
2034 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002035 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01002036 break;
2037 case ST_F_IID:
2038 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2039 break;
2040 case ST_F_SID:
2041 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
2042 break;
2043 case ST_F_TYPE:
2044 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
2045 break;
2046 case ST_F_WREW:
2047 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
2048 break;
2049 case ST_F_EINT:
2050 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
2051 break;
2052 case ST_F_ADDR:
2053 if (flags & STAT_SHLGNDS) {
2054 char str[INET6_ADDRSTRLEN];
2055 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01002056
William Dauchy655e14e2021-02-14 23:22:54 +01002057 port = get_host_port(&l->rx.addr);
2058 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
2059 case AF_INET:
2060 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2061 chunk_appendf(out, "%s:%d", str, port);
2062 break;
2063 case AF_INET6:
2064 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2065 chunk_appendf(out, "[%s]:%d", str, port);
2066 break;
2067 case AF_UNIX:
2068 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2069 break;
2070 case -1:
2071 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2072 chunk_strcat(out, strerror(errno));
2073 break;
2074 default: /* address family not supported */
2075 break;
2076 }
2077 }
2078 break;
2079 default:
2080 /* not used for listen. If a specific metric
2081 * is requested, return an error. Otherwise continue.
2082 */
2083 if (selected_field != NULL)
2084 return 0;
2085 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002086 }
William Dauchy655e14e2021-02-14 23:22:54 +01002087 stats[current_field] = metric;
2088 if (selected_field != NULL)
2089 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002090 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002091 return 1;
2092}
2093
Christopher Fauleta8b76842022-12-16 15:08:36 +01002094/* Dumps a line for listener <l> and proxy <px> to the local trash buffer and
2095 * uses the state from stream connector <sc>. The caller is responsible for
2096 * clearing the local trash buffer if needed. Returns non-zero if it emits
2097 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002098 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002099static int stats_dump_li_stats(struct stconn *sc, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01002100{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002101 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002102 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002103 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2104 struct stats_module *mod;
2105 size_t stats_count = ST_F_TOTAL_FIELDS;
2106
2107 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002108
Willy Tarreau91cefca2022-05-03 17:08:29 +02002109 if (!stats_fill_li_stats(px, l, ctx->flags, stats,
William Dauchy655e14e2021-02-14 23:22:54 +01002110 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002111 return 0;
2112
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002113 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2114 void *counters;
2115
2116 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
2117 stats_count += mod->stats_count;
2118 continue;
2119 }
2120
2121 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2122 mod->fill_stats(counters, stats + stats_count);
2123 stats_count += mod->stats_count;
2124 }
2125
2126 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002127}
2128
2129enum srv_stats_state {
2130 SRV_STATS_STATE_DOWN = 0,
2131 SRV_STATS_STATE_DOWN_AGENT,
2132 SRV_STATS_STATE_GOING_UP,
2133 SRV_STATS_STATE_UP_GOING_DOWN,
2134 SRV_STATS_STATE_UP,
2135 SRV_STATS_STATE_NOLB_GOING_DOWN,
2136 SRV_STATS_STATE_NOLB,
2137 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2138 SRV_STATS_STATE_DRAIN,
2139 SRV_STATS_STATE_DRAIN_AGENT,
2140 SRV_STATS_STATE_NO_CHECK,
2141
2142 SRV_STATS_STATE_COUNT, /* Must be last */
2143};
2144
2145static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2146 [SRV_STATS_STATE_DOWN] = "DOWN",
2147 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2148 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2149 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2150 [SRV_STATS_STATE_UP] = "UP",
2151 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2152 [SRV_STATS_STATE_NOLB] = "NOLB",
2153 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2154 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2155 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2156 [SRV_STATS_STATE_NO_CHECK] = "no check"
2157};
2158
William Dauchyd3a9a492021-01-25 17:29:03 +01002159/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002160 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002161static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2162 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002163{
Emeric Brun52a91d32017-08-31 14:41:55 +02002164 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002165 if ((ref->check.state & CHK_ST_ENABLED) &&
2166 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002167 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002168 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002169 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002170 }
2171
Emeric Brun52a91d32017-08-31 14:41:55 +02002172 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002173 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002174 *state = SRV_STATS_STATE_DRAIN_AGENT;
2175 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2176 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002177 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002178 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002179 }
2180
William Dauchyd3a9a492021-01-25 17:29:03 +01002181 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2182 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002183 }
2184 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002185 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002186 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2187 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002188 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002189 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002190 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002191 }
2192 }
2193 else { /* stopped */
2194 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002195 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002196 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002197 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002198 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002199 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002200 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002201 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002202 }
2203 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002204}
William Lallemand74c24fb2016-11-21 17:18:36 +01002205
William Dauchyd3a9a492021-01-25 17:29:03 +01002206/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2207 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2208 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2209 * this value, or if the selected field is not implemented for servers, the
2210 * function returns 0, otherwise, it returns 1. <flags> can take the value
2211 * STAT_SHLGNDS.
2212 */
2213int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2214 struct field *stats, int len,
2215 enum stat_field *selected_field)
2216{
2217 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2218 struct server *via = sv->track ? sv->track : sv;
2219 struct server *ref = via;
2220 enum srv_stats_state state = 0;
2221 char str[INET6_ADDRSTRLEN];
2222 struct buffer *out = get_trash_chunk();
2223 char *fld_status;
2224 long long srv_samples_counter;
2225 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002226
William Dauchyd3a9a492021-01-25 17:29:03 +01002227 if (len < ST_F_TOTAL_FIELDS)
2228 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002229
William Dauchyd3a9a492021-01-25 17:29:03 +01002230 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002231
William Dauchyd3a9a492021-01-25 17:29:03 +01002232 /* compute state for later use */
2233 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2234 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2235 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2236 /* we have "via" which is the tracked server as described in the configuration,
2237 * and "ref" which is the checked server and the end of the chain.
2238 */
2239 while (ref->track)
2240 ref = ref->track;
2241 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002242 }
2243
William Dauchyd3a9a492021-01-25 17:29:03 +01002244 /* compue time values for later use */
2245 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2246 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2247 *selected_field == ST_F_TTIME) {
2248 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2249 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2250 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002251 }
2252
William Dauchyd3a9a492021-01-25 17:29:03 +01002253 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2254 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002255
William Dauchyd3a9a492021-01-25 17:29:03 +01002256 switch (current_field) {
2257 case ST_F_PXNAME:
2258 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2259 break;
2260 case ST_F_SVNAME:
2261 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2262 break;
2263 case ST_F_MODE:
2264 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002265 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002266 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002267 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002268 break;
2269 case ST_F_QMAX:
2270 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2271 break;
2272 case ST_F_SCUR:
2273 metric = mkf_u32(0, sv->cur_sess);
2274 break;
2275 case ST_F_SMAX:
2276 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2277 break;
2278 case ST_F_SLIM:
2279 if (sv->maxconn)
2280 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2281 break;
2282 case ST_F_SRV_ICUR:
2283 metric = mkf_u32(0, sv->curr_idle_conns);
2284 break;
2285 case ST_F_SRV_ILIM:
2286 if (sv->max_idle_conns != -1)
2287 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2288 break;
2289 case ST_F_STOT:
2290 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2291 break;
2292 case ST_F_BIN:
2293 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2294 break;
2295 case ST_F_BOUT:
2296 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2297 break;
2298 case ST_F_DRESP:
2299 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2300 break;
2301 case ST_F_ECON:
2302 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2303 break;
2304 case ST_F_ERESP:
2305 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2306 break;
2307 case ST_F_WRETR:
2308 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2309 break;
2310 case ST_F_WREDIS:
2311 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2312 break;
2313 case ST_F_WREW:
2314 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2315 break;
2316 case ST_F_EINT:
2317 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2318 break;
2319 case ST_F_CONNECT:
2320 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2321 break;
2322 case ST_F_REUSE:
2323 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2324 break;
2325 case ST_F_IDLE_CONN_CUR:
2326 metric = mkf_u32(0, sv->curr_idle_nb);
2327 break;
2328 case ST_F_SAFE_CONN_CUR:
2329 metric = mkf_u32(0, sv->curr_safe_nb);
2330 break;
2331 case ST_F_USED_CONN_CUR:
2332 metric = mkf_u32(0, sv->curr_used_conns);
2333 break;
2334 case ST_F_NEED_CONN_EST:
2335 metric = mkf_u32(0, sv->est_need_conns);
2336 break;
2337 case ST_F_STATUS:
2338 fld_status = chunk_newstr(out);
2339 if (sv->cur_admin & SRV_ADMF_RMAINT)
2340 chunk_appendf(out, "MAINT (resolution)");
2341 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2342 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2343 else if (sv->cur_admin & SRV_ADMF_MAINT)
2344 chunk_appendf(out, "MAINT");
2345 else
2346 chunk_appendf(out,
2347 srv_hlt_st[state],
2348 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2349 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002350
William Dauchyd3a9a492021-01-25 17:29:03 +01002351 metric = mkf_str(FO_STATUS, fld_status);
2352 break;
2353 case ST_F_LASTCHG:
Willy Tarreau69530f52023-04-28 09:16:15 +02002354 metric = mkf_u32(FN_AGE, ns_to_sec(now_ns) - sv->last_change);
William Dauchyd3a9a492021-01-25 17:29:03 +01002355 break;
2356 case ST_F_WEIGHT:
2357 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2358 break;
2359 case ST_F_UWEIGHT:
2360 metric = mkf_u32(FN_AVG, sv->uweight);
2361 break;
2362 case ST_F_ACT:
2363 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2364 break;
2365 case ST_F_BCK:
2366 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2367 break;
2368 case ST_F_CHKFAIL:
2369 if (sv->check.state & CHK_ST_ENABLED)
2370 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2371 break;
2372 case ST_F_CHKDOWN:
2373 if (sv->check.state & CHK_ST_ENABLED)
2374 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2375 break;
2376 case ST_F_DOWNTIME:
2377 if (sv->check.state & CHK_ST_ENABLED)
2378 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2379 break;
2380 case ST_F_QLIMIT:
2381 if (sv->maxqueue)
2382 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2383 break;
2384 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002385 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002386 break;
2387 case ST_F_IID:
2388 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2389 break;
2390 case ST_F_SID:
2391 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2392 break;
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01002393 case ST_F_SRID:
2394 metric = mkf_u32(FN_COUNTER, sv->rid);
2395 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002396 case ST_F_THROTTLE:
2397 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2398 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2399 break;
2400 case ST_F_LBTOT:
2401 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2402 break;
2403 case ST_F_TRACKED:
2404 if (sv->track) {
2405 char *fld_track = chunk_newstr(out);
2406 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2407 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2408 }
2409 break;
2410 case ST_F_TYPE:
2411 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2412 break;
2413 case ST_F_RATE:
2414 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2415 break;
2416 case ST_F_RATE_MAX:
2417 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2418 break;
2419 case ST_F_CHECK_STATUS:
2420 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2421 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002422
William Dauchyd3a9a492021-01-25 17:29:03 +01002423 fld_chksts = chunk_newstr(out);
2424 chunk_strcat(out, "* "); // for check in progress
2425 chunk_strcat(out, get_check_status_info(sv->check.status));
2426 if (!(sv->check.state & CHK_ST_INPROGRESS))
2427 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002428 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002429 }
2430 break;
2431 case ST_F_CHECK_CODE:
2432 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2433 sv->check.status >= HCHK_STATUS_L57DATA)
2434 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2435 break;
2436 case ST_F_CHECK_DURATION:
2437 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2438 sv->check.status >= HCHK_STATUS_CHECKED)
Aurelien DARRAGONb118f2f2022-12-07 14:52:10 +01002439 metric = mkf_u64(FN_DURATION, MAX(sv->check.duration, 0));
William Dauchyd3a9a492021-01-25 17:29:03 +01002440 break;
2441 case ST_F_CHECK_DESC:
2442 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2443 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2444 break;
2445 case ST_F_LAST_CHK:
2446 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2447 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2448 break;
2449 case ST_F_CHECK_RISE:
2450 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2451 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2452 break;
2453 case ST_F_CHECK_FALL:
2454 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2455 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2456 break;
2457 case ST_F_CHECK_HEALTH:
2458 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2459 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2460 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002461 case ST_F_AGENT_STATUS:
2462 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2463 const char *fld_chksts;
2464
2465 fld_chksts = chunk_newstr(out);
2466 chunk_strcat(out, "* "); // for check in progress
2467 chunk_strcat(out, get_check_status_info(sv->agent.status));
2468 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2469 fld_chksts += 2; // skip "* "
2470 metric = mkf_str(FN_OUTPUT, fld_chksts);
2471 }
2472 break;
2473 case ST_F_AGENT_CODE:
2474 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2475 (sv->agent.status >= HCHK_STATUS_L57DATA))
2476 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2477 break;
2478 case ST_F_AGENT_DURATION:
2479 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2480 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2481 break;
2482 case ST_F_AGENT_DESC:
2483 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2484 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2485 break;
2486 case ST_F_LAST_AGT:
2487 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2488 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2489 break;
2490 case ST_F_AGENT_RISE:
2491 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2492 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2493 break;
2494 case ST_F_AGENT_FALL:
2495 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2496 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2497 break;
2498 case ST_F_AGENT_HEALTH:
2499 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2500 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2501 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002502 case ST_F_REQ_TOT:
2503 if (px->mode == PR_MODE_HTTP)
2504 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2505 break;
2506 case ST_F_HRSP_1XX:
2507 if (px->mode == PR_MODE_HTTP)
2508 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2509 break;
2510 case ST_F_HRSP_2XX:
2511 if (px->mode == PR_MODE_HTTP)
2512 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2513 break;
2514 case ST_F_HRSP_3XX:
2515 if (px->mode == PR_MODE_HTTP)
2516 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2517 break;
2518 case ST_F_HRSP_4XX:
2519 if (px->mode == PR_MODE_HTTP)
2520 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2521 break;
2522 case ST_F_HRSP_5XX:
2523 if (px->mode == PR_MODE_HTTP)
2524 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2525 break;
2526 case ST_F_HRSP_OTHER:
2527 if (px->mode == PR_MODE_HTTP)
2528 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2529 break;
2530 case ST_F_HANAFAIL:
2531 if (ref->observe)
2532 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2533 break;
2534 case ST_F_CLI_ABRT:
2535 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2536 break;
2537 case ST_F_SRV_ABRT:
2538 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2539 break;
2540 case ST_F_LASTSESS:
2541 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2542 break;
2543 case ST_F_QTIME:
2544 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2545 break;
2546 case ST_F_CTIME:
2547 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2548 break;
2549 case ST_F_RTIME:
2550 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2551 break;
2552 case ST_F_TTIME:
2553 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2554 break;
2555 case ST_F_QT_MAX:
2556 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2557 break;
2558 case ST_F_CT_MAX:
2559 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2560 break;
2561 case ST_F_RT_MAX:
2562 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2563 break;
2564 case ST_F_TT_MAX:
2565 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2566 break;
2567 case ST_F_ADDR:
2568 if (flags & STAT_SHLGNDS) {
2569 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2570 case AF_INET:
2571 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2572 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2573 break;
2574 case AF_INET6:
2575 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2576 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2577 break;
2578 case AF_UNIX:
2579 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2580 break;
2581 case -1:
2582 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2583 chunk_strcat(out, strerror(errno));
2584 break;
2585 default: /* address family not supported */
2586 break;
2587 }
2588 }
2589 break;
2590 case ST_F_COOKIE:
2591 if (flags & STAT_SHLGNDS && sv->cookie)
2592 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2593 break;
2594 default:
2595 /* not used for servers. If a specific metric
2596 * is requested, return an error. Otherwise continue.
2597 */
2598 if (selected_field != NULL)
2599 return 0;
2600 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002601 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002602 stats[current_field] = metric;
2603 if (selected_field != NULL)
2604 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002605 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002606 return 1;
2607}
2608
Christopher Fauleta8b76842022-12-16 15:08:36 +01002609/* Dumps a line for server <sv> and proxy <px> to the local trash vbuffer and
2610 * uses the state from stream connector <sc>, and server state <state>. The
2611 * caller is responsible for clearing the local trash buffer if needed. Returns
2612 * non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002613 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002614static int stats_dump_sv_stats(struct stconn *sc, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002615{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002616 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002617 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002618 struct stats_module *mod;
2619 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2620 size_t stats_count = ST_F_TOTAL_FIELDS;
2621
2622 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002623
Willy Tarreau91cefca2022-05-03 17:08:29 +02002624 if (!stats_fill_sv_stats(px, sv, ctx->flags, stats,
William Dauchyd3a9a492021-01-25 17:29:03 +01002625 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002626 return 0;
2627
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002628 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2629 void *counters;
2630
2631 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2632 continue;
2633
2634 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2635 stats_count += mod->stats_count;
2636 continue;
2637 }
2638
2639 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2640 mod->fill_stats(counters, stats + stats_count);
2641 stats_count += mod->stats_count;
2642 }
2643
2644 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002645}
2646
William Dauchyda3b4662021-01-25 17:29:01 +01002647/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002648 */
William Dauchyda3b4662021-01-25 17:29:01 +01002649static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002650{
William Dauchyda3b4662021-01-25 17:29:01 +01002651 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002652 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002653
William Dauchyda3b4662021-01-25 17:29:01 +01002654 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002655 for (srv = px->srv; srv; srv = srv->next) {
2656 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002657 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002658 if (srv_currently_usable(srv) &&
2659 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002660 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002661 }
William Dauchyda3b4662021-01-25 17:29:01 +01002662 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002663 }
2664
Willy Tarreaubd715102020-10-23 22:44:30 +02002665 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2666 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002667 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002668 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2669
William Dauchyda3b4662021-01-25 17:29:01 +01002670 /* use tmp variable then assign result to make gcc happy */
2671 *nbup = nbup_tmp;
2672 *nbsrv = nbsrv_tmp;
2673 *totuw = totuw_tmp;
2674}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002675
William Dauchyda3b4662021-01-25 17:29:01 +01002676/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2677 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2678 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2679 * this value, or if the selected field is not implemented for backends, the
2680 * function returns 0, otherwise, it returns 1. <flags> can take the value
2681 * STAT_SHLGNDS.
2682 */
2683int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2684 enum stat_field *selected_field)
2685{
2686 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2687 long long be_samples_counter;
2688 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2689 struct buffer *out = get_trash_chunk();
2690 int nbup, nbsrv, totuw;
2691 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002692
William Dauchyda3b4662021-01-25 17:29:01 +01002693 if (len < ST_F_TOTAL_FIELDS)
2694 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002695
William Dauchyda3b4662021-01-25 17:29:01 +01002696 nbup = nbsrv = totuw = 0;
2697 /* some srv values compute for later if we either select all fields or
2698 * need them for one of the mentioned ones */
2699 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2700 *selected_field == ST_F_UWEIGHT)
2701 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002702
William Dauchyda3b4662021-01-25 17:29:01 +01002703 /* same here but specific to time fields */
2704 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2705 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2706 *selected_field == ST_F_TTIME) {
2707 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2708 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2709 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002710 }
2711
William Dauchyda3b4662021-01-25 17:29:01 +01002712 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2713 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002714
William Dauchyda3b4662021-01-25 17:29:01 +01002715 switch (current_field) {
2716 case ST_F_PXNAME:
2717 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2718 break;
2719 case ST_F_SVNAME:
2720 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2721 break;
2722 case ST_F_MODE:
2723 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2724 break;
2725 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002726 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002727 break;
2728 case ST_F_QMAX:
2729 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2730 break;
2731 case ST_F_SCUR:
2732 metric = mkf_u32(0, px->beconn);
2733 break;
2734 case ST_F_SMAX:
2735 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2736 break;
2737 case ST_F_SLIM:
2738 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2739 break;
2740 case ST_F_STOT:
2741 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2742 break;
2743 case ST_F_BIN:
2744 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2745 break;
2746 case ST_F_BOUT:
2747 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2748 break;
2749 case ST_F_DREQ:
2750 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2751 break;
2752 case ST_F_DRESP:
2753 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2754 break;
2755 case ST_F_ECON:
2756 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2757 break;
2758 case ST_F_ERESP:
2759 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2760 break;
2761 case ST_F_WRETR:
2762 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2763 break;
2764 case ST_F_WREDIS:
2765 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2766 break;
2767 case ST_F_WREW:
2768 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2769 break;
2770 case ST_F_EINT:
2771 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2772 break;
2773 case ST_F_CONNECT:
2774 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2775 break;
2776 case ST_F_REUSE:
2777 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2778 break;
2779 case ST_F_STATUS:
2780 fld = chunk_newstr(out);
2781 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2782 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2783 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2784 metric = mkf_str(FO_STATUS, fld);
2785 break;
Cedric Paillet7d6644e2022-12-08 09:17:00 +00002786 case ST_F_AGG_SRV_CHECK_STATUS: // DEPRECATED
2787 case ST_F_AGG_SRV_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +01002788 metric = mkf_u32(FN_GAUGE, 0);
2789 break;
Cedric Paillete06e31e2022-12-08 09:17:01 +00002790 case ST_F_AGG_CHECK_STATUS:
2791 metric = mkf_u32(FN_GAUGE, 0);
2792 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002793 case ST_F_WEIGHT:
2794 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2795 break;
2796 case ST_F_UWEIGHT:
2797 metric = mkf_u32(FN_AVG, totuw);
2798 break;
2799 case ST_F_ACT:
2800 metric = mkf_u32(0, px->srv_act);
2801 break;
2802 case ST_F_BCK:
2803 metric = mkf_u32(0, px->srv_bck);
2804 break;
2805 case ST_F_CHKDOWN:
2806 metric = mkf_u64(FN_COUNTER, px->down_trans);
2807 break;
2808 case ST_F_LASTCHG:
Willy Tarreau69530f52023-04-28 09:16:15 +02002809 metric = mkf_u32(FN_AGE, ns_to_sec(now_ns) - px->last_change);
William Dauchyda3b4662021-01-25 17:29:01 +01002810 break;
2811 case ST_F_DOWNTIME:
2812 if (px->srv)
2813 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2814 break;
2815 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002816 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002817 break;
2818 case ST_F_IID:
2819 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2820 break;
2821 case ST_F_SID:
2822 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2823 break;
2824 case ST_F_LBTOT:
2825 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2826 break;
2827 case ST_F_TYPE:
2828 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2829 break;
2830 case ST_F_RATE:
2831 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2832 break;
2833 case ST_F_RATE_MAX:
2834 metric = mkf_u32(0, px->be_counters.sps_max);
2835 break;
2836 case ST_F_COOKIE:
2837 if (flags & STAT_SHLGNDS && px->cookie_name)
2838 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2839 break;
2840 case ST_F_ALGO:
2841 if (flags & STAT_SHLGNDS)
2842 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2843 break;
2844 case ST_F_REQ_TOT:
2845 if (px->mode == PR_MODE_HTTP)
2846 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2847 break;
2848 case ST_F_HRSP_1XX:
2849 if (px->mode == PR_MODE_HTTP)
2850 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2851 break;
2852 case ST_F_HRSP_2XX:
2853 if (px->mode == PR_MODE_HTTP)
2854 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2855 break;
2856 case ST_F_HRSP_3XX:
2857 if (px->mode == PR_MODE_HTTP)
2858 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2859 break;
2860 case ST_F_HRSP_4XX:
2861 if (px->mode == PR_MODE_HTTP)
2862 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2863 break;
2864 case ST_F_HRSP_5XX:
2865 if (px->mode == PR_MODE_HTTP)
2866 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2867 break;
2868 case ST_F_HRSP_OTHER:
2869 if (px->mode == PR_MODE_HTTP)
2870 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2871 break;
2872 case ST_F_CACHE_LOOKUPS:
2873 if (px->mode == PR_MODE_HTTP)
2874 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2875 break;
2876 case ST_F_CACHE_HITS:
2877 if (px->mode == PR_MODE_HTTP)
2878 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2879 break;
2880 case ST_F_CLI_ABRT:
2881 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2882 break;
2883 case ST_F_SRV_ABRT:
2884 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2885 break;
2886 case ST_F_COMP_IN:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002887 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002888 break;
2889 case ST_F_COMP_OUT:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002890 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002891 break;
2892 case ST_F_COMP_BYP:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002893 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002894 break;
2895 case ST_F_COMP_RSP:
2896 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2897 break;
2898 case ST_F_LASTSESS:
2899 metric = mkf_s32(FN_AGE, be_lastsession(px));
2900 break;
2901 case ST_F_QTIME:
2902 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2903 break;
2904 case ST_F_CTIME:
2905 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2906 break;
2907 case ST_F_RTIME:
2908 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2909 break;
2910 case ST_F_TTIME:
2911 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2912 break;
2913 case ST_F_QT_MAX:
2914 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2915 break;
2916 case ST_F_CT_MAX:
2917 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2918 break;
2919 case ST_F_RT_MAX:
2920 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2921 break;
2922 case ST_F_TT_MAX:
2923 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2924 break;
2925 default:
2926 /* not used for backends. If a specific metric
2927 * is requested, return an error. Otherwise continue.
2928 */
2929 if (selected_field != NULL)
2930 return 0;
2931 continue;
2932 }
2933 stats[current_field] = metric;
2934 if (selected_field != NULL)
2935 break;
2936 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002937 return 1;
2938}
2939
Christopher Fauleta8b76842022-12-16 15:08:36 +01002940/* Dumps a line for backend <px> to the local trash buffer for and uses the
2941 * state from stream interface <si>. The caller is responsible for clearing the
2942 * local trash buffer if needed. Returns non-zero if it emits anything, zero
2943 * otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002944 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002945static int stats_dump_be_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002946{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002947 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002948 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002949 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2950 struct stats_module *mod;
2951 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002952
2953 if (!(px->cap & PR_CAP_BE))
2954 return 0;
2955
Willy Tarreau91cefca2022-05-03 17:08:29 +02002956 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_BE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01002957 return 0;
2958
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002959 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2960
Willy Tarreau91cefca2022-05-03 17:08:29 +02002961 if (!stats_fill_be_stats(px, ctx->flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002962 return 0;
2963
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002964 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2965 struct extra_counters *counters;
2966
2967 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2968 continue;
2969
2970 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2971 stats_count += mod->stats_count;
2972 continue;
2973 }
2974
2975 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2976 mod->fill_stats(counters, stats + stats_count);
2977 stats_count += mod->stats_count;
2978 }
2979
2980 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002981}
2982
Christopher Fauleta8b76842022-12-16 15:08:36 +01002983/* Dumps the HTML table header for proxy <px> to the local trash buffer for and
2984 * uses the state from stream connector <sc> and per-uri parameters <uri>. The
2985 * caller is responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01002986 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002987static void stats_dump_html_px_hdr(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002988{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002989 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002990 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01002991 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002992 struct stats_module *mod;
2993 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002994
Willy Tarreau91cefca2022-05-03 17:08:29 +02002995 if (px->cap & PR_CAP_BE && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002996 /* A form to enable/disable this proxy servers */
2997
Willy Tarreau91cefca2022-05-03 17:08:29 +02002998 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002999 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003000 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003001 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003002
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003003 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02003004 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3005 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003006 }
3007
Christopher Fauleta8b76842022-12-16 15:08:36 +01003008 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003009 "<form method=\"post\">");
3010 }
3011
3012 /* print a new table */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003013 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003014 "<table class=\"tbl\" width=\"100%%\">\n"
3015 "<tr class=\"titre\">"
3016 "<th class=\"pxname\" width=\"10%%\">");
3017
Christopher Fauleta8b76842022-12-16 15:08:36 +01003018 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003019 "<a name=\"%s\"></a>%s"
3020 "<a class=px href=\"#%s\">%s</a>",
3021 px->id,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003022 (ctx->flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01003023 px->id, px->id);
3024
Willy Tarreau91cefca2022-05-03 17:08:29 +02003025 if (ctx->flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003026 /* cap, mode, id */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003027 chunk_appendf(&trash_chunk, "<div class=tips>cap: %s, mode: %s, id: %d",
William Lallemand74c24fb2016-11-21 17:18:36 +01003028 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3029 px->uuid);
Christopher Fauleta8b76842022-12-16 15:08:36 +01003030 chunk_appendf(&trash_chunk, "</div>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003031 }
3032
Christopher Fauleta8b76842022-12-16 15:08:36 +01003033 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003034 "%s</th>"
3035 "<th class=\"%s\" width=\"90%%\">%s</th>"
3036 "</tr>\n"
3037 "</table>\n"
3038 "<table class=\"tbl\" width=\"100%%\">\n"
3039 "<tr class=\"titre\">",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003040 (ctx->flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01003041 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3042
Willy Tarreau91cefca2022-05-03 17:08:29 +02003043 if (ctx->flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003044 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01003045 if ((px->cap & PR_CAP_BE) && px->srv)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003046 chunk_appendf(&trash_chunk,
Christopher Fauletbc271ec2019-12-02 11:29:04 +01003047 "<th rowspan=2 width=1><input type=\"checkbox\" "
3048 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
3049 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
3050 px->id,
3051 px->id);
3052 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003053 chunk_appendf(&trash_chunk, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003054 }
3055
Christopher Fauleta8b76842022-12-16 15:08:36 +01003056 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003057 "<th rowspan=2></th>"
3058 "<th colspan=3>Queue</th>"
3059 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
3060 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3061 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003062 "<th colspan=9>Server</th>");
3063
Willy Tarreau91cefca2022-05-03 17:08:29 +02003064 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003065 // calculate the count of module for colspan attribute
3066 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
3067 ++stats_module_len;
3068 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003069 chunk_appendf(&trash_chunk, "<th colspan=%d>Extra modules</th>",
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003070 stats_module_len);
3071 }
3072
Christopher Fauleta8b76842022-12-16 15:08:36 +01003073 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003074 "</tr>\n"
3075 "<tr class=\"titre\">"
3076 "<th>Cur</th><th>Max</th><th>Limit</th>"
3077 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
3078 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
3079 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3080 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3081 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3082 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003083 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003084
Willy Tarreau91cefca2022-05-03 17:08:29 +02003085 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003086 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Christopher Fauleta8b76842022-12-16 15:08:36 +01003087 chunk_appendf(&trash_chunk, "<th>%s</th>", mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003088 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003089 }
3090
Christopher Fauleta8b76842022-12-16 15:08:36 +01003091 chunk_appendf(&trash_chunk, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003092}
3093
Christopher Fauleta8b76842022-12-16 15:08:36 +01003094/* Dumps the HTML table trailer for proxy <px> to the local trash buffer for and
3095 * uses the state from stream connector <sc>. The caller is responsible for
3096 * clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003097 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003098static void stats_dump_html_px_end(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01003099{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003100 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003101 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet693b23b2022-02-28 09:09:05 +01003102
Christopher Fauleta8b76842022-12-16 15:08:36 +01003103 chunk_appendf(&trash_chunk, "</table>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003104
Willy Tarreau91cefca2022-05-03 17:08:29 +02003105 if ((px->cap & PR_CAP_BE) && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003106 /* close the form used to enable/disable this proxy servers */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003107 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003108 "Choose the action to perform on the checked servers : "
3109 "<select name=action>"
3110 "<option value=\"\"></option>"
3111 "<option value=\"ready\">Set state to READY</option>"
3112 "<option value=\"drain\">Set state to DRAIN</option>"
3113 "<option value=\"maint\">Set state to MAINT</option>"
3114 "<option value=\"dhlth\">Health: disable checks</option>"
3115 "<option value=\"ehlth\">Health: enable checks</option>"
3116 "<option value=\"hrunn\">Health: force UP</option>"
3117 "<option value=\"hnolb\">Health: force NOLB</option>"
3118 "<option value=\"hdown\">Health: force DOWN</option>"
3119 "<option value=\"dagent\">Agent: disable checks</option>"
3120 "<option value=\"eagent\">Agent: enable checks</option>"
3121 "<option value=\"arunn\">Agent: force UP</option>"
3122 "<option value=\"adown\">Agent: force DOWN</option>"
3123 "<option value=\"shutdown\">Kill Sessions</option>"
3124 "</select>"
3125 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3126 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3127 "</form>",
3128 px->uuid);
3129 }
3130
Christopher Fauleta8b76842022-12-16 15:08:36 +01003131 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003132}
3133
3134/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003135 * Dumps statistics for a proxy. The output is sent to the stream connector's
William Lallemand74c24fb2016-11-21 17:18:36 +01003136 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3137 * buffer space, or non-zero if everything completed. This function is used
3138 * both by the CLI and the HTTP entry points, and is able to dump the output
3139 * in HTML or CSV formats. If the later, <uri> must be NULL.
3140 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003141int stats_dump_proxy_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003142 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003143{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003144 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003145 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003146 struct stream *s = __sc_strm(sc);
3147 struct channel *rep = sc_ic(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +01003148 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3149 struct listener *l;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003150 int current_field;
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003151 int px_st = ctx->px_st;
William Lallemand74c24fb2016-11-21 17:18:36 +01003152
Christopher Fauleta8b76842022-12-16 15:08:36 +01003153 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003154more:
3155 current_field = ctx->field;
William Lallemand74c24fb2016-11-21 17:18:36 +01003156
Willy Tarreau91cefca2022-05-03 17:08:29 +02003157 switch (ctx->px_st) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003158 case STAT_PX_ST_INIT:
3159 /* we are on a new proxy */
3160 if (uri && uri->scope) {
3161 /* we have a limited scope, we have to check the proxy name */
3162 struct stat_scope *scope;
3163 int len;
3164
3165 len = strlen(px->id);
3166 scope = uri->scope;
3167
3168 while (scope) {
3169 /* match exact proxy name */
3170 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3171 break;
3172
3173 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003174 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003175 break;
3176 scope = scope->next;
3177 }
3178
3179 /* proxy name not found : don't dump anything */
3180 if (scope == NULL)
3181 return 1;
3182 }
3183
3184 /* if the user has requested a limited output and the proxy
3185 * name does not match, skip it.
3186 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003187 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003188 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003189
Willy Tarreau91cefca2022-05-03 17:08:29 +02003190 if (strnistr(px->id, strlen(px->id), scope_ptr, ctx->scope_len) == NULL)
Christopher Fauleted7a0662019-01-14 11:07:34 +01003191 return 1;
3192 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003193
Willy Tarreau91cefca2022-05-03 17:08:29 +02003194 if ((ctx->flags & STAT_BOUND) &&
3195 (ctx->iid != -1) &&
3196 (px->uuid != ctx->iid))
William Lallemand74c24fb2016-11-21 17:18:36 +01003197 return 1;
3198
Willy Tarreau91cefca2022-05-03 17:08:29 +02003199 ctx->px_st = STAT_PX_ST_TH;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003200 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003201
3202 case STAT_PX_ST_TH:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003203 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003204 stats_dump_html_px_hdr(sc, px);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003205 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003206 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003207 }
3208
Willy Tarreau91cefca2022-05-03 17:08:29 +02003209 ctx->px_st = STAT_PX_ST_FE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003210 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003211
3212 case STAT_PX_ST_FE:
3213 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003214 if (stats_dump_fe_stats(sc, px)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003215 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003216 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003217 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003218 if (ctx->field)
3219 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003220 }
3221
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003222 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003223 ctx->obj2 = px->conf.listeners.n;
3224 ctx->px_st = STAT_PX_ST_LI;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003225 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003226
3227 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003228 /* obj2 points to listeners list as initialized above */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003229 for (; ctx->obj2 != &px->conf.listeners; ctx->obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003230 if (htx) {
Christopher Fauleta236c582023-05-09 16:31:01 +02003231 if (htx_almost_full(htx)) {
3232 sc_need_room(sc, htx->size / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003233 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003234 }
Christopher Fauletef779222018-10-31 08:47:01 +01003235 }
3236 else {
Christopher Fauleta236c582023-05-09 16:31:01 +02003237 if (buffer_almost_full(&rep->buf)) {
3238 sc_need_room(sc, b_size(&rep->buf) / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003239 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003240 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003241 }
3242
Willy Tarreau91cefca2022-05-03 17:08:29 +02003243 l = LIST_ELEM(ctx->obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003244 if (!l->counters)
3245 continue;
3246
Willy Tarreau91cefca2022-05-03 17:08:29 +02003247 if (ctx->flags & STAT_BOUND) {
3248 if (!(ctx->type & (1 << STATS_TYPE_SO)))
William Lallemand74c24fb2016-11-21 17:18:36 +01003249 break;
3250
Willy Tarreau91cefca2022-05-03 17:08:29 +02003251 if (ctx->sid != -1 && l->luid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003252 continue;
3253 }
3254
3255 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003256 if (stats_dump_li_stats(sc, px, l)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003257 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003258 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003259 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003260 if (ctx->field)
3261 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003262 }
Aurelien DARRAGON9b07d4f2023-02-03 11:43:05 +01003263 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003264 }
3265
Willy Tarreau91cefca2022-05-03 17:08:29 +02003266 ctx->obj2 = px->srv; /* may be NULL */
3267 ctx->px_st = STAT_PX_ST_SV;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003268 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003269
3270 case STAT_PX_ST_SV:
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003271 /* check for dump resumption */
3272 if (px_st == STAT_PX_ST_SV) {
3273 struct server *cur = ctx->obj2;
3274
3275 /* re-entrant dump */
3276 BUG_ON(!cur);
3277 if (cur->flags & SRV_F_DELETED) {
3278 /* the server could have been marked as deleted
3279 * between two dumping attempts, skip it.
3280 */
3281 cur = cur->next;
3282 }
3283 srv_drop(ctx->obj2); /* drop old srv taken on last dumping attempt */
3284 ctx->obj2 = cur; /* could be NULL */
3285 /* back to normal */
3286 }
3287
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003288 /* obj2 points to servers list as initialized above.
3289 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003290 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003291 * Temporarily increment its refcount to prevent its
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003292 * anticipated cleaning. Call srv_drop() to release it.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003293 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003294 for (; ctx->obj2 != NULL;
3295 ctx->obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003296
Willy Tarreau91cefca2022-05-03 17:08:29 +02003297 sv = ctx->obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003298 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003299
Christopher Fauletef779222018-10-31 08:47:01 +01003300 if (htx) {
Christopher Fauleta236c582023-05-09 16:31:01 +02003301 if (htx_almost_full(htx)) {
3302 sc_need_room(sc, htx->size / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003303 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003304 }
Christopher Fauletef779222018-10-31 08:47:01 +01003305 }
3306 else {
Christopher Fauleta236c582023-05-09 16:31:01 +02003307 if (buffer_almost_full(&rep->buf)) {
3308 sc_need_room(sc, b_size(&rep->buf) / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003309 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003310 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003311 }
3312
Willy Tarreau91cefca2022-05-03 17:08:29 +02003313 if (ctx->flags & STAT_BOUND) {
3314 if (!(ctx->type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003315 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003316 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003317 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003318
Willy Tarreau91cefca2022-05-03 17:08:29 +02003319 if (ctx->sid != -1 && sv->puid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003320 continue;
3321 }
3322
Willy Tarreau3e320362020-10-23 17:28:57 +02003323 /* do not report disabled servers */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003324 if (ctx->flags & STAT_HIDE_MAINT &&
Willy Tarreau3e320362020-10-23 17:28:57 +02003325 sv->cur_admin & SRV_ADMF_MAINT) {
3326 continue;
3327 }
3328
William Lallemand74c24fb2016-11-21 17:18:36 +01003329 svs = sv;
3330 while (svs->track)
3331 svs = svs->track;
3332
3333 /* do not report servers which are DOWN and not changing state */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003334 if ((ctx->flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003335 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3336 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003337 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3338 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3339 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3340 continue;
3341 }
3342
Willy Tarreaucaff6312022-05-27 10:17:46 +02003343 if (stats_dump_sv_stats(sc, px, sv)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003344 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003345 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003346 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003347 if (ctx->field)
3348 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003349 }
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003350 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003351 } /* for sv */
3352
Willy Tarreau91cefca2022-05-03 17:08:29 +02003353 ctx->px_st = STAT_PX_ST_BE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003354 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003355
3356 case STAT_PX_ST_BE:
3357 /* print the backend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003358 if (stats_dump_be_stats(sc, px)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003359 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003360 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003361 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003362 if (ctx->field)
3363 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003364 }
3365
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003366 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003367 ctx->px_st = STAT_PX_ST_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003368 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003369
3370 case STAT_PX_ST_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003371 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003372 stats_dump_html_px_end(sc, px);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003373 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003374 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003375 }
3376
Willy Tarreau91cefca2022-05-03 17:08:29 +02003377 ctx->px_st = STAT_PX_ST_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003378 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003379
3380 case STAT_PX_ST_FIN:
3381 return 1;
3382
3383 default:
3384 /* unknown state, we should put an abort() here ! */
3385 return 1;
3386 }
Christopher Fauletef779222018-10-31 08:47:01 +01003387
3388 full:
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003389 /* restore previous field */
3390 ctx->field = current_field;
Christopher Fauletef779222018-10-31 08:47:01 +01003391 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003392}
3393
Christopher Fauleta8b76842022-12-16 15:08:36 +01003394/* Dumps the HTTP stats head block to the local trash buffer for and uses the
3395 * per-uri parameters <uri>. The caller is responsible for clearing the local
3396 * trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003397 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003398static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003399{
Willy Tarreau91cefca2022-05-03 17:08:29 +02003400 struct show_stat_ctx *ctx = appctx->svcctx;
3401
William Lallemand74c24fb2016-11-21 17:18:36 +01003402 /* WARNING! This must fit in the first buffer !!! */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003403 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003404 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3405 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3406 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003407 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003408 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3409 "<style type=\"text/css\"><!--\n"
3410 "body {"
3411 " font-family: arial, helvetica, sans-serif;"
3412 " font-size: 12px;"
3413 " font-weight: normal;"
3414 " color: black;"
3415 " background: white;"
3416 "}\n"
3417 "th,td {"
3418 " font-size: 10px;"
3419 "}\n"
3420 "h1 {"
3421 " font-size: x-large;"
3422 " margin-bottom: 0.5em;"
3423 "}\n"
3424 "h2 {"
3425 " font-family: helvetica, arial;"
3426 " font-size: x-large;"
3427 " font-weight: bold;"
3428 " font-style: italic;"
3429 " color: #6020a0;"
3430 " margin-top: 0em;"
3431 " margin-bottom: 0em;"
3432 "}\n"
3433 "h3 {"
3434 " font-family: helvetica, arial;"
3435 " font-size: 16px;"
3436 " font-weight: bold;"
3437 " color: #b00040;"
3438 " background: #e8e8d0;"
3439 " margin-top: 0em;"
3440 " margin-bottom: 0em;"
3441 "}\n"
3442 "li {"
3443 " margin-top: 0.25em;"
3444 " margin-right: 2em;"
3445 "}\n"
3446 ".hr {margin-top: 0.25em;"
3447 " border-color: black;"
3448 " border-bottom-style: solid;"
3449 "}\n"
3450 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3451 ".total {background: #20D0D0;color: #ffff80;}\n"
3452 ".frontend {background: #e8e8d0;}\n"
3453 ".socket {background: #d0d0d0;}\n"
3454 ".backend {background: #e8e8d0;}\n"
3455 ".active_down {background: #ff9090;}\n"
3456 ".active_going_up {background: #ffd020;}\n"
3457 ".active_going_down {background: #ffffa0;}\n"
3458 ".active_up {background: #c0ffc0;}\n"
3459 ".active_nolb {background: #20a0ff;}\n"
3460 ".active_draining {background: #20a0FF;}\n"
3461 ".active_no_check {background: #e0e0e0;}\n"
3462 ".backup_down {background: #ff9090;}\n"
3463 ".backup_going_up {background: #ff80ff;}\n"
3464 ".backup_going_down {background: #c060ff;}\n"
3465 ".backup_up {background: #b0d0ff;}\n"
3466 ".backup_nolb {background: #90b0e0;}\n"
3467 ".backup_draining {background: #cc9900;}\n"
3468 ".backup_no_check {background: #e0e0e0;}\n"
3469 ".maintain {background: #c07820;}\n"
3470 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3471 "\n"
3472 "a.px:link {color: #ffff40; text-decoration: none;}"
3473 "a.px:visited {color: #ffff40; text-decoration: none;}"
3474 "a.px:hover {color: #ffffff; text-decoration: none;}"
3475 "a.lfsb:link {color: #000000; text-decoration: none;}"
3476 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3477 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3478 "\n"
3479 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3480 "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"
3481 "table.tbl td.ac { text-align: center;}\n"
3482 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3483 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3484 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3485 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3486 "\n"
3487 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3488 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3489 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3490 "table.det { border-collapse: collapse; border-style: none; }\n"
3491 "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"
3492 "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"
3493 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003494 "div.tips {\n"
3495 " display:block;\n"
3496 " visibility:hidden;\n"
3497 " z-index:2147483647;\n"
3498 " position:absolute;\n"
3499 " padding:2px 4px 3px;\n"
3500 " background:#f0f060; color:#000000;\n"
3501 " border:1px solid #7040c0;\n"
3502 " white-space:nowrap;\n"
3503 " font-style:normal;font-size:11px;font-weight:normal;\n"
3504 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3505 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3506 "}\n"
3507 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003508 "@media (prefers-color-scheme: dark) {\n"
3509 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3510 " h1 { color: #a265e0!important; }\n"
3511 " h2 { color: #a265e0; }\n"
3512 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3513 " a { color: #3391ff; }\n"
3514 " input { background-color: #2f3437; }\n"
3515 " .hr { border-color: #8c8273; }\n"
3516 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3517 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003518 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003519 " .backend {background: #2f3437;}\n"
3520 " .active_down {background: #760000;}\n"
3521 " .active_going_up {background: #b99200;}\n"
3522 " .active_going_down {background: #6c6c00;}\n"
3523 " .active_up {background: #165900;}\n"
3524 " .active_nolb {background: #006ab9;}\n"
3525 " .active_draining {background: #006ab9;}\n"
3526 " .active_no_check {background: #2a2d2f;}\n"
3527 " .backup_down {background: #760000;}\n"
3528 " .backup_going_up {background: #7f007f;}\n"
3529 " .backup_going_down {background: #580092;}\n"
3530 " .backup_up {background: #2e3234;}\n"
3531 " .backup_nolb {background: #1e3c6a;}\n"
3532 " .backup_draining {background: #a37a00;}\n"
3533 " .backup_no_check {background: #2a2d2f;}\n"
3534 " .maintain {background: #9a601a;}\n"
3535 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3536 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3537 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3538 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3539 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3540 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3541 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003542 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003543 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3544 " table.tbl th { border-color: #808080; }\n"
3545 " table.tbl td { border-color: #808080; }\n"
3546 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3547 " div.tips {\n"
3548 " background:#8e8e0d;\n"
3549 " color:#e8e6e3;\n"
3550 " border-color: #4e2c86;\n"
3551 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3552 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3553 " box-shadow: #60686c 2px 2px 3px;\n"
3554 " }\n"
3555 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003556 "-->\n"
3557 "</style></head>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003558 (ctx->flags & STAT_SHNODE) ? " on " : "",
3559 (ctx->flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003560 );
3561}
3562
Christopher Fauleta8b76842022-12-16 15:08:36 +01003563/* Dumps the HTML stats information block to the local trash buffer for and uses
3564 * the state from stream connector <sc> and per-uri parameters <uri>. The caller
3565 * is responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003566 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003567static void stats_dump_html_info(struct stconn *sc, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003568{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003569 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003570 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc05d30e2023-04-28 14:50:29 +02003571 unsigned int up = ns_to_sec(now_ns - start_time_ns);
William Lallemand74c24fb2016-11-21 17:18:36 +01003572 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02003573 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Willy Tarreau6be8d092023-01-12 16:08:41 +01003574 unsigned long long bps;
3575 int thr;
3576
3577 for (bps = thr = 0; thr < global.nbthread; thr++)
3578 bps += 32ULL * read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
Willy Tarreau1713c032019-05-23 12:23:55 +02003579
3580 /* Turn the bytes per second to bits per second and take care of the
3581 * usual ethernet overhead in order to help figure how far we are from
3582 * interface saturation since it's the only case which usually matters.
3583 * For this we count the total size of an Ethernet frame on the wire
3584 * including preamble and IFG (1538) for the largest TCP segment it
3585 * transports (1448 with TCP timestamps). This is not valid for smaller
3586 * packets (under-estimated), but it gives a reasonably accurate
3587 * estimation of how far we are from uplink saturation.
3588 */
3589 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003590
3591 /* WARNING! this has to fit the first packet too.
3592 * We are around 3.5 kB, add adding entries will
3593 * become tricky if we want to support 4kB buffers !
3594 */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003595 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003596 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3597 PRODUCT_NAME "%s</a></h1>\n"
3598 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3599 "<hr width=\"100%%\" class=\"hr\">\n"
3600 "<h3>&gt; General process information</h3>\n"
3601 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003602 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003603 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3604 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3605 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003606 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003607 "Running tasks: %d/%d; idle = %d %%<br>\n"
3608 "</td><td align=\"center\" nowrap>\n"
3609 "<table class=\"lgd\"><tr>\n"
3610 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3611 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3612 "</tr><tr>\n"
3613 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3614 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3615 "</tr><tr>\n"
3616 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3617 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3618 "</tr><tr>\n"
3619 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3620 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3621 "</tr><tr>\n"
3622 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3623 "</tr><tr>\n"
3624 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3625 "</tr></table>\n"
3626 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3627 "</td>"
3628 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3629 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3630 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003631 (ctx->flags & STAT_HIDEVER) ? "" : (stats_version_string),
3632 pid, (ctx->flags & STAT_SHNODE) ? " on " : "",
3633 (ctx->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3634 (ctx->flags & STAT_SHDESC) ? ": " : "",
3635 (ctx->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003636 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003637 up / 86400, (up % 86400) / 3600,
3638 (up % 3600) / 60, (up % 60),
3639 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3640 global.rlimit_memmax ? " MB" : "",
3641 global.rlimit_nofile,
3642 global.maxsock, global.maxconn, global.maxpipes,
3643 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003644 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3645 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003646 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003647 );
3648
Willy Tarreau91cefca2022-05-03 17:08:29 +02003649 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3650 memcpy(scope_txt, scope_ptr, ctx->scope_len);
3651 scope_txt[ctx->scope_len] = '\0';
William Lallemand74c24fb2016-11-21 17:18:36 +01003652
Christopher Fauleta8b76842022-12-16 15:08:36 +01003653 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003654 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003655 (ctx->scope_len > 0) ? scope_txt : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003656 STAT_SCOPE_TXT_MAXLEN);
3657
Willy Tarreau91cefca2022-05-03 17:08:29 +02003658 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003659 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003660 if (ctx->scope_len) {
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003661 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02003662 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3663 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003664 }
3665
Willy Tarreau91cefca2022-05-03 17:08:29 +02003666 if (ctx->flags & STAT_HIDE_DOWN)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003667 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003668 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3669 uri->uri_prefix,
3670 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003671 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003672 scope_txt);
3673 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003674 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003675 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3676 uri->uri_prefix,
3677 ";up",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003678 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003679 scope_txt);
3680
3681 if (uri->refresh > 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003682 if (ctx->flags & STAT_NO_REFRESH)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003683 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003684 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3685 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003686 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003687 "",
3688 scope_txt);
3689 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003690 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003691 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3692 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003693 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003694 ";norefresh",
3695 scope_txt);
3696 }
3697
Christopher Fauleta8b76842022-12-16 15:08:36 +01003698 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003699 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3700 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003701 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3702 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003703 scope_txt);
3704
Christopher Fauleta8b76842022-12-16 15:08:36 +01003705 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003706 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3707 uri->uri_prefix,
3708 (uri->refresh > 0) ? ";norefresh" : "",
3709 scope_txt);
3710
Christopher Fauleta8b76842022-12-16 15:08:36 +01003711 chunk_appendf(&trash_chunk,
Christopher Faulet6338a082019-09-09 15:50:54 +02003712 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3713 uri->uri_prefix,
3714 (uri->refresh > 0) ? ";norefresh" : "",
3715 scope_txt, uri->uri_prefix);
3716
Christopher Fauleta8b76842022-12-16 15:08:36 +01003717 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003718 "</ul></td>"
3719 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3720 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3721 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3722 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3723 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3724 "</ul>"
3725 "</td>"
3726 "</tr></table>\n"
3727 ""
3728 );
3729
Willy Tarreau91cefca2022-05-03 17:08:29 +02003730 if (ctx->st_code) {
3731 switch (ctx->st_code) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003732 case STAT_STATUS_DONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003733 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003734 "<p><div class=active_up>"
3735 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3736 "Action processed successfully."
3737 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003738 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3739 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003740 scope_txt);
3741 break;
3742 case STAT_STATUS_NONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003743 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003744 "<p><div class=active_going_down>"
3745 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3746 "Nothing has changed."
3747 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003748 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3749 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003750 scope_txt);
3751 break;
3752 case STAT_STATUS_PART:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003753 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003754 "<p><div class=active_going_down>"
3755 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3756 "Action partially processed.<br>"
3757 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3758 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003759 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3760 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003761 scope_txt);
3762 break;
3763 case STAT_STATUS_ERRP:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003764 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003765 "<p><div class=active_down>"
3766 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3767 "Action not processed because of invalid parameters."
3768 "<ul>"
3769 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003770 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003771 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3772 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3773 "</ul>"
3774 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003775 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3776 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003777 scope_txt);
3778 break;
3779 case STAT_STATUS_EXCD:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003780 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003781 "<p><div class=active_down>"
3782 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3783 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3784 "You should retry with less servers at a time.</b>"
3785 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003786 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3787 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003788 scope_txt);
3789 break;
3790 case STAT_STATUS_DENY:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003791 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003792 "<p><div class=active_down>"
3793 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3794 "<b>Action denied.</b>"
3795 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003796 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3797 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003798 scope_txt);
3799 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003800 case STAT_STATUS_IVAL:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003801 chunk_appendf(&trash_chunk,
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003802 "<p><div class=active_down>"
3803 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3804 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3805 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003806 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3807 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003808 scope_txt);
3809 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003810 default:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003811 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003812 "<p><div class=active_no_check>"
3813 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3814 "Unexpected result."
3815 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003816 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3817 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003818 scope_txt);
3819 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003820 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003821 }
3822}
3823
Christopher Fauleta8b76842022-12-16 15:08:36 +01003824/* Dumps the HTML stats trailer block to the local trash buffer. The caller is
3825 * responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003826 */
3827static void stats_dump_html_end()
3828{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003829 chunk_appendf(&trash_chunk, "</body></html>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003830}
3831
Christopher Fauleta8b76842022-12-16 15:08:36 +01003832/* Dumps the stats JSON header to the local trash buffer buffer which. The
3833 * caller is responsible for clearing it if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003834 */
3835static void stats_dump_json_header()
3836{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003837 chunk_strcat(&trash_chunk, "[");
Simon Horman05ee2132017-01-04 09:37:25 +01003838}
3839
3840
Christopher Fauleta8b76842022-12-16 15:08:36 +01003841/* Dumps the JSON stats trailer block to the local trash buffer. The caller is
3842 * responsible for clearing the local trash buffer if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003843 */
3844static void stats_dump_json_end()
3845{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003846 chunk_strcat(&trash_chunk, "]\n");
Simon Horman05ee2132017-01-04 09:37:25 +01003847}
3848
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003849/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3850 * a pointer to the current server/listener.
3851 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003852static int stats_dump_proxies(struct stconn *sc,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003853 struct htx *htx,
3854 struct uri_auth *uri)
3855{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003856 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003857 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003858 struct channel *rep = sc_ic(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003859 struct proxy *px;
3860
3861 /* dump proxies */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003862 while (ctx->obj1) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003863 if (htx) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003864 if (htx_almost_full(htx)) {
3865 sc_need_room(sc, htx->size / 2);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003866 goto full;
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003867 }
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003868 }
3869 else {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003870 if (buffer_almost_full(&rep->buf)) {
3871 sc_need_room(sc, b_size(&rep->buf) / 2);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003872 goto full;
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003873 }
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003874 }
3875
Willy Tarreau91cefca2022-05-03 17:08:29 +02003876 px = ctx->obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003877 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003878 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003879 * This change allows retrieving stats from "old" proxies after a reload.
3880 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003881 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003882 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003883 if (stats_dump_proxy_to_buffer(sc, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003884 return 0;
3885 }
3886
Willy Tarreau91cefca2022-05-03 17:08:29 +02003887 ctx->obj1 = px->next;
3888 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003889 ctx->field = 0;
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003890 }
3891
3892 return 1;
3893
3894 full:
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003895 return 0;
3896}
3897
Willy Tarreau4596fe22022-05-17 19:07:51 +02003898/* This function dumps statistics onto the stream connector's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003899 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3900 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3901 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3902 * and the stream must be closed, or -1 in case of any error. This function is
3903 * used by both the CLI and the HTTP handlers.
3904 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003905static int stats_dump_stat_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003906 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003907{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003908 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003909 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003910 enum stats_domain domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003911
Christopher Fauleta8b76842022-12-16 15:08:36 +01003912 chunk_reset(&trash_chunk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003913
Willy Tarreau41f88522022-05-03 18:39:27 +02003914 switch (ctx->state) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003915 case STAT_STATE_INIT:
3916 ctx->state = STAT_STATE_HEAD; /* let's start producing data */
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003917 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003918
Willy Tarreau6ef16482022-05-06 18:07:53 +02003919 case STAT_STATE_HEAD:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003920 if (ctx->flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003921 stats_dump_html_head(appctx, uri);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003922 else if (ctx->flags & STAT_JSON_SCHM)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003923 stats_dump_json_schema(&trash_chunk);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003924 else if (ctx->flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003925 stats_dump_json_header();
Willy Tarreau91cefca2022-05-03 17:08:29 +02003926 else if (!(ctx->flags & STAT_FMT_TYPED))
3927 stats_dump_csv_header(ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003928
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003929 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003930 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003931
Willy Tarreau91cefca2022-05-03 17:08:29 +02003932 if (ctx->flags & STAT_JSON_SCHM) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003933 ctx->state = STAT_STATE_FIN;
Christopher Faulet6338a082019-09-09 15:50:54 +02003934 return 1;
3935 }
Willy Tarreau6ef16482022-05-06 18:07:53 +02003936 ctx->state = STAT_STATE_INFO;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003937 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003938
Willy Tarreau6ef16482022-05-06 18:07:53 +02003939 case STAT_STATE_INFO:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003940 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003941 stats_dump_html_info(sc, uri);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003942 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003943 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003944 }
3945
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003946 if (domain == STATS_DOMAIN_PROXY)
Willy Tarreau91cefca2022-05-03 17:08:29 +02003947 ctx->obj1 = proxies_list;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003948
Willy Tarreau91cefca2022-05-03 17:08:29 +02003949 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003950 ctx->field = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003951 ctx->state = STAT_STATE_LIST;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003952 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003953
Willy Tarreau6ef16482022-05-06 18:07:53 +02003954 case STAT_STATE_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003955 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003956 case STATS_DOMAIN_RESOLVERS:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003957 if (!stats_dump_resolvers(sc, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003958 stat_count[domain],
3959 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003960 return 0;
3961 }
3962 break;
3963
3964 case STATS_DOMAIN_PROXY:
3965 default:
3966 /* dump proxies */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003967 if (!stats_dump_proxies(sc, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003968 return 0;
3969 break;
3970 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003971
Willy Tarreau6ef16482022-05-06 18:07:53 +02003972 ctx->state = STAT_STATE_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003973 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003974
Willy Tarreau6ef16482022-05-06 18:07:53 +02003975 case STAT_STATE_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003976 if (ctx->flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3977 if (ctx->flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01003978 stats_dump_html_end();
3979 else
3980 stats_dump_json_end();
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003981 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003982 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003983 }
3984
Willy Tarreau6ef16482022-05-06 18:07:53 +02003985 ctx->state = STAT_STATE_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003986 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003987
Willy Tarreau6ef16482022-05-06 18:07:53 +02003988 case STAT_STATE_FIN:
William Lallemand74c24fb2016-11-21 17:18:36 +01003989 return 1;
3990
3991 default:
3992 /* unknown state ! */
Willy Tarreau6ef16482022-05-06 18:07:53 +02003993 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01003994 return -1;
3995 }
Christopher Fauletef779222018-10-31 08:47:01 +01003996
3997 full:
Christopher Fauletef779222018-10-31 08:47:01 +01003998 return 0;
3999
William Lallemand74c24fb2016-11-21 17:18:36 +01004000}
4001
4002/* We reached the stats page through a POST request. The appctx is
4003 * expected to have already been allocated by the caller.
4004 * Parse the posted data and enable/disable servers if necessary.
4005 * Returns 1 if request was parsed or zero if it needs more data.
4006 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004007static int stats_process_http_post(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01004008{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004009 struct stream *s = __sc_strm(sc);
4010 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004011 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01004012
4013 struct proxy *px = NULL;
4014 struct server *sv = NULL;
4015
4016 char key[LINESIZE];
4017 int action = ST_ADM_ACTION_NONE;
4018 int reprocess = 0;
4019
4020 int total_servers = 0;
4021 int altered_servers = 0;
4022
4023 char *first_param, *cur_param, *next_param, *end_params;
4024 char *st_cur_param = NULL;
4025 char *st_next_param = NULL;
4026
Christopher Fauleta3618372018-12-13 21:59:56 +01004027 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01004028
Christopher Fauletb7f88902019-07-15 21:56:43 +02004029 struct htx *htx = htxbuf(&s->req.buf);
4030 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01004031
Christopher Fauletb7f88902019-07-15 21:56:43 +02004032 /* we need more data */
4033 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
4034 /* check if we can receive more */
4035 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004036 ctx->st_code = STAT_STATUS_EXCD;
Christopher Fauletb7f88902019-07-15 21:56:43 +02004037 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01004038 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004039 goto wait;
4040 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004041
Christopher Fauletb7f88902019-07-15 21:56:43 +02004042 /* The request was fully received. Copy data */
4043 blk = htx_get_head_blk(htx);
4044 while (blk) {
4045 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004046
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004047 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02004048 break;
4049 if (type == HTX_BLK_DATA) {
4050 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004051
Christopher Fauletb7f88902019-07-15 21:56:43 +02004052 if (!chunk_memcat(temp, v.ptr, v.len)) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004053 ctx->st_code = STAT_STATUS_EXCD;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004054 goto out;
4055 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004056 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004057 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01004058 }
4059
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004060 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01004061 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01004062 cur_param = next_param = end_params;
4063 *end_params = '\0';
4064
Willy Tarreau91cefca2022-05-03 17:08:29 +02004065 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004066
4067 /*
4068 * Parse the parameters in reverse order to only store the last value.
4069 * From the html form, the backend and the action are at the end.
4070 */
4071 while (cur_param > first_param) {
4072 char *value;
4073 int poffset, plen;
4074
4075 cur_param--;
4076
4077 if ((*cur_param == '&') || (cur_param == first_param)) {
4078 reprocess_servers:
4079 /* Parse the key */
4080 poffset = (cur_param != first_param ? 1 : 0);
4081 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4082 if ((plen > 0) && (plen <= sizeof(key))) {
4083 strncpy(key, cur_param + poffset, plen);
4084 key[plen - 1] = '\0';
4085 } else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004086 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004087 goto out;
4088 }
4089
4090 /* Parse the value */
4091 value = key;
4092 while (*value != '\0' && *value != '=') {
4093 value++;
4094 }
4095 if (*value == '=') {
4096 /* Ok, a value is found, we can mark the end of the key */
4097 *value++ = '\0';
4098 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02004099 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01004100 break;
4101
4102 /* Now we can check the key to see what to do */
4103 if (!px && (strcmp(key, "b") == 0)) {
4104 if ((px = proxy_be_by_name(value)) == NULL) {
4105 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004106 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004107 goto out;
4108 }
4109 }
4110 else if (!action && (strcmp(key, "action") == 0)) {
4111 if (strcmp(value, "ready") == 0) {
4112 action = ST_ADM_ACTION_READY;
4113 }
4114 else if (strcmp(value, "drain") == 0) {
4115 action = ST_ADM_ACTION_DRAIN;
4116 }
4117 else if (strcmp(value, "maint") == 0) {
4118 action = ST_ADM_ACTION_MAINT;
4119 }
4120 else if (strcmp(value, "shutdown") == 0) {
4121 action = ST_ADM_ACTION_SHUTDOWN;
4122 }
4123 else if (strcmp(value, "dhlth") == 0) {
4124 action = ST_ADM_ACTION_DHLTH;
4125 }
4126 else if (strcmp(value, "ehlth") == 0) {
4127 action = ST_ADM_ACTION_EHLTH;
4128 }
4129 else if (strcmp(value, "hrunn") == 0) {
4130 action = ST_ADM_ACTION_HRUNN;
4131 }
4132 else if (strcmp(value, "hnolb") == 0) {
4133 action = ST_ADM_ACTION_HNOLB;
4134 }
4135 else if (strcmp(value, "hdown") == 0) {
4136 action = ST_ADM_ACTION_HDOWN;
4137 }
4138 else if (strcmp(value, "dagent") == 0) {
4139 action = ST_ADM_ACTION_DAGENT;
4140 }
4141 else if (strcmp(value, "eagent") == 0) {
4142 action = ST_ADM_ACTION_EAGENT;
4143 }
4144 else if (strcmp(value, "arunn") == 0) {
4145 action = ST_ADM_ACTION_ARUNN;
4146 }
4147 else if (strcmp(value, "adown") == 0) {
4148 action = ST_ADM_ACTION_ADOWN;
4149 }
4150 /* else these are the old supported methods */
4151 else if (strcmp(value, "disable") == 0) {
4152 action = ST_ADM_ACTION_DISABLE;
4153 }
4154 else if (strcmp(value, "enable") == 0) {
4155 action = ST_ADM_ACTION_ENABLE;
4156 }
4157 else if (strcmp(value, "stop") == 0) {
4158 action = ST_ADM_ACTION_STOP;
4159 }
4160 else if (strcmp(value, "start") == 0) {
4161 action = ST_ADM_ACTION_START;
4162 }
4163 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004164 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004165 goto out;
4166 }
4167 }
4168 else if (strcmp(key, "s") == 0) {
4169 if (!(px && action)) {
4170 /*
4171 * Indicates that we'll need to reprocess the parameters
4172 * as soon as backend and action are known
4173 */
4174 if (!reprocess) {
4175 st_cur_param = cur_param;
4176 st_next_param = next_param;
4177 }
4178 reprocess = 1;
4179 }
4180 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004181 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004182 switch (action) {
4183 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004184 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004185 altered_servers++;
4186 total_servers++;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004187 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, SRV_ADM_STCHGC_STATS_DISABLE);
William Lallemand74c24fb2016-11-21 17:18:36 +01004188 }
4189 break;
4190 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004191 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004192 altered_servers++;
4193 total_servers++;
4194 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4195 }
4196 break;
4197 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004198 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004199 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, SRV_ADM_STCHGC_STATS_STOP);
William Lallemand74c24fb2016-11-21 17:18:36 +01004200 altered_servers++;
4201 total_servers++;
4202 }
4203 break;
4204 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004205 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004206 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4207 altered_servers++;
4208 total_servers++;
4209 }
4210 break;
4211 case ST_ADM_ACTION_DHLTH:
4212 if (sv->check.state & CHK_ST_CONFIGURED) {
4213 sv->check.state &= ~CHK_ST_ENABLED;
4214 altered_servers++;
4215 total_servers++;
4216 }
4217 break;
4218 case ST_ADM_ACTION_EHLTH:
4219 if (sv->check.state & CHK_ST_CONFIGURED) {
4220 sv->check.state |= CHK_ST_ENABLED;
4221 altered_servers++;
4222 total_servers++;
4223 }
4224 break;
4225 case ST_ADM_ACTION_HRUNN:
4226 if (!(sv->track)) {
4227 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004228 srv_set_running(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004229 altered_servers++;
4230 total_servers++;
4231 }
4232 break;
4233 case ST_ADM_ACTION_HNOLB:
4234 if (!(sv->track)) {
4235 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004236 srv_set_stopping(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004237 altered_servers++;
4238 total_servers++;
4239 }
4240 break;
4241 case ST_ADM_ACTION_HDOWN:
4242 if (!(sv->track)) {
4243 sv->check.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004244 srv_set_stopped(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004245 altered_servers++;
4246 total_servers++;
4247 }
4248 break;
4249 case ST_ADM_ACTION_DAGENT:
4250 if (sv->agent.state & CHK_ST_CONFIGURED) {
4251 sv->agent.state &= ~CHK_ST_ENABLED;
4252 altered_servers++;
4253 total_servers++;
4254 }
4255 break;
4256 case ST_ADM_ACTION_EAGENT:
4257 if (sv->agent.state & CHK_ST_CONFIGURED) {
4258 sv->agent.state |= CHK_ST_ENABLED;
4259 altered_servers++;
4260 total_servers++;
4261 }
4262 break;
4263 case ST_ADM_ACTION_ARUNN:
4264 if (sv->agent.state & CHK_ST_ENABLED) {
4265 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004266 srv_set_running(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004267 altered_servers++;
4268 total_servers++;
4269 }
4270 break;
4271 case ST_ADM_ACTION_ADOWN:
4272 if (sv->agent.state & CHK_ST_ENABLED) {
4273 sv->agent.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004274 srv_set_stopped(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004275 altered_servers++;
4276 total_servers++;
4277 }
4278 break;
4279 case ST_ADM_ACTION_READY:
4280 srv_adm_set_ready(sv);
4281 altered_servers++;
4282 total_servers++;
4283 break;
4284 case ST_ADM_ACTION_DRAIN:
4285 srv_adm_set_drain(sv);
4286 altered_servers++;
4287 total_servers++;
4288 break;
4289 case ST_ADM_ACTION_MAINT:
4290 srv_adm_set_maint(sv);
4291 altered_servers++;
4292 total_servers++;
4293 break;
4294 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004295 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004296 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004297 altered_servers++;
4298 total_servers++;
4299 }
4300 break;
4301 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004302 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004303 } else {
4304 /* the server name is unknown or ambiguous (duplicate names) */
4305 total_servers++;
4306 }
4307 }
4308 if (reprocess && px && action) {
4309 /* Now, we know the backend and the action chosen by the user.
4310 * We can safely restart from the first server parameter
4311 * to reprocess them
4312 */
4313 cur_param = st_cur_param;
4314 next_param = st_next_param;
4315 reprocess = 0;
4316 goto reprocess_servers;
4317 }
4318
4319 next_param = cur_param;
4320 }
4321 }
4322
4323 if (total_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004324 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004325 }
4326 else if (altered_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004327 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004328 }
4329 else if (altered_servers == total_servers) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004330 ctx->st_code = STAT_STATUS_DONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004331 }
4332 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004333 ctx->st_code = STAT_STATUS_PART;
William Lallemand74c24fb2016-11-21 17:18:36 +01004334 }
4335 out:
4336 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004337 wait:
Willy Tarreau91cefca2022-05-03 17:08:29 +02004338 ctx->st_code = STAT_STATUS_NONE;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004339 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004340}
4341
4342
Willy Tarreaucaff6312022-05-27 10:17:46 +02004343static int stats_send_http_headers(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004344{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004345 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004346 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004347 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004348 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004349 struct htx_sl *sl;
4350 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004351
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004352 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);
4353 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4354 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004355 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004356 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004357
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004358 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004359 goto full;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004360 if (ctx->flags & STAT_FMT_HTML) {
Christopher Fauletef779222018-10-31 08:47:01 +01004361 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4362 goto full;
4363 }
Willy Tarreau91cefca2022-05-03 17:08:29 +02004364 else if (ctx->flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004365 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4366 goto full;
4367 }
Christopher Fauletef779222018-10-31 08:47:01 +01004368 else {
4369 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4370 goto full;
4371 }
4372
Willy Tarreau91cefca2022-05-03 17:08:29 +02004373 if (uri->refresh > 0 && !(ctx->flags & STAT_NO_REFRESH)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004374 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004375 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004376 goto full;
4377 }
4378
Willy Tarreau91cefca2022-05-03 17:08:29 +02004379 if (ctx->flags & STAT_CHUNKED) {
Christopher Fauletef779222018-10-31 08:47:01 +01004380 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4381 goto full;
4382 }
4383
4384 if (!htx_add_endof(htx, HTX_BLK_EOH))
4385 goto full;
4386
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004387 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004388 return 1;
4389
4390 full:
4391 htx_reset(htx);
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004392 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004393 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004394}
4395
Christopher Fauletef779222018-10-31 08:47:01 +01004396
Willy Tarreaucaff6312022-05-27 10:17:46 +02004397static int stats_send_http_redirect(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004398{
4399 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02004400 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004401 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004402 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004403 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004404 struct htx_sl *sl;
4405 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004406
Willy Tarreau91cefca2022-05-03 17:08:29 +02004407 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauletef779222018-10-31 08:47:01 +01004408 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004409 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004410 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004411
Willy Tarreaufc458ec2023-04-07 18:11:39 +02004412 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02004413 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
4414 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004415 }
4416
4417 /* We don't want to land on the posted stats page because a refresh will
4418 * repost the data. We don't want this to happen on accident so we redirect
4419 * the browse to the stats page with a GET.
4420 */
4421 chunk_printf(&trash, "%s;st=%s%s%s%s",
4422 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02004423 ((ctx->st_code > STAT_STATUS_INIT) &&
4424 (ctx->st_code < STAT_STATUS_SIZE) &&
4425 stat_status_codes[ctx->st_code]) ?
4426 stat_status_codes[ctx->st_code] :
Christopher Fauletef779222018-10-31 08:47:01 +01004427 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau91cefca2022-05-03 17:08:29 +02004428 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
4429 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Fauletef779222018-10-31 08:47:01 +01004430 scope_txt);
4431
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004432 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4433 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4434 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004435 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004436 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004437
4438 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004439 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4440 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4441 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4442 goto full;
4443
4444 if (!htx_add_endof(htx, HTX_BLK_EOH))
4445 goto full;
4446
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004447 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004448 return 1;
4449
4450full:
4451 htx_reset(htx);
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004452 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004453 return 0;
4454}
William Lallemand74c24fb2016-11-21 17:18:36 +01004455
Simon Horman05ee2132017-01-04 09:37:25 +01004456
Willy Tarreau4596fe22022-05-17 19:07:51 +02004457/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004458 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4459 * appctx->st0 contains the operation in progress (dump, done). The handler
4460 * automatically unregisters itself once transfer is complete.
4461 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004462static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004463{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004464 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02004465 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004466 struct stream *s = __sc_strm(sc);
4467 struct channel *req = sc_oc(sc);
4468 struct channel *res = sc_ic(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004469 struct htx *req_htx, *res_htx;
4470
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004471 /* only proxy stats are available via http */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004472 ctx->domain = STATS_DOMAIN_PROXY;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004473
Christopher Fauletef779222018-10-31 08:47:01 +01004474 res_htx = htx_from_buf(&res->buf);
4475
Christopher Faulet9837bd82023-04-11 07:41:30 +02004476 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) {
4477 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004478 goto out;
Christopher Faulet9837bd82023-04-11 07:41:30 +02004479 }
Christopher Fauletef779222018-10-31 08:47:01 +01004480
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004481 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004482 if (!b_size(&res->buf)) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004483 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004484 goto out;
4485 }
4486
Christopher Fauletef779222018-10-31 08:47:01 +01004487 /* all states are processed in sequence */
4488 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004489 if (stats_send_http_headers(sc, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004490 if (s->txn->meth == HTTP_METH_HEAD)
4491 appctx->st0 = STAT_HTTP_DONE;
4492 else
4493 appctx->st0 = STAT_HTTP_DUMP;
4494 }
4495 }
4496
4497 if (appctx->st0 == STAT_HTTP_DUMP) {
Aurelien DARRAGON14656842023-02-03 08:31:42 +01004498 trash_chunk = b_make(trash.area, res->buf.size, 0, 0);
Aurelien DARRAGON5e7ecbe2023-02-02 15:03:12 +01004499 /* adjust buffer size to take htx overhead into account,
4500 * make sure to perform this call on an empty buffer
4501 */
4502 trash_chunk.size = buf_room_for_htx_data(&trash_chunk);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004503 if (stats_dump_stat_to_buffer(sc, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004504 appctx->st0 = STAT_HTTP_DONE;
4505 }
4506
4507 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004508 if (stats_process_http_post(sc))
Christopher Fauletef779222018-10-31 08:47:01 +01004509 appctx->st0 = STAT_HTTP_LAST;
Christopher Fauletca5309a2023-04-17 16:17:32 +02004510 else if (s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Fauletef779222018-10-31 08:47:01 +01004511 appctx->st0 = STAT_HTTP_DONE;
4512 }
4513
4514 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004515 if (stats_send_http_redirect(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004516 appctx->st0 = STAT_HTTP_DONE;
4517 }
4518
4519 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004520 /* no more data are expected. If the response buffer is empty,
4521 * be sure to add something (EOT block in this case) to have
4522 * something to send. It is important to be sure the EOM flags
4523 * will be handled by the endpoint.
4524 */
4525 if (htx_is_empty(res_htx)) {
4526 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004527 sc_need_room(sc, sizeof(struct htx_blk) + 1);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004528 goto out;
4529 }
4530 channel_add_input(res, 1);
4531 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004532 res_htx->flags |= HTX_FL_EOM;
Willy Tarreaud869e132022-05-17 18:05:31 +02004533 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004534 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004535 }
4536
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004537 if (appctx->st0 == STAT_HTTP_END) {
Christopher Fauletdf15a5d2023-03-31 11:28:22 +02004538 se_fl_set(appctx->sedesc, SE_FL_EOS);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004539 applet_will_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004540 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004541
Christopher Fauletef779222018-10-31 08:47:01 +01004542 out:
4543 /* we have left the request in the buffer for the case where we
4544 * process a POST, and this automatically re-enables activity on
4545 * read. It's better to indicate that we want to stop reading when
4546 * we're sending, so that we know there's at most one direction
4547 * deciding to wake the applet up. It saves it from looping when
4548 * emitting large blocks into small TCP windows.
4549 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004550 htx_to_buf(res_htx, &res->buf);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004551 if (appctx->st0 == STAT_HTTP_END) {
4552 /* eat the whole request */
4553 if (co_data(req)) {
4554 req_htx = htx_from_buf(&req->buf);
4555 co_htx_skip(req, req_htx, co_data(req));
4556 htx_to_buf(req_htx, &req->buf);
4557 }
4558 }
4559 else if (!channel_is_empty(res))
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02004560 applet_wont_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004561}
4562
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004563/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004564static int stats_dump_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004565 const struct field *info,
4566 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004567{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004568 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004569 int field;
4570
4571 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4572 if (!field_format(info, field))
4573 continue;
4574
Willy Tarreaueaa55372019-10-09 07:39:11 +02004575 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004576 return 0;
4577 if (!stats_emit_raw_data_field(out, &info[field]))
4578 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004579 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4580 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004581 if (!chunk_strcat(out, "\n"))
4582 return 0;
4583 }
4584 return 1;
4585}
4586
4587/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004588static int stats_dump_typed_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004589 const struct field *info,
4590 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004591{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004592 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004593 int field;
4594
4595 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4596 if (!field_format(info, field))
4597 continue;
4598
Willy Tarreaueaa55372019-10-09 07:39:11 +02004599 if (!chunk_appendf(out, "%d.%s.%u:", field, info_fields[field].name, info[INF_PROCESS_NUM].u.u32))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004600 return 0;
4601 if (!stats_emit_field_tags(out, &info[field], ':'))
4602 return 0;
4603 if (!stats_emit_typed_data_field(out, &info[field]))
4604 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004605 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4606 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004607 if (!chunk_strcat(out, "\n"))
4608 return 0;
4609 }
4610 return 1;
4611}
4612
Willy Tarreau0b26b382021-05-08 07:43:53 +02004613/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4614 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4615 * less then this value, the function returns 0, otherwise, it returns 1. Some
4616 * fields' presence or precision may depend on some of the STAT_* flags present
4617 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004618 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004619int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004620{
Willy Tarreau83061a82018-07-13 11:56:34 +02004621 struct buffer *out = get_trash_chunk();
Willy Tarreau6be8d092023-01-12 16:08:41 +01004622 uint64_t glob_out_bytes, glob_spl_bytes, glob_out_b32;
Willy Tarreau563efe62023-04-27 14:41:37 +02004623 uint up_sec, up_usec;
4624 ullong up;
Willy Tarreau6be8d092023-01-12 16:08:41 +01004625 int thr;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004626
4627#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004628 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4629 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4630 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004631
Willy Tarreauc5977722021-05-08 08:14:04 +02004632 if (ssl_key_rate < ssl_sess_rate)
4633 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004634#endif
4635
Willy Tarreau6be8d092023-01-12 16:08:41 +01004636 /* sum certain per-thread totals (mostly byte counts) */
4637 glob_out_bytes = glob_spl_bytes = glob_out_b32 = 0;
4638 for (thr = 0; thr < global.nbthread; thr++) {
4639 glob_out_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].out_bytes);
4640 glob_spl_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].spliced_out_bytes);
4641 glob_out_b32 += read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
4642 }
4643 glob_out_b32 *= 32; // values are 32-byte units
4644
Willy Tarreauc05d30e2023-04-28 14:50:29 +02004645 up = now_ns - start_time_ns;
Willy Tarreau563efe62023-04-27 14:41:37 +02004646 up_sec = ns_to_sec(up);
4647 up_usec = (up / 1000U) % 1000000U;
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004648
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004649 if (len < INF_TOTAL_FIELDS)
4650 return 0;
4651
4652 chunk_reset(out);
4653 memset(info, 0, sizeof(*info) * len);
4654
4655 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004656 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004657 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004658 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004659
Yves Lafon95317282018-02-26 11:10:37 +01004660 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004661 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004662 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004663 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4664
4665 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreau563efe62023-04-27 14:41:37 +02004666 chunk_appendf(out, "%ud %uh%02um%02us", up_sec / 86400, (up_sec % 86400) / 3600, (up_sec % 3600) / 60, (up_sec % 60));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004667
Willy Tarreau563efe62023-04-27 14:41:37 +02004668 info[INF_UPTIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, up_sec + up_usec / 1000000.0) : mkf_u32(FN_DURATION, up_sec);
Willy Tarreau7222db72023-04-28 15:57:18 +02004669 info[INF_START_TIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, start_date.tv_sec + start_date.tv_usec / 1000000.0) : mkf_u32(FN_DURATION, start_date.tv_sec);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004670 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004671 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004672 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004673 info[INF_POOL_ALLOC_BYTES] = mkf_u64(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004674 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004675 info[INF_POOL_USED_BYTES] = mkf_u64(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004676 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4677 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4678 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4679 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4680 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4681 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4682 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4683 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4684#ifdef USE_OPENSSL
4685 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004686 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4687 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004688#endif
4689 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4690 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4691 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004692 info[INF_CONN_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.conn_per_sec)) : mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004693 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4694 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004695 info[INF_SESS_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.sess_per_sec)) : mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004696 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4697 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4698
4699#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004700 info[INF_SSL_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_sess_rate) : mkf_u32(FN_RATE, ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004701 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4702 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004703 info[INF_SSL_FRONTEND_KEY_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_key_rate) : mkf_u32(0, ssl_key_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004704 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004705 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4706 info[INF_SSL_BACKEND_KEY_RATE] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.ssl_be_keys_per_sec)) : mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004707 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4708 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4709 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4710#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004711 info[INF_COMPRESS_BPS_IN] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.comp_bps_in)) : mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
4712 info[INF_COMPRESS_BPS_OUT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, read_freq_ctr_flt(&global.comp_bps_out)) : mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004713 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4714#ifdef USE_ZLIB
4715 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4716 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4717#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004718 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004719 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004720 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004721 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4722 if (global.desc)
4723 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004724 info[INF_STOPPING] = mkf_u32(0, stopping);
4725 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004726 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004727 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004728 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004729 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004730 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004731 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004732 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau6be8d092023-01-12 16:08:41 +01004733 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, glob_out_bytes);
4734 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, glob_spl_bytes);
4735 info[INF_BYTES_OUT_RATE] = mkf_u64(FN_RATE, glob_out_b32);
Willy Tarreau9b013702019-10-24 18:18:02 +02004736 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004737 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004738
4739 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4740 chunk_appendf(out, "%#x", get_tainted());
4741
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004742 return 1;
4743}
4744
Willy Tarreau4596fe22022-05-17 19:07:51 +02004745/* This function dumps information onto the stream connector's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004746 * It returns 0 as long as it does not complete, non-zero upon completion.
4747 * No state is used.
4748 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004749static int stats_dump_info_to_buffer(struct stconn *sc)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004750{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004751 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004752 struct show_stat_ctx *ctx = appctx->svcctx;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004753 int ret;
4754 int current_field;
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004755
Willy Tarreau91cefca2022-05-03 17:08:29 +02004756 if (!stats_fill_info(info, INF_TOTAL_FIELDS, ctx->flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004757 return 0;
4758
Christopher Fauleta8b76842022-12-16 15:08:36 +01004759 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004760more:
4761 current_field = ctx->field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004762
Willy Tarreau91cefca2022-05-03 17:08:29 +02004763 if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004764 ret = stats_dump_typed_info_fields(&trash_chunk, info, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004765 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004766 ret = stats_dump_json_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004767 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01004768 ret = stats_dump_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004769
Christopher Fauleta8b76842022-12-16 15:08:36 +01004770 if (applet_putchk(appctx, &trash_chunk) == -1) {
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004771 /* restore previous field */
4772 ctx->field = current_field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004773 return 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004774 }
4775 if (ret && ctx->field) {
4776 /* partial dump */
4777 goto more;
4778 }
4779 ctx->field = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004780 return 1;
4781}
4782
Willy Tarreau4596fe22022-05-17 19:07:51 +02004783/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004784 * It returns 0 as long as it does not complete, non-zero upon completion.
4785 * No state is used.
4786 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004787 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004788 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4789 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004790static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004791{
4792
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004793 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004794
4795 chunk_strcat(out,
4796 "{"
4797 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4798 "\"oneOf\":["
4799 "{"
4800 "\"title\":\"Info\","
4801 "\"type\":\"array\","
4802 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004803 "\"title\":\"InfoItem\","
4804 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004805 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004806 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4807 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4808 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4809 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4810 "},"
4811 "\"required\":[\"field\",\"processNum\",\"tags\","
4812 "\"value\"]"
4813 "}"
4814 "},"
4815 "{"
4816 "\"title\":\"Stat\","
4817 "\"type\":\"array\","
4818 "\"items\":{"
4819 "\"title\":\"InfoItem\","
4820 "\"type\":\"object\","
4821 "\"properties\":{"
4822 "\"objType\":{"
4823 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4824 "\"Server\",\"Unknown\"]"
4825 "},"
4826 "\"proxyId\":{"
4827 "\"type\":\"integer\","
4828 "\"minimum\":0"
4829 "},"
4830 "\"id\":{"
4831 "\"type\":\"integer\","
4832 "\"minimum\":0"
4833 "},"
4834 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4835 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4836 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4837 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4838 "},"
4839 "\"required\":[\"objType\",\"proxyId\",\"id\","
4840 "\"field\",\"processNum\",\"tags\","
4841 "\"value\"]"
4842 "}"
4843 "},"
4844 "{"
4845 "\"title\":\"Error\","
4846 "\"type\":\"object\","
4847 "\"properties\":{"
4848 "\"errorStr\":{"
4849 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004850 "}"
4851 "},"
4852 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004853 "}"
4854 "],"
4855 "\"definitions\":{"
4856 "\"field\":{"
4857 "\"type\":\"object\","
4858 "\"pos\":{"
4859 "\"type\":\"integer\","
4860 "\"minimum\":0"
4861 "},"
4862 "\"name\":{"
4863 "\"type\":\"string\""
4864 "},"
4865 "\"required\":[\"pos\",\"name\"]"
4866 "},"
4867 "\"processNum\":{"
4868 "\"type\":\"integer\","
4869 "\"minimum\":1"
4870 "},"
4871 "\"tags\":{"
4872 "\"type\":\"object\","
4873 "\"origin\":{"
4874 "\"type\":\"string\","
4875 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4876 "\"Config\",\"Product\",\"Unknown\"]"
4877 "},"
4878 "\"nature\":{"
4879 "\"type\":\"string\","
4880 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4881 "\"Rate\",\"Counter\",\"Duration\","
4882 "\"Age\",\"Time\",\"Name\",\"Output\","
4883 "\"Avg\", \"Unknown\"]"
4884 "},"
4885 "\"scope\":{"
4886 "\"type\":\"string\","
4887 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4888 "\"System\",\"Unknown\"]"
4889 "},"
4890 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4891 "},"
4892 "\"typedValue\":{"
4893 "\"type\":\"object\","
4894 "\"oneOf\":["
4895 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4896 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4897 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4898 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4899 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4900 "],"
4901 "\"definitions\":{"
4902 "\"s32Value\":{"
4903 "\"properties\":{"
4904 "\"type\":{"
4905 "\"type\":\"string\","
4906 "\"enum\":[\"s32\"]"
4907 "},"
4908 "\"value\":{"
4909 "\"type\":\"integer\","
4910 "\"minimum\":-2147483648,"
4911 "\"maximum\":2147483647"
4912 "}"
4913 "},"
4914 "\"required\":[\"type\",\"value\"]"
4915 "},"
4916 "\"s64Value\":{"
4917 "\"properties\":{"
4918 "\"type\":{"
4919 "\"type\":\"string\","
4920 "\"enum\":[\"s64\"]"
4921 "},"
4922 "\"value\":{"
4923 "\"type\":\"integer\","
4924 "\"minimum\":-9007199254740991,"
4925 "\"maximum\":9007199254740991"
4926 "}"
4927 "},"
4928 "\"required\":[\"type\",\"value\"]"
4929 "},"
4930 "\"u32Value\":{"
4931 "\"properties\":{"
4932 "\"type\":{"
4933 "\"type\":\"string\","
4934 "\"enum\":[\"u32\"]"
4935 "},"
4936 "\"value\":{"
4937 "\"type\":\"integer\","
4938 "\"minimum\":0,"
4939 "\"maximum\":4294967295"
4940 "}"
4941 "},"
4942 "\"required\":[\"type\",\"value\"]"
4943 "},"
4944 "\"u64Value\":{"
4945 "\"properties\":{"
4946 "\"type\":{"
4947 "\"type\":\"string\","
4948 "\"enum\":[\"u64\"]"
4949 "},"
4950 "\"value\":{"
4951 "\"type\":\"integer\","
4952 "\"minimum\":0,"
4953 "\"maximum\":9007199254740991"
4954 "}"
4955 "},"
4956 "\"required\":[\"type\",\"value\"]"
4957 "},"
4958 "\"strValue\":{"
4959 "\"properties\":{"
4960 "\"type\":{"
4961 "\"type\":\"string\","
4962 "\"enum\":[\"str\"]"
4963 "},"
4964 "\"value\":{\"type\":\"string\"}"
4965 "},"
4966 "\"required\":[\"type\",\"value\"]"
4967 "},"
4968 "\"unknownValue\":{"
4969 "\"properties\":{"
4970 "\"type\":{"
4971 "\"type\":\"integer\","
4972 "\"minimum\":0"
4973 "},"
4974 "\"value\":{"
4975 "\"type\":\"string\","
4976 "\"enum\":[\"unknown\"]"
4977 "}"
4978 "},"
4979 "\"required\":[\"type\",\"value\"]"
4980 "}"
4981 "}"
4982 "}"
4983 "}"
4984 "}");
4985
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004986 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004987 chunk_reset(out);
4988 chunk_appendf(out,
4989 "{\"errorStr\":\"output buffer too short\"}");
4990 }
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02004991 chunk_appendf(out, "\n");
Simon Horman6f6bb382017-01-04 09:37:26 +01004992}
4993
Willy Tarreau4596fe22022-05-17 19:07:51 +02004994/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004995 * It returns 0 as long as it does not complete, non-zero upon completion.
4996 * No state is used.
4997 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004998static int stats_dump_json_schema_to_buffer(struct appctx *appctx)
Simon Horman6f6bb382017-01-04 09:37:26 +01004999{
Simon Horman6f6bb382017-01-04 09:37:26 +01005000
Christopher Fauleta8b76842022-12-16 15:08:36 +01005001 chunk_reset(&trash_chunk);
Simon Horman6f6bb382017-01-04 09:37:26 +01005002
Christopher Fauleta8b76842022-12-16 15:08:36 +01005003 stats_dump_json_schema(&trash_chunk);
5004
5005 if (applet_putchk(appctx, &trash_chunk) == -1)
Simon Horman6f6bb382017-01-04 09:37:26 +01005006 return 0;
Simon Horman6f6bb382017-01-04 09:37:26 +01005007
5008 return 1;
5009}
5010
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005011static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01005012{
5013 struct proxy *px;
5014 struct server *sv;
5015 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005016 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005017 int clrall = 0;
5018
5019 if (strcmp(args[2], "all") == 0)
5020 clrall = 1;
5021
5022 /* check permissions */
5023 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
5024 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
5025 return 1;
5026
Olivier Houchardfbc74e82017-11-24 16:54:05 +01005027 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01005028 if (clrall) {
5029 memset(&px->be_counters, 0, sizeof(px->be_counters));
5030 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
5031 }
5032 else {
5033 px->be_counters.conn_max = 0;
5034 px->be_counters.p.http.rps_max = 0;
5035 px->be_counters.sps_max = 0;
5036 px->be_counters.cps_max = 0;
5037 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005038 px->be_counters.qtime_max = 0;
5039 px->be_counters.ctime_max = 0;
5040 px->be_counters.dtime_max = 0;
5041 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005042
5043 px->fe_counters.conn_max = 0;
5044 px->fe_counters.p.http.rps_max = 0;
5045 px->fe_counters.sps_max = 0;
5046 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005047 }
5048
5049 for (sv = px->srv; sv; sv = sv->next)
5050 if (clrall)
5051 memset(&sv->counters, 0, sizeof(sv->counters));
5052 else {
5053 sv->counters.cur_sess_max = 0;
5054 sv->counters.nbpend_max = 0;
5055 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005056 sv->counters.qtime_max = 0;
5057 sv->counters.ctime_max = 0;
5058 sv->counters.dtime_max = 0;
5059 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005060 }
5061
5062 list_for_each_entry(li, &px->conf.listeners, by_fe)
5063 if (li->counters) {
5064 if (clrall)
5065 memset(li->counters, 0, sizeof(*li->counters));
5066 else
5067 li->counters->conn_max = 0;
5068 }
5069 }
5070
5071 global.cps_max = 0;
5072 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02005073 global.ssl_max = 0;
5074 global.ssl_fe_keys_max = 0;
5075 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005076
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005077 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5078 if (!mod->clearable && !clrall)
5079 continue;
5080
5081 for (px = proxies_list; px; px = px->next) {
5082 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
5083
5084 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
5085 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
5086 mod,
5087 mod->counters,
5088 mod->counters_size);
5089 }
5090
5091 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
5092 EXTRA_COUNTERS_INIT(px->extra_counters_be,
5093 mod,
5094 mod->counters,
5095 mod->counters_size);
5096 }
5097
5098 if (mod_cap & STATS_PX_CAP_SRV) {
5099 for (sv = px->srv; sv; sv = sv->next) {
5100 EXTRA_COUNTERS_INIT(sv->extra_counters,
5101 mod,
5102 mod->counters,
5103 mod->counters_size);
5104 }
5105 }
5106
5107 if (mod_cap & STATS_PX_CAP_LI) {
5108 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5109 EXTRA_COUNTERS_INIT(li->extra_counters,
5110 mod,
5111 mod->counters,
5112 mod->counters_size);
5113 }
5114 }
5115 }
5116 }
5117
Emeric Brunf8642ee2021-10-29 17:59:18 +02005118 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005119
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005120 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01005121 return 1;
5122}
5123
5124
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005125static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005126{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005127 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005128 int arg = 2;
5129
Willy Tarreau91cefca2022-05-03 17:08:29 +02005130 ctx->scope_str = 0;
5131 ctx->scope_len = 0;
5132 ctx->flags = 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01005133 ctx->field = 0; /* explicit default value */
Willy Tarreaud25fc792016-12-16 12:33:47 +01005134
Willy Tarreau2f397382019-10-09 11:43:59 +02005135 while (*args[arg]) {
5136 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005137 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005138 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005139 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005140 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005141 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02005142 else if (strcmp(args[arg], "float") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005143 ctx->flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02005144 arg++;
5145 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005146 return 0;
5147}
5148
5149
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005150static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01005151{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005152 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005153 int arg = 2;
5154
Willy Tarreau91cefca2022-05-03 17:08:29 +02005155 ctx->scope_str = 0;
5156 ctx->scope_len = 0;
5157 ctx->flags = STAT_SHNODE | STAT_SHDESC;
Willy Tarreau578d6e42019-10-09 11:00:22 +02005158
Willy Tarreau0698c802022-05-11 14:09:57 +02005159 if ((strm_li(appctx_strm(appctx))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005160 ctx->flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01005161
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005162 /* proxy is the default domain */
Willy Tarreau91cefca2022-05-03 17:08:29 +02005163 ctx->domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005164 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005165 ++args;
5166
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005167 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005168 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005169 } else if (strcmp(args[arg], "resolvers") == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02005170 ctx->domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005171 ++args;
5172 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005173 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005174 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005175 }
5176
Willy Tarreau91cefca2022-05-03 17:08:29 +02005177 if (ctx->domain == STATS_DOMAIN_PROXY
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005178 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005179 struct proxy *px;
5180
Willy Tarreau2f397382019-10-09 11:43:59 +02005181 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005182 if (px)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005183 ctx->iid = px->uuid;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005184 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02005185 ctx->iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005186
Willy Tarreau91cefca2022-05-03 17:08:29 +02005187 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02005188 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005189
Willy Tarreau91cefca2022-05-03 17:08:29 +02005190 ctx->flags |= STAT_BOUND;
5191 ctx->type = atoi(args[arg+1]);
5192 ctx->sid = atoi(args[arg+2]);
Willy Tarreau2f397382019-10-09 11:43:59 +02005193 arg += 3;
5194 }
5195
5196 while (*args[arg]) {
5197 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005198 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005199 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005200 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005201 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005202 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02005203 else if (strcmp(args[arg], "no-maint") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005204 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02005205 else if (strcmp(args[arg], "up") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005206 ctx->flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02005207 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01005208 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01005209
Willy Tarreau2b812e22016-11-22 16:18:05 +01005210 return 0;
5211}
5212
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005213static int cli_io_handler_dump_info(struct appctx *appctx)
5214{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005215 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005216 return stats_dump_info_to_buffer(appctx_sc(appctx));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005217}
5218
Willy Tarreau4596fe22022-05-17 19:07:51 +02005219/* This I/O handler runs as an applet embedded in a stream connector. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01005220 * used to send raw stats over a socket.
5221 */
5222static int cli_io_handler_dump_stat(struct appctx *appctx)
5223{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005224 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005225 return stats_dump_stat_to_buffer(appctx_sc(appctx), NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01005226}
5227
Simon Horman6f6bb382017-01-04 09:37:26 +01005228static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5229{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005230 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005231 return stats_dump_json_schema_to_buffer(appctx);
Simon Horman6f6bb382017-01-04 09:37:26 +01005232}
5233
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005234int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5235 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005236{
5237 struct stats_module *mod;
5238
5239 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5240
5241 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5242 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5243 continue;
5244
5245 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5246 }
5247
5248 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5249
5250 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5251 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5252 continue;
5253
5254 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5255 }
5256
5257 return 1;
5258
5259 alloc_failed:
5260 return 0;
5261}
5262
5263/* Initialize and allocate all extra counters for a proxy and its attached
5264 * servers/listeners with all already registered stats module
5265 */
5266int stats_allocate_proxy_counters(struct proxy *px)
5267{
5268 struct server *sv;
5269 struct listener *li;
5270
5271 if (px->cap & PR_CAP_FE) {
5272 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5273 COUNTERS_FE,
5274 STATS_PX_CAP_FE)) {
5275 return 0;
5276 }
5277 }
5278
5279 if (px->cap & PR_CAP_BE) {
5280 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5281 COUNTERS_BE,
5282 STATS_PX_CAP_BE)) {
5283 return 0;
5284 }
5285 }
5286
5287 for (sv = px->srv; sv; sv = sv->next) {
5288 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5289 COUNTERS_SV,
5290 STATS_PX_CAP_SRV)) {
5291 return 0;
5292 }
5293 }
5294
5295 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5296 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5297 COUNTERS_LI,
5298 STATS_PX_CAP_LI)) {
5299 return 0;
5300 }
5301 }
5302
5303 return 1;
5304}
5305
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005306void stats_register_module(struct stats_module *m)
5307{
5308 const uint8_t domain = stats_get_domain(m->domain_flags);
5309
Willy Tarreau2b718102021-04-21 07:32:39 +02005310 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005311 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005312}
5313
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005314static int allocate_stats_px_postcheck(void)
5315{
5316 struct stats_module *mod;
5317 size_t i = ST_F_TOTAL_FIELDS;
5318 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005319 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005320
5321 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5322
5323 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5324 if (!stat_f[STATS_DOMAIN_PROXY]) {
5325 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5326 err_code |= ERR_ALERT | ERR_FATAL;
5327 return err_code;
5328 }
5329
5330 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5331 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5332
5333 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5334 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5335 mod->stats,
5336 mod->stats_count * sizeof(struct name_desc));
5337 i += mod->stats_count;
5338 }
5339
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005340 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005341 if (!stats_allocate_proxy_counters(px)) {
5342 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5343 err_code |= ERR_ALERT | ERR_FATAL;
5344 return err_code;
5345 }
5346 }
5347
Christopher Fauletde79cd22021-01-06 07:41:56 +01005348 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005349
5350 return err_code;
5351}
5352
5353REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5354
Emeric Brunf8642ee2021-10-29 17:59:18 +02005355static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005356{
5357 struct stats_module *mod;
5358 size_t i = 0;
5359 int err_code = 0;
5360
Emeric Brunf8642ee2021-10-29 17:59:18 +02005361 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5362 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5363 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005364 err_code |= ERR_ALERT | ERR_FATAL;
5365 return err_code;
5366 }
5367
Emeric Brunf8642ee2021-10-29 17:59:18 +02005368 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5369 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005370 mod->stats,
5371 mod->stats_count * sizeof(struct name_desc));
5372 i += mod->stats_count;
5373 }
5374
Emeric Brunf8642ee2021-10-29 17:59:18 +02005375 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5376 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005377 err_code |= ERR_ALERT | ERR_FATAL;
5378 return err_code;
5379 }
5380
Christopher Fauletde79cd22021-01-06 07:41:56 +01005381 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005382
5383 return err_code;
5384}
5385
Emeric Brunf8642ee2021-10-29 17:59:18 +02005386REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005387
Christopher Fauletde79cd22021-01-06 07:41:56 +01005388static int allocate_stat_lines_per_thread(void)
5389{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005390 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005391
5392 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5393 const int domain = domains[i];
5394
5395 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5396 if (!stat_l[domain])
5397 return 0;
5398 }
5399 return 1;
5400}
5401
5402REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5403
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005404static int allocate_trash_counters(void)
5405{
5406 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005407 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005408 size_t max_counters_size = 0;
5409
5410 /* calculate the greatest counters used by any stats modules */
5411 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5412 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5413 max_counters_size = mod->counters_size > max_counters_size ?
5414 mod->counters_size : max_counters_size;
5415 }
5416 }
5417
5418 /* allocate the trash with the size of the greatest counters */
5419 if (max_counters_size) {
5420 trash_counters = malloc(max_counters_size);
5421 if (!trash_counters) {
5422 ha_alert("stats: cannot allocate trash counters for statistics\n");
5423 return 0;
5424 }
5425 }
5426
5427 return 1;
5428}
5429
5430REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5431
Christopher Fauletde79cd22021-01-06 07:41:56 +01005432static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005433{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005434 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005435
5436 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5437 const int domain = domains[i];
5438
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005439 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005440 }
5441}
5442
5443
5444REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5445
5446static void deinit_stats(void)
5447{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005448 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005449
5450 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5451 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005452
5453 if (stat_f[domain])
5454 free(stat_f[domain]);
5455 }
5456}
5457
5458REGISTER_POST_DEINIT(deinit_stats);
5459
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005460static void free_trash_counters(void)
5461{
5462 if (trash_counters)
5463 free(trash_counters);
5464}
5465
5466REGISTER_PER_THREAD_FREE(free_trash_counters);
5467
Willy Tarreau2b812e22016-11-22 16:18:05 +01005468/* register cli keywords */
5469static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005470 { { "clear", "counters", NULL }, "clear counters [all] : clear max statistics counters (or all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreau27456202021-05-08 07:54:24 +02005471 { { "show", "info", NULL }, "show info [desc|json|typed|float]* : report information about the running process", cli_parse_show_info, cli_io_handler_dump_info, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005472 { { "show", "stat", NULL }, "show stat [desc|json|no-maint|typed|up]*: report counters for each proxy and server", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
5473 { { "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 +01005474 {{},}
5475}};
5476
Willy Tarreau0108d902018-11-25 19:14:37 +01005477INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5478
William Lallemand74c24fb2016-11-21 17:18:36 +01005479struct applet http_stats_applet = {
5480 .obj_type = OBJ_TYPE_APPLET,
5481 .name = "<STATS>", /* used for logging */
5482 .fct = http_stats_io_handler,
5483 .release = NULL,
5484};
5485
William Lallemand74c24fb2016-11-21 17:18:36 +01005486/*
5487 * Local variables:
5488 * c-indent-level: 8
5489 * c-basic-offset: 8
5490 * End:
5491 */