blob: 0f2e027c165556da5713d75e61b4d3e26c4da0b4 [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) {
3231 if (htx_almost_full(htx))
3232 goto full;
3233 }
3234 else {
3235 if (buffer_almost_full(&rep->buf))
3236 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003237 }
3238
Willy Tarreau91cefca2022-05-03 17:08:29 +02003239 l = LIST_ELEM(ctx->obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003240 if (!l->counters)
3241 continue;
3242
Willy Tarreau91cefca2022-05-03 17:08:29 +02003243 if (ctx->flags & STAT_BOUND) {
3244 if (!(ctx->type & (1 << STATS_TYPE_SO)))
William Lallemand74c24fb2016-11-21 17:18:36 +01003245 break;
3246
Willy Tarreau91cefca2022-05-03 17:08:29 +02003247 if (ctx->sid != -1 && l->luid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003248 continue;
3249 }
3250
3251 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003252 if (stats_dump_li_stats(sc, px, l)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003253 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003254 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003255 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003256 if (ctx->field)
3257 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003258 }
Aurelien DARRAGON9b07d4f2023-02-03 11:43:05 +01003259 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003260 }
3261
Willy Tarreau91cefca2022-05-03 17:08:29 +02003262 ctx->obj2 = px->srv; /* may be NULL */
3263 ctx->px_st = STAT_PX_ST_SV;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003264 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003265
3266 case STAT_PX_ST_SV:
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003267 /* check for dump resumption */
3268 if (px_st == STAT_PX_ST_SV) {
3269 struct server *cur = ctx->obj2;
3270
3271 /* re-entrant dump */
3272 BUG_ON(!cur);
3273 if (cur->flags & SRV_F_DELETED) {
3274 /* the server could have been marked as deleted
3275 * between two dumping attempts, skip it.
3276 */
3277 cur = cur->next;
3278 }
3279 srv_drop(ctx->obj2); /* drop old srv taken on last dumping attempt */
3280 ctx->obj2 = cur; /* could be NULL */
3281 /* back to normal */
3282 }
3283
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003284 /* obj2 points to servers list as initialized above.
3285 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003286 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003287 * Temporarily increment its refcount to prevent its
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003288 * anticipated cleaning. Call srv_drop() to release it.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003289 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003290 for (; ctx->obj2 != NULL;
3291 ctx->obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003292
Willy Tarreau91cefca2022-05-03 17:08:29 +02003293 sv = ctx->obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003294 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003295
Christopher Fauletef779222018-10-31 08:47:01 +01003296 if (htx) {
3297 if (htx_almost_full(htx))
3298 goto full;
3299 }
3300 else {
3301 if (buffer_almost_full(&rep->buf))
3302 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003303 }
3304
Willy Tarreau91cefca2022-05-03 17:08:29 +02003305 if (ctx->flags & STAT_BOUND) {
3306 if (!(ctx->type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003307 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003308 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003309 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003310
Willy Tarreau91cefca2022-05-03 17:08:29 +02003311 if (ctx->sid != -1 && sv->puid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003312 continue;
3313 }
3314
Willy Tarreau3e320362020-10-23 17:28:57 +02003315 /* do not report disabled servers */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003316 if (ctx->flags & STAT_HIDE_MAINT &&
Willy Tarreau3e320362020-10-23 17:28:57 +02003317 sv->cur_admin & SRV_ADMF_MAINT) {
3318 continue;
3319 }
3320
William Lallemand74c24fb2016-11-21 17:18:36 +01003321 svs = sv;
3322 while (svs->track)
3323 svs = svs->track;
3324
3325 /* do not report servers which are DOWN and not changing state */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003326 if ((ctx->flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003327 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3328 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003329 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3330 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3331 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3332 continue;
3333 }
3334
Willy Tarreaucaff6312022-05-27 10:17:46 +02003335 if (stats_dump_sv_stats(sc, px, sv)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003336 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003337 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003338 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003339 if (ctx->field)
3340 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003341 }
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003342 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003343 } /* for sv */
3344
Willy Tarreau91cefca2022-05-03 17:08:29 +02003345 ctx->px_st = STAT_PX_ST_BE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003346 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003347
3348 case STAT_PX_ST_BE:
3349 /* print the backend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003350 if (stats_dump_be_stats(sc, px)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003351 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003352 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003353 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003354 if (ctx->field)
3355 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003356 }
3357
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003358 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003359 ctx->px_st = STAT_PX_ST_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003360 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003361
3362 case STAT_PX_ST_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003363 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003364 stats_dump_html_px_end(sc, px);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003365 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003366 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003367 }
3368
Willy Tarreau91cefca2022-05-03 17:08:29 +02003369 ctx->px_st = STAT_PX_ST_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003370 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003371
3372 case STAT_PX_ST_FIN:
3373 return 1;
3374
3375 default:
3376 /* unknown state, we should put an abort() here ! */
3377 return 1;
3378 }
Christopher Fauletef779222018-10-31 08:47:01 +01003379
3380 full:
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003381 /* restore previous field */
3382 ctx->field = current_field;
Christopher Fauletef779222018-10-31 08:47:01 +01003383 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003384}
3385
Christopher Fauleta8b76842022-12-16 15:08:36 +01003386/* Dumps the HTTP stats head block to the local trash buffer for and uses the
3387 * per-uri parameters <uri>. The caller is responsible for clearing the local
3388 * trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003389 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003390static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003391{
Willy Tarreau91cefca2022-05-03 17:08:29 +02003392 struct show_stat_ctx *ctx = appctx->svcctx;
3393
William Lallemand74c24fb2016-11-21 17:18:36 +01003394 /* WARNING! This must fit in the first buffer !!! */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003395 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003396 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3397 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3398 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003399 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003400 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3401 "<style type=\"text/css\"><!--\n"
3402 "body {"
3403 " font-family: arial, helvetica, sans-serif;"
3404 " font-size: 12px;"
3405 " font-weight: normal;"
3406 " color: black;"
3407 " background: white;"
3408 "}\n"
3409 "th,td {"
3410 " font-size: 10px;"
3411 "}\n"
3412 "h1 {"
3413 " font-size: x-large;"
3414 " margin-bottom: 0.5em;"
3415 "}\n"
3416 "h2 {"
3417 " font-family: helvetica, arial;"
3418 " font-size: x-large;"
3419 " font-weight: bold;"
3420 " font-style: italic;"
3421 " color: #6020a0;"
3422 " margin-top: 0em;"
3423 " margin-bottom: 0em;"
3424 "}\n"
3425 "h3 {"
3426 " font-family: helvetica, arial;"
3427 " font-size: 16px;"
3428 " font-weight: bold;"
3429 " color: #b00040;"
3430 " background: #e8e8d0;"
3431 " margin-top: 0em;"
3432 " margin-bottom: 0em;"
3433 "}\n"
3434 "li {"
3435 " margin-top: 0.25em;"
3436 " margin-right: 2em;"
3437 "}\n"
3438 ".hr {margin-top: 0.25em;"
3439 " border-color: black;"
3440 " border-bottom-style: solid;"
3441 "}\n"
3442 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3443 ".total {background: #20D0D0;color: #ffff80;}\n"
3444 ".frontend {background: #e8e8d0;}\n"
3445 ".socket {background: #d0d0d0;}\n"
3446 ".backend {background: #e8e8d0;}\n"
3447 ".active_down {background: #ff9090;}\n"
3448 ".active_going_up {background: #ffd020;}\n"
3449 ".active_going_down {background: #ffffa0;}\n"
3450 ".active_up {background: #c0ffc0;}\n"
3451 ".active_nolb {background: #20a0ff;}\n"
3452 ".active_draining {background: #20a0FF;}\n"
3453 ".active_no_check {background: #e0e0e0;}\n"
3454 ".backup_down {background: #ff9090;}\n"
3455 ".backup_going_up {background: #ff80ff;}\n"
3456 ".backup_going_down {background: #c060ff;}\n"
3457 ".backup_up {background: #b0d0ff;}\n"
3458 ".backup_nolb {background: #90b0e0;}\n"
3459 ".backup_draining {background: #cc9900;}\n"
3460 ".backup_no_check {background: #e0e0e0;}\n"
3461 ".maintain {background: #c07820;}\n"
3462 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3463 "\n"
3464 "a.px:link {color: #ffff40; text-decoration: none;}"
3465 "a.px:visited {color: #ffff40; text-decoration: none;}"
3466 "a.px:hover {color: #ffffff; text-decoration: none;}"
3467 "a.lfsb:link {color: #000000; text-decoration: none;}"
3468 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3469 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3470 "\n"
3471 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3472 "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"
3473 "table.tbl td.ac { text-align: center;}\n"
3474 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3475 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3476 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3477 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3478 "\n"
3479 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3480 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3481 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3482 "table.det { border-collapse: collapse; border-style: none; }\n"
3483 "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"
3484 "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"
3485 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003486 "div.tips {\n"
3487 " display:block;\n"
3488 " visibility:hidden;\n"
3489 " z-index:2147483647;\n"
3490 " position:absolute;\n"
3491 " padding:2px 4px 3px;\n"
3492 " background:#f0f060; color:#000000;\n"
3493 " border:1px solid #7040c0;\n"
3494 " white-space:nowrap;\n"
3495 " font-style:normal;font-size:11px;font-weight:normal;\n"
3496 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3497 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3498 "}\n"
3499 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003500 "@media (prefers-color-scheme: dark) {\n"
3501 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3502 " h1 { color: #a265e0!important; }\n"
3503 " h2 { color: #a265e0; }\n"
3504 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3505 " a { color: #3391ff; }\n"
3506 " input { background-color: #2f3437; }\n"
3507 " .hr { border-color: #8c8273; }\n"
3508 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3509 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003510 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003511 " .backend {background: #2f3437;}\n"
3512 " .active_down {background: #760000;}\n"
3513 " .active_going_up {background: #b99200;}\n"
3514 " .active_going_down {background: #6c6c00;}\n"
3515 " .active_up {background: #165900;}\n"
3516 " .active_nolb {background: #006ab9;}\n"
3517 " .active_draining {background: #006ab9;}\n"
3518 " .active_no_check {background: #2a2d2f;}\n"
3519 " .backup_down {background: #760000;}\n"
3520 " .backup_going_up {background: #7f007f;}\n"
3521 " .backup_going_down {background: #580092;}\n"
3522 " .backup_up {background: #2e3234;}\n"
3523 " .backup_nolb {background: #1e3c6a;}\n"
3524 " .backup_draining {background: #a37a00;}\n"
3525 " .backup_no_check {background: #2a2d2f;}\n"
3526 " .maintain {background: #9a601a;}\n"
3527 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3528 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3529 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3530 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3531 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3532 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3533 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003534 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003535 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3536 " table.tbl th { border-color: #808080; }\n"
3537 " table.tbl td { border-color: #808080; }\n"
3538 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3539 " div.tips {\n"
3540 " background:#8e8e0d;\n"
3541 " color:#e8e6e3;\n"
3542 " border-color: #4e2c86;\n"
3543 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3544 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3545 " box-shadow: #60686c 2px 2px 3px;\n"
3546 " }\n"
3547 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003548 "-->\n"
3549 "</style></head>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003550 (ctx->flags & STAT_SHNODE) ? " on " : "",
3551 (ctx->flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003552 );
3553}
3554
Christopher Fauleta8b76842022-12-16 15:08:36 +01003555/* Dumps the HTML stats information block to the local trash buffer for and uses
3556 * the state from stream connector <sc> and per-uri parameters <uri>. The caller
3557 * is responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003558 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003559static void stats_dump_html_info(struct stconn *sc, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003560{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003561 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003562 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc05d30e2023-04-28 14:50:29 +02003563 unsigned int up = ns_to_sec(now_ns - start_time_ns);
William Lallemand74c24fb2016-11-21 17:18:36 +01003564 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02003565 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Willy Tarreau6be8d092023-01-12 16:08:41 +01003566 unsigned long long bps;
3567 int thr;
3568
3569 for (bps = thr = 0; thr < global.nbthread; thr++)
3570 bps += 32ULL * read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
Willy Tarreau1713c032019-05-23 12:23:55 +02003571
3572 /* Turn the bytes per second to bits per second and take care of the
3573 * usual ethernet overhead in order to help figure how far we are from
3574 * interface saturation since it's the only case which usually matters.
3575 * For this we count the total size of an Ethernet frame on the wire
3576 * including preamble and IFG (1538) for the largest TCP segment it
3577 * transports (1448 with TCP timestamps). This is not valid for smaller
3578 * packets (under-estimated), but it gives a reasonably accurate
3579 * estimation of how far we are from uplink saturation.
3580 */
3581 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003582
3583 /* WARNING! this has to fit the first packet too.
3584 * We are around 3.5 kB, add adding entries will
3585 * become tricky if we want to support 4kB buffers !
3586 */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003587 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003588 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3589 PRODUCT_NAME "%s</a></h1>\n"
3590 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3591 "<hr width=\"100%%\" class=\"hr\">\n"
3592 "<h3>&gt; General process information</h3>\n"
3593 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003594 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003595 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3596 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3597 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003598 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003599 "Running tasks: %d/%d; idle = %d %%<br>\n"
3600 "</td><td align=\"center\" nowrap>\n"
3601 "<table class=\"lgd\"><tr>\n"
3602 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3603 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3604 "</tr><tr>\n"
3605 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3606 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3607 "</tr><tr>\n"
3608 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3609 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3610 "</tr><tr>\n"
3611 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3612 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3613 "</tr><tr>\n"
3614 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3615 "</tr><tr>\n"
3616 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3617 "</tr></table>\n"
3618 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3619 "</td>"
3620 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3621 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3622 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003623 (ctx->flags & STAT_HIDEVER) ? "" : (stats_version_string),
3624 pid, (ctx->flags & STAT_SHNODE) ? " on " : "",
3625 (ctx->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3626 (ctx->flags & STAT_SHDESC) ? ": " : "",
3627 (ctx->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003628 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003629 up / 86400, (up % 86400) / 3600,
3630 (up % 3600) / 60, (up % 60),
3631 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3632 global.rlimit_memmax ? " MB" : "",
3633 global.rlimit_nofile,
3634 global.maxsock, global.maxconn, global.maxpipes,
3635 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003636 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3637 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003638 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003639 );
3640
Willy Tarreau91cefca2022-05-03 17:08:29 +02003641 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3642 memcpy(scope_txt, scope_ptr, ctx->scope_len);
3643 scope_txt[ctx->scope_len] = '\0';
William Lallemand74c24fb2016-11-21 17:18:36 +01003644
Christopher Fauleta8b76842022-12-16 15:08:36 +01003645 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003646 "<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 +02003647 (ctx->scope_len > 0) ? scope_txt : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003648 STAT_SCOPE_TXT_MAXLEN);
3649
Willy Tarreau91cefca2022-05-03 17:08:29 +02003650 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003651 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003652 if (ctx->scope_len) {
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003653 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02003654 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3655 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003656 }
3657
Willy Tarreau91cefca2022-05-03 17:08:29 +02003658 if (ctx->flags & STAT_HIDE_DOWN)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003659 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003660 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3661 uri->uri_prefix,
3662 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003663 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003664 scope_txt);
3665 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003666 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003667 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3668 uri->uri_prefix,
3669 ";up",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003670 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003671 scope_txt);
3672
3673 if (uri->refresh > 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003674 if (ctx->flags & STAT_NO_REFRESH)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003675 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003676 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3677 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003678 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003679 "",
3680 scope_txt);
3681 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003682 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003683 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3684 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003685 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003686 ";norefresh",
3687 scope_txt);
3688 }
3689
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\">Refresh now</a><br>\n",
3692 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003693 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3694 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003695 scope_txt);
3696
Christopher Fauleta8b76842022-12-16 15:08:36 +01003697 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003698 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3699 uri->uri_prefix,
3700 (uri->refresh > 0) ? ";norefresh" : "",
3701 scope_txt);
3702
Christopher Fauleta8b76842022-12-16 15:08:36 +01003703 chunk_appendf(&trash_chunk,
Christopher Faulet6338a082019-09-09 15:50:54 +02003704 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3705 uri->uri_prefix,
3706 (uri->refresh > 0) ? ";norefresh" : "",
3707 scope_txt, uri->uri_prefix);
3708
Christopher Fauleta8b76842022-12-16 15:08:36 +01003709 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003710 "</ul></td>"
3711 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3712 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3713 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3714 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3715 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3716 "</ul>"
3717 "</td>"
3718 "</tr></table>\n"
3719 ""
3720 );
3721
Willy Tarreau91cefca2022-05-03 17:08:29 +02003722 if (ctx->st_code) {
3723 switch (ctx->st_code) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003724 case STAT_STATUS_DONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003725 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003726 "<p><div class=active_up>"
3727 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3728 "Action processed successfully."
3729 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003730 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3731 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003732 scope_txt);
3733 break;
3734 case STAT_STATUS_NONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003735 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003736 "<p><div class=active_going_down>"
3737 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3738 "Nothing has changed."
3739 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003740 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3741 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003742 scope_txt);
3743 break;
3744 case STAT_STATUS_PART:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003745 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003746 "<p><div class=active_going_down>"
3747 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3748 "Action partially processed.<br>"
3749 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3750 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003751 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3752 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003753 scope_txt);
3754 break;
3755 case STAT_STATUS_ERRP:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003756 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003757 "<p><div class=active_down>"
3758 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3759 "Action not processed because of invalid parameters."
3760 "<ul>"
3761 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003762 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003763 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3764 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3765 "</ul>"
3766 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003767 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3768 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003769 scope_txt);
3770 break;
3771 case STAT_STATUS_EXCD:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003772 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003773 "<p><div class=active_down>"
3774 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3775 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3776 "You should retry with less servers at a time.</b>"
3777 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003778 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3779 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003780 scope_txt);
3781 break;
3782 case STAT_STATUS_DENY:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003783 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003784 "<p><div class=active_down>"
3785 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3786 "<b>Action denied.</b>"
3787 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003788 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3789 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003790 scope_txt);
3791 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003792 case STAT_STATUS_IVAL:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003793 chunk_appendf(&trash_chunk,
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003794 "<p><div class=active_down>"
3795 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3796 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3797 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003798 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3799 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003800 scope_txt);
3801 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003802 default:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003803 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003804 "<p><div class=active_no_check>"
3805 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3806 "Unexpected result."
3807 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003808 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3809 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003810 scope_txt);
3811 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003812 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003813 }
3814}
3815
Christopher Fauleta8b76842022-12-16 15:08:36 +01003816/* Dumps the HTML stats trailer block to the local trash buffer. The caller is
3817 * responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003818 */
3819static void stats_dump_html_end()
3820{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003821 chunk_appendf(&trash_chunk, "</body></html>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003822}
3823
Christopher Fauleta8b76842022-12-16 15:08:36 +01003824/* Dumps the stats JSON header to the local trash buffer buffer which. The
3825 * caller is responsible for clearing it if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003826 */
3827static void stats_dump_json_header()
3828{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003829 chunk_strcat(&trash_chunk, "[");
Simon Horman05ee2132017-01-04 09:37:25 +01003830}
3831
3832
Christopher Fauleta8b76842022-12-16 15:08:36 +01003833/* Dumps the JSON stats trailer block to the local trash buffer. The caller is
3834 * responsible for clearing the local trash buffer if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003835 */
3836static void stats_dump_json_end()
3837{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003838 chunk_strcat(&trash_chunk, "]\n");
Simon Horman05ee2132017-01-04 09:37:25 +01003839}
3840
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003841/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3842 * a pointer to the current server/listener.
3843 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003844static int stats_dump_proxies(struct stconn *sc,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003845 struct htx *htx,
3846 struct uri_auth *uri)
3847{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003848 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003849 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003850 struct channel *rep = sc_ic(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003851 struct proxy *px;
3852
3853 /* dump proxies */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003854 while (ctx->obj1) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003855 if (htx) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003856 if (htx_almost_full(htx)) {
3857 sc_need_room(sc, htx->size / 2);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003858 goto full;
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003859 }
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003860 }
3861 else {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003862 if (buffer_almost_full(&rep->buf)) {
3863 sc_need_room(sc, b_size(&rep->buf) / 2);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003864 goto full;
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003865 }
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003866 }
3867
Willy Tarreau91cefca2022-05-03 17:08:29 +02003868 px = ctx->obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003869 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003870 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003871 * This change allows retrieving stats from "old" proxies after a reload.
3872 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003873 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003874 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003875 if (stats_dump_proxy_to_buffer(sc, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003876 return 0;
3877 }
3878
Willy Tarreau91cefca2022-05-03 17:08:29 +02003879 ctx->obj1 = px->next;
3880 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003881 ctx->field = 0;
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003882 }
3883
3884 return 1;
3885
3886 full:
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003887 return 0;
3888}
3889
Willy Tarreau4596fe22022-05-17 19:07:51 +02003890/* This function dumps statistics onto the stream connector's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003891 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3892 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3893 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3894 * and the stream must be closed, or -1 in case of any error. This function is
3895 * used by both the CLI and the HTTP handlers.
3896 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003897static int stats_dump_stat_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003898 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003899{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003900 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003901 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003902 enum stats_domain domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003903
Christopher Fauleta8b76842022-12-16 15:08:36 +01003904 chunk_reset(&trash_chunk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003905
Willy Tarreau41f88522022-05-03 18:39:27 +02003906 switch (ctx->state) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003907 case STAT_STATE_INIT:
3908 ctx->state = STAT_STATE_HEAD; /* let's start producing data */
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003909 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003910
Willy Tarreau6ef16482022-05-06 18:07:53 +02003911 case STAT_STATE_HEAD:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003912 if (ctx->flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003913 stats_dump_html_head(appctx, uri);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003914 else if (ctx->flags & STAT_JSON_SCHM)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003915 stats_dump_json_schema(&trash_chunk);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003916 else if (ctx->flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003917 stats_dump_json_header();
Willy Tarreau91cefca2022-05-03 17:08:29 +02003918 else if (!(ctx->flags & STAT_FMT_TYPED))
3919 stats_dump_csv_header(ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003920
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003921 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003922 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003923
Willy Tarreau91cefca2022-05-03 17:08:29 +02003924 if (ctx->flags & STAT_JSON_SCHM) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003925 ctx->state = STAT_STATE_FIN;
Christopher Faulet6338a082019-09-09 15:50:54 +02003926 return 1;
3927 }
Willy Tarreau6ef16482022-05-06 18:07:53 +02003928 ctx->state = STAT_STATE_INFO;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003929 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003930
Willy Tarreau6ef16482022-05-06 18:07:53 +02003931 case STAT_STATE_INFO:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003932 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003933 stats_dump_html_info(sc, uri);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003934 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003935 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003936 }
3937
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003938 if (domain == STATS_DOMAIN_PROXY)
Willy Tarreau91cefca2022-05-03 17:08:29 +02003939 ctx->obj1 = proxies_list;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003940
Willy Tarreau91cefca2022-05-03 17:08:29 +02003941 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003942 ctx->field = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003943 ctx->state = STAT_STATE_LIST;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003944 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003945
Willy Tarreau6ef16482022-05-06 18:07:53 +02003946 case STAT_STATE_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003947 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003948 case STATS_DOMAIN_RESOLVERS:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003949 if (!stats_dump_resolvers(sc, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003950 stat_count[domain],
3951 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003952 return 0;
3953 }
3954 break;
3955
3956 case STATS_DOMAIN_PROXY:
3957 default:
3958 /* dump proxies */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003959 if (!stats_dump_proxies(sc, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003960 return 0;
3961 break;
3962 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003963
Willy Tarreau6ef16482022-05-06 18:07:53 +02003964 ctx->state = STAT_STATE_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003965 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003966
Willy Tarreau6ef16482022-05-06 18:07:53 +02003967 case STAT_STATE_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003968 if (ctx->flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3969 if (ctx->flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01003970 stats_dump_html_end();
3971 else
3972 stats_dump_json_end();
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003973 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003974 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003975 }
3976
Willy Tarreau6ef16482022-05-06 18:07:53 +02003977 ctx->state = STAT_STATE_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003978 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003979
Willy Tarreau6ef16482022-05-06 18:07:53 +02003980 case STAT_STATE_FIN:
William Lallemand74c24fb2016-11-21 17:18:36 +01003981 return 1;
3982
3983 default:
3984 /* unknown state ! */
Willy Tarreau6ef16482022-05-06 18:07:53 +02003985 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01003986 return -1;
3987 }
Christopher Fauletef779222018-10-31 08:47:01 +01003988
3989 full:
Christopher Fauletef779222018-10-31 08:47:01 +01003990 return 0;
3991
William Lallemand74c24fb2016-11-21 17:18:36 +01003992}
3993
3994/* We reached the stats page through a POST request. The appctx is
3995 * expected to have already been allocated by the caller.
3996 * Parse the posted data and enable/disable servers if necessary.
3997 * Returns 1 if request was parsed or zero if it needs more data.
3998 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003999static int stats_process_http_post(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01004000{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004001 struct stream *s = __sc_strm(sc);
4002 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004003 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01004004
4005 struct proxy *px = NULL;
4006 struct server *sv = NULL;
4007
4008 char key[LINESIZE];
4009 int action = ST_ADM_ACTION_NONE;
4010 int reprocess = 0;
4011
4012 int total_servers = 0;
4013 int altered_servers = 0;
4014
4015 char *first_param, *cur_param, *next_param, *end_params;
4016 char *st_cur_param = NULL;
4017 char *st_next_param = NULL;
4018
Christopher Fauleta3618372018-12-13 21:59:56 +01004019 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01004020
Christopher Fauletb7f88902019-07-15 21:56:43 +02004021 struct htx *htx = htxbuf(&s->req.buf);
4022 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01004023
Christopher Fauletb7f88902019-07-15 21:56:43 +02004024 /* we need more data */
4025 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
4026 /* check if we can receive more */
4027 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004028 ctx->st_code = STAT_STATUS_EXCD;
Christopher Fauletb7f88902019-07-15 21:56:43 +02004029 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01004030 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004031 goto wait;
4032 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004033
Christopher Fauletb7f88902019-07-15 21:56:43 +02004034 /* The request was fully received. Copy data */
4035 blk = htx_get_head_blk(htx);
4036 while (blk) {
4037 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004038
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004039 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02004040 break;
4041 if (type == HTX_BLK_DATA) {
4042 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004043
Christopher Fauletb7f88902019-07-15 21:56:43 +02004044 if (!chunk_memcat(temp, v.ptr, v.len)) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004045 ctx->st_code = STAT_STATUS_EXCD;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004046 goto out;
4047 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004048 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004049 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01004050 }
4051
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004052 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01004053 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01004054 cur_param = next_param = end_params;
4055 *end_params = '\0';
4056
Willy Tarreau91cefca2022-05-03 17:08:29 +02004057 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004058
4059 /*
4060 * Parse the parameters in reverse order to only store the last value.
4061 * From the html form, the backend and the action are at the end.
4062 */
4063 while (cur_param > first_param) {
4064 char *value;
4065 int poffset, plen;
4066
4067 cur_param--;
4068
4069 if ((*cur_param == '&') || (cur_param == first_param)) {
4070 reprocess_servers:
4071 /* Parse the key */
4072 poffset = (cur_param != first_param ? 1 : 0);
4073 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4074 if ((plen > 0) && (plen <= sizeof(key))) {
4075 strncpy(key, cur_param + poffset, plen);
4076 key[plen - 1] = '\0';
4077 } else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004078 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004079 goto out;
4080 }
4081
4082 /* Parse the value */
4083 value = key;
4084 while (*value != '\0' && *value != '=') {
4085 value++;
4086 }
4087 if (*value == '=') {
4088 /* Ok, a value is found, we can mark the end of the key */
4089 *value++ = '\0';
4090 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02004091 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01004092 break;
4093
4094 /* Now we can check the key to see what to do */
4095 if (!px && (strcmp(key, "b") == 0)) {
4096 if ((px = proxy_be_by_name(value)) == NULL) {
4097 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004098 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004099 goto out;
4100 }
4101 }
4102 else if (!action && (strcmp(key, "action") == 0)) {
4103 if (strcmp(value, "ready") == 0) {
4104 action = ST_ADM_ACTION_READY;
4105 }
4106 else if (strcmp(value, "drain") == 0) {
4107 action = ST_ADM_ACTION_DRAIN;
4108 }
4109 else if (strcmp(value, "maint") == 0) {
4110 action = ST_ADM_ACTION_MAINT;
4111 }
4112 else if (strcmp(value, "shutdown") == 0) {
4113 action = ST_ADM_ACTION_SHUTDOWN;
4114 }
4115 else if (strcmp(value, "dhlth") == 0) {
4116 action = ST_ADM_ACTION_DHLTH;
4117 }
4118 else if (strcmp(value, "ehlth") == 0) {
4119 action = ST_ADM_ACTION_EHLTH;
4120 }
4121 else if (strcmp(value, "hrunn") == 0) {
4122 action = ST_ADM_ACTION_HRUNN;
4123 }
4124 else if (strcmp(value, "hnolb") == 0) {
4125 action = ST_ADM_ACTION_HNOLB;
4126 }
4127 else if (strcmp(value, "hdown") == 0) {
4128 action = ST_ADM_ACTION_HDOWN;
4129 }
4130 else if (strcmp(value, "dagent") == 0) {
4131 action = ST_ADM_ACTION_DAGENT;
4132 }
4133 else if (strcmp(value, "eagent") == 0) {
4134 action = ST_ADM_ACTION_EAGENT;
4135 }
4136 else if (strcmp(value, "arunn") == 0) {
4137 action = ST_ADM_ACTION_ARUNN;
4138 }
4139 else if (strcmp(value, "adown") == 0) {
4140 action = ST_ADM_ACTION_ADOWN;
4141 }
4142 /* else these are the old supported methods */
4143 else if (strcmp(value, "disable") == 0) {
4144 action = ST_ADM_ACTION_DISABLE;
4145 }
4146 else if (strcmp(value, "enable") == 0) {
4147 action = ST_ADM_ACTION_ENABLE;
4148 }
4149 else if (strcmp(value, "stop") == 0) {
4150 action = ST_ADM_ACTION_STOP;
4151 }
4152 else if (strcmp(value, "start") == 0) {
4153 action = ST_ADM_ACTION_START;
4154 }
4155 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004156 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004157 goto out;
4158 }
4159 }
4160 else if (strcmp(key, "s") == 0) {
4161 if (!(px && action)) {
4162 /*
4163 * Indicates that we'll need to reprocess the parameters
4164 * as soon as backend and action are known
4165 */
4166 if (!reprocess) {
4167 st_cur_param = cur_param;
4168 st_next_param = next_param;
4169 }
4170 reprocess = 1;
4171 }
4172 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004173 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004174 switch (action) {
4175 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004176 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004177 altered_servers++;
4178 total_servers++;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004179 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, SRV_ADM_STCHGC_STATS_DISABLE);
William Lallemand74c24fb2016-11-21 17:18:36 +01004180 }
4181 break;
4182 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004183 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004184 altered_servers++;
4185 total_servers++;
4186 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4187 }
4188 break;
4189 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004190 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004191 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, SRV_ADM_STCHGC_STATS_STOP);
William Lallemand74c24fb2016-11-21 17:18:36 +01004192 altered_servers++;
4193 total_servers++;
4194 }
4195 break;
4196 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004197 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004198 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4199 altered_servers++;
4200 total_servers++;
4201 }
4202 break;
4203 case ST_ADM_ACTION_DHLTH:
4204 if (sv->check.state & CHK_ST_CONFIGURED) {
4205 sv->check.state &= ~CHK_ST_ENABLED;
4206 altered_servers++;
4207 total_servers++;
4208 }
4209 break;
4210 case ST_ADM_ACTION_EHLTH:
4211 if (sv->check.state & CHK_ST_CONFIGURED) {
4212 sv->check.state |= CHK_ST_ENABLED;
4213 altered_servers++;
4214 total_servers++;
4215 }
4216 break;
4217 case ST_ADM_ACTION_HRUNN:
4218 if (!(sv->track)) {
4219 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004220 srv_set_running(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004221 altered_servers++;
4222 total_servers++;
4223 }
4224 break;
4225 case ST_ADM_ACTION_HNOLB:
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_stopping(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_HDOWN:
4234 if (!(sv->track)) {
4235 sv->check.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004236 srv_set_stopped(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_DAGENT:
4242 if (sv->agent.state & CHK_ST_CONFIGURED) {
4243 sv->agent.state &= ~CHK_ST_ENABLED;
4244 altered_servers++;
4245 total_servers++;
4246 }
4247 break;
4248 case ST_ADM_ACTION_EAGENT:
4249 if (sv->agent.state & CHK_ST_CONFIGURED) {
4250 sv->agent.state |= CHK_ST_ENABLED;
4251 altered_servers++;
4252 total_servers++;
4253 }
4254 break;
4255 case ST_ADM_ACTION_ARUNN:
4256 if (sv->agent.state & CHK_ST_ENABLED) {
4257 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004258 srv_set_running(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004259 altered_servers++;
4260 total_servers++;
4261 }
4262 break;
4263 case ST_ADM_ACTION_ADOWN:
4264 if (sv->agent.state & CHK_ST_ENABLED) {
4265 sv->agent.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004266 srv_set_stopped(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_READY:
4272 srv_adm_set_ready(sv);
4273 altered_servers++;
4274 total_servers++;
4275 break;
4276 case ST_ADM_ACTION_DRAIN:
4277 srv_adm_set_drain(sv);
4278 altered_servers++;
4279 total_servers++;
4280 break;
4281 case ST_ADM_ACTION_MAINT:
4282 srv_adm_set_maint(sv);
4283 altered_servers++;
4284 total_servers++;
4285 break;
4286 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004287 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004288 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004289 altered_servers++;
4290 total_servers++;
4291 }
4292 break;
4293 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004294 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004295 } else {
4296 /* the server name is unknown or ambiguous (duplicate names) */
4297 total_servers++;
4298 }
4299 }
4300 if (reprocess && px && action) {
4301 /* Now, we know the backend and the action chosen by the user.
4302 * We can safely restart from the first server parameter
4303 * to reprocess them
4304 */
4305 cur_param = st_cur_param;
4306 next_param = st_next_param;
4307 reprocess = 0;
4308 goto reprocess_servers;
4309 }
4310
4311 next_param = cur_param;
4312 }
4313 }
4314
4315 if (total_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004316 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004317 }
4318 else if (altered_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004319 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004320 }
4321 else if (altered_servers == total_servers) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004322 ctx->st_code = STAT_STATUS_DONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004323 }
4324 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004325 ctx->st_code = STAT_STATUS_PART;
William Lallemand74c24fb2016-11-21 17:18:36 +01004326 }
4327 out:
4328 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004329 wait:
Willy Tarreau91cefca2022-05-03 17:08:29 +02004330 ctx->st_code = STAT_STATUS_NONE;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004331 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004332}
4333
4334
Willy Tarreaucaff6312022-05-27 10:17:46 +02004335static int stats_send_http_headers(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004336{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004337 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004338 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004339 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004340 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004341 struct htx_sl *sl;
4342 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004343
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004344 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);
4345 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4346 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004347 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004348 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004349
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004350 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004351 goto full;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004352 if (ctx->flags & STAT_FMT_HTML) {
Christopher Fauletef779222018-10-31 08:47:01 +01004353 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4354 goto full;
4355 }
Willy Tarreau91cefca2022-05-03 17:08:29 +02004356 else if (ctx->flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004357 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4358 goto full;
4359 }
Christopher Fauletef779222018-10-31 08:47:01 +01004360 else {
4361 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4362 goto full;
4363 }
4364
Willy Tarreau91cefca2022-05-03 17:08:29 +02004365 if (uri->refresh > 0 && !(ctx->flags & STAT_NO_REFRESH)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004366 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004367 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004368 goto full;
4369 }
4370
Willy Tarreau91cefca2022-05-03 17:08:29 +02004371 if (ctx->flags & STAT_CHUNKED) {
Christopher Fauletef779222018-10-31 08:47:01 +01004372 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4373 goto full;
4374 }
4375
4376 if (!htx_add_endof(htx, HTX_BLK_EOH))
4377 goto full;
4378
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004379 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004380 return 1;
4381
4382 full:
4383 htx_reset(htx);
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004384 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004385 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004386}
4387
Christopher Fauletef779222018-10-31 08:47:01 +01004388
Willy Tarreaucaff6312022-05-27 10:17:46 +02004389static int stats_send_http_redirect(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004390{
4391 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02004392 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004393 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004394 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004395 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004396 struct htx_sl *sl;
4397 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004398
Willy Tarreau91cefca2022-05-03 17:08:29 +02004399 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauletef779222018-10-31 08:47:01 +01004400 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004401 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004402 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004403
Willy Tarreaufc458ec2023-04-07 18:11:39 +02004404 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02004405 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
4406 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004407 }
4408
4409 /* We don't want to land on the posted stats page because a refresh will
4410 * repost the data. We don't want this to happen on accident so we redirect
4411 * the browse to the stats page with a GET.
4412 */
4413 chunk_printf(&trash, "%s;st=%s%s%s%s",
4414 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02004415 ((ctx->st_code > STAT_STATUS_INIT) &&
4416 (ctx->st_code < STAT_STATUS_SIZE) &&
4417 stat_status_codes[ctx->st_code]) ?
4418 stat_status_codes[ctx->st_code] :
Christopher Fauletef779222018-10-31 08:47:01 +01004419 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau91cefca2022-05-03 17:08:29 +02004420 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
4421 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Fauletef779222018-10-31 08:47:01 +01004422 scope_txt);
4423
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004424 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4425 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4426 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004427 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004428 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004429
4430 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004431 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4432 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4433 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4434 goto full;
4435
4436 if (!htx_add_endof(htx, HTX_BLK_EOH))
4437 goto full;
4438
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004439 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004440 return 1;
4441
4442full:
4443 htx_reset(htx);
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004444 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004445 return 0;
4446}
William Lallemand74c24fb2016-11-21 17:18:36 +01004447
Simon Horman05ee2132017-01-04 09:37:25 +01004448
Willy Tarreau4596fe22022-05-17 19:07:51 +02004449/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004450 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4451 * appctx->st0 contains the operation in progress (dump, done). The handler
4452 * automatically unregisters itself once transfer is complete.
4453 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004454static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004455{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004456 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02004457 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004458 struct stream *s = __sc_strm(sc);
4459 struct channel *req = sc_oc(sc);
4460 struct channel *res = sc_ic(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004461 struct htx *req_htx, *res_htx;
4462
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004463 /* only proxy stats are available via http */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004464 ctx->domain = STATS_DOMAIN_PROXY;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004465
Christopher Fauletef779222018-10-31 08:47:01 +01004466 res_htx = htx_from_buf(&res->buf);
4467
Christopher Faulet9837bd82023-04-11 07:41:30 +02004468 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) {
4469 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004470 goto out;
Christopher Faulet9837bd82023-04-11 07:41:30 +02004471 }
Christopher Fauletef779222018-10-31 08:47:01 +01004472
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004473 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004474 if (!b_size(&res->buf)) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004475 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004476 goto out;
4477 }
4478
Christopher Fauletef779222018-10-31 08:47:01 +01004479 /* all states are processed in sequence */
4480 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004481 if (stats_send_http_headers(sc, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004482 if (s->txn->meth == HTTP_METH_HEAD)
4483 appctx->st0 = STAT_HTTP_DONE;
4484 else
4485 appctx->st0 = STAT_HTTP_DUMP;
4486 }
4487 }
4488
4489 if (appctx->st0 == STAT_HTTP_DUMP) {
Aurelien DARRAGON14656842023-02-03 08:31:42 +01004490 trash_chunk = b_make(trash.area, res->buf.size, 0, 0);
Aurelien DARRAGON5e7ecbe2023-02-02 15:03:12 +01004491 /* adjust buffer size to take htx overhead into account,
4492 * make sure to perform this call on an empty buffer
4493 */
4494 trash_chunk.size = buf_room_for_htx_data(&trash_chunk);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004495 if (stats_dump_stat_to_buffer(sc, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004496 appctx->st0 = STAT_HTTP_DONE;
4497 }
4498
4499 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004500 if (stats_process_http_post(sc))
Christopher Fauletef779222018-10-31 08:47:01 +01004501 appctx->st0 = STAT_HTTP_LAST;
Christopher Fauletca5309a2023-04-17 16:17:32 +02004502 else if (s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Fauletef779222018-10-31 08:47:01 +01004503 appctx->st0 = STAT_HTTP_DONE;
4504 }
4505
4506 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004507 if (stats_send_http_redirect(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004508 appctx->st0 = STAT_HTTP_DONE;
4509 }
4510
4511 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004512 /* no more data are expected. If the response buffer is empty,
4513 * be sure to add something (EOT block in this case) to have
4514 * something to send. It is important to be sure the EOM flags
4515 * will be handled by the endpoint.
4516 */
4517 if (htx_is_empty(res_htx)) {
4518 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004519 sc_need_room(sc, sizeof(struct htx_blk) + 1);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004520 goto out;
4521 }
4522 channel_add_input(res, 1);
4523 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004524 res_htx->flags |= HTX_FL_EOM;
Willy Tarreaud869e132022-05-17 18:05:31 +02004525 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004526 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004527 }
4528
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004529 if (appctx->st0 == STAT_HTTP_END) {
Christopher Fauletdf15a5d2023-03-31 11:28:22 +02004530 se_fl_set(appctx->sedesc, SE_FL_EOS);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004531 applet_will_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004532 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004533
Christopher Fauletef779222018-10-31 08:47:01 +01004534 out:
4535 /* we have left the request in the buffer for the case where we
4536 * process a POST, and this automatically re-enables activity on
4537 * read. It's better to indicate that we want to stop reading when
4538 * we're sending, so that we know there's at most one direction
4539 * deciding to wake the applet up. It saves it from looping when
4540 * emitting large blocks into small TCP windows.
4541 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004542 htx_to_buf(res_htx, &res->buf);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004543 if (appctx->st0 == STAT_HTTP_END) {
4544 /* eat the whole request */
4545 if (co_data(req)) {
4546 req_htx = htx_from_buf(&req->buf);
4547 co_htx_skip(req, req_htx, co_data(req));
4548 htx_to_buf(req_htx, &req->buf);
4549 }
4550 }
4551 else if (!channel_is_empty(res))
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02004552 applet_wont_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004553}
4554
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004555/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004556static int stats_dump_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004557 const struct field *info,
4558 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004559{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004560 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004561 int field;
4562
4563 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4564 if (!field_format(info, field))
4565 continue;
4566
Willy Tarreaueaa55372019-10-09 07:39:11 +02004567 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004568 return 0;
4569 if (!stats_emit_raw_data_field(out, &info[field]))
4570 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004571 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4572 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004573 if (!chunk_strcat(out, "\n"))
4574 return 0;
4575 }
4576 return 1;
4577}
4578
4579/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004580static int stats_dump_typed_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004581 const struct field *info,
4582 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004583{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004584 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004585 int field;
4586
4587 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4588 if (!field_format(info, field))
4589 continue;
4590
Willy Tarreaueaa55372019-10-09 07:39:11 +02004591 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 +01004592 return 0;
4593 if (!stats_emit_field_tags(out, &info[field], ':'))
4594 return 0;
4595 if (!stats_emit_typed_data_field(out, &info[field]))
4596 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004597 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4598 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004599 if (!chunk_strcat(out, "\n"))
4600 return 0;
4601 }
4602 return 1;
4603}
4604
Willy Tarreau0b26b382021-05-08 07:43:53 +02004605/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4606 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4607 * less then this value, the function returns 0, otherwise, it returns 1. Some
4608 * fields' presence or precision may depend on some of the STAT_* flags present
4609 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004610 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004611int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004612{
Willy Tarreau83061a82018-07-13 11:56:34 +02004613 struct buffer *out = get_trash_chunk();
Willy Tarreau6be8d092023-01-12 16:08:41 +01004614 uint64_t glob_out_bytes, glob_spl_bytes, glob_out_b32;
Willy Tarreau563efe62023-04-27 14:41:37 +02004615 uint up_sec, up_usec;
4616 ullong up;
Willy Tarreau6be8d092023-01-12 16:08:41 +01004617 int thr;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004618
4619#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004620 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4621 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4622 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004623
Willy Tarreauc5977722021-05-08 08:14:04 +02004624 if (ssl_key_rate < ssl_sess_rate)
4625 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004626#endif
4627
Willy Tarreau6be8d092023-01-12 16:08:41 +01004628 /* sum certain per-thread totals (mostly byte counts) */
4629 glob_out_bytes = glob_spl_bytes = glob_out_b32 = 0;
4630 for (thr = 0; thr < global.nbthread; thr++) {
4631 glob_out_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].out_bytes);
4632 glob_spl_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].spliced_out_bytes);
4633 glob_out_b32 += read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
4634 }
4635 glob_out_b32 *= 32; // values are 32-byte units
4636
Willy Tarreauc05d30e2023-04-28 14:50:29 +02004637 up = now_ns - start_time_ns;
Willy Tarreau563efe62023-04-27 14:41:37 +02004638 up_sec = ns_to_sec(up);
4639 up_usec = (up / 1000U) % 1000000U;
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004640
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004641 if (len < INF_TOTAL_FIELDS)
4642 return 0;
4643
4644 chunk_reset(out);
4645 memset(info, 0, sizeof(*info) * len);
4646
4647 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004648 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004649 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004650 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004651
Yves Lafon95317282018-02-26 11:10:37 +01004652 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004653 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004654 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004655 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4656
4657 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreau563efe62023-04-27 14:41:37 +02004658 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 +01004659
Willy Tarreau563efe62023-04-27 14:41:37 +02004660 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 +02004661 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 +01004662 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004663 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004664 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004665 info[INF_POOL_ALLOC_BYTES] = mkf_u64(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004666 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004667 info[INF_POOL_USED_BYTES] = mkf_u64(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004668 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4669 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4670 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4671 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4672 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4673 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4674 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4675 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4676#ifdef USE_OPENSSL
4677 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004678 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4679 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004680#endif
4681 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4682 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4683 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004684 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 +01004685 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4686 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004687 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 +01004688 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4689 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4690
4691#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004692 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 +01004693 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4694 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004695 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 +01004696 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004697 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4698 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 +01004699 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4700 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4701 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4702#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004703 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));
4704 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 +01004705 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4706#ifdef USE_ZLIB
4707 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4708 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4709#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004710 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004711 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004712 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004713 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4714 if (global.desc)
4715 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004716 info[INF_STOPPING] = mkf_u32(0, stopping);
4717 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004718 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004719 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004720 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004721 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004722 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004723 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004724 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau6be8d092023-01-12 16:08:41 +01004725 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, glob_out_bytes);
4726 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, glob_spl_bytes);
4727 info[INF_BYTES_OUT_RATE] = mkf_u64(FN_RATE, glob_out_b32);
Willy Tarreau9b013702019-10-24 18:18:02 +02004728 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004729 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004730
4731 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4732 chunk_appendf(out, "%#x", get_tainted());
4733
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004734 return 1;
4735}
4736
Willy Tarreau4596fe22022-05-17 19:07:51 +02004737/* This function dumps information onto the stream connector's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004738 * It returns 0 as long as it does not complete, non-zero upon completion.
4739 * No state is used.
4740 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004741static int stats_dump_info_to_buffer(struct stconn *sc)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004742{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004743 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004744 struct show_stat_ctx *ctx = appctx->svcctx;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004745 int ret;
4746 int current_field;
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004747
Willy Tarreau91cefca2022-05-03 17:08:29 +02004748 if (!stats_fill_info(info, INF_TOTAL_FIELDS, ctx->flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004749 return 0;
4750
Christopher Fauleta8b76842022-12-16 15:08:36 +01004751 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004752more:
4753 current_field = ctx->field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004754
Willy Tarreau91cefca2022-05-03 17:08:29 +02004755 if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004756 ret = stats_dump_typed_info_fields(&trash_chunk, info, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004757 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004758 ret = stats_dump_json_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004759 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01004760 ret = stats_dump_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004761
Christopher Fauleta8b76842022-12-16 15:08:36 +01004762 if (applet_putchk(appctx, &trash_chunk) == -1) {
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004763 /* restore previous field */
4764 ctx->field = current_field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004765 return 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004766 }
4767 if (ret && ctx->field) {
4768 /* partial dump */
4769 goto more;
4770 }
4771 ctx->field = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004772 return 1;
4773}
4774
Willy Tarreau4596fe22022-05-17 19:07:51 +02004775/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004776 * It returns 0 as long as it does not complete, non-zero upon completion.
4777 * No state is used.
4778 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004779 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004780 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4781 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004782static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004783{
4784
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004785 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004786
4787 chunk_strcat(out,
4788 "{"
4789 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4790 "\"oneOf\":["
4791 "{"
4792 "\"title\":\"Info\","
4793 "\"type\":\"array\","
4794 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004795 "\"title\":\"InfoItem\","
4796 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004797 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004798 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4799 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4800 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4801 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4802 "},"
4803 "\"required\":[\"field\",\"processNum\",\"tags\","
4804 "\"value\"]"
4805 "}"
4806 "},"
4807 "{"
4808 "\"title\":\"Stat\","
4809 "\"type\":\"array\","
4810 "\"items\":{"
4811 "\"title\":\"InfoItem\","
4812 "\"type\":\"object\","
4813 "\"properties\":{"
4814 "\"objType\":{"
4815 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4816 "\"Server\",\"Unknown\"]"
4817 "},"
4818 "\"proxyId\":{"
4819 "\"type\":\"integer\","
4820 "\"minimum\":0"
4821 "},"
4822 "\"id\":{"
4823 "\"type\":\"integer\","
4824 "\"minimum\":0"
4825 "},"
4826 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4827 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4828 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4829 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4830 "},"
4831 "\"required\":[\"objType\",\"proxyId\",\"id\","
4832 "\"field\",\"processNum\",\"tags\","
4833 "\"value\"]"
4834 "}"
4835 "},"
4836 "{"
4837 "\"title\":\"Error\","
4838 "\"type\":\"object\","
4839 "\"properties\":{"
4840 "\"errorStr\":{"
4841 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004842 "}"
4843 "},"
4844 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004845 "}"
4846 "],"
4847 "\"definitions\":{"
4848 "\"field\":{"
4849 "\"type\":\"object\","
4850 "\"pos\":{"
4851 "\"type\":\"integer\","
4852 "\"minimum\":0"
4853 "},"
4854 "\"name\":{"
4855 "\"type\":\"string\""
4856 "},"
4857 "\"required\":[\"pos\",\"name\"]"
4858 "},"
4859 "\"processNum\":{"
4860 "\"type\":\"integer\","
4861 "\"minimum\":1"
4862 "},"
4863 "\"tags\":{"
4864 "\"type\":\"object\","
4865 "\"origin\":{"
4866 "\"type\":\"string\","
4867 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4868 "\"Config\",\"Product\",\"Unknown\"]"
4869 "},"
4870 "\"nature\":{"
4871 "\"type\":\"string\","
4872 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4873 "\"Rate\",\"Counter\",\"Duration\","
4874 "\"Age\",\"Time\",\"Name\",\"Output\","
4875 "\"Avg\", \"Unknown\"]"
4876 "},"
4877 "\"scope\":{"
4878 "\"type\":\"string\","
4879 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4880 "\"System\",\"Unknown\"]"
4881 "},"
4882 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4883 "},"
4884 "\"typedValue\":{"
4885 "\"type\":\"object\","
4886 "\"oneOf\":["
4887 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4888 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4889 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4890 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4891 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4892 "],"
4893 "\"definitions\":{"
4894 "\"s32Value\":{"
4895 "\"properties\":{"
4896 "\"type\":{"
4897 "\"type\":\"string\","
4898 "\"enum\":[\"s32\"]"
4899 "},"
4900 "\"value\":{"
4901 "\"type\":\"integer\","
4902 "\"minimum\":-2147483648,"
4903 "\"maximum\":2147483647"
4904 "}"
4905 "},"
4906 "\"required\":[\"type\",\"value\"]"
4907 "},"
4908 "\"s64Value\":{"
4909 "\"properties\":{"
4910 "\"type\":{"
4911 "\"type\":\"string\","
4912 "\"enum\":[\"s64\"]"
4913 "},"
4914 "\"value\":{"
4915 "\"type\":\"integer\","
4916 "\"minimum\":-9007199254740991,"
4917 "\"maximum\":9007199254740991"
4918 "}"
4919 "},"
4920 "\"required\":[\"type\",\"value\"]"
4921 "},"
4922 "\"u32Value\":{"
4923 "\"properties\":{"
4924 "\"type\":{"
4925 "\"type\":\"string\","
4926 "\"enum\":[\"u32\"]"
4927 "},"
4928 "\"value\":{"
4929 "\"type\":\"integer\","
4930 "\"minimum\":0,"
4931 "\"maximum\":4294967295"
4932 "}"
4933 "},"
4934 "\"required\":[\"type\",\"value\"]"
4935 "},"
4936 "\"u64Value\":{"
4937 "\"properties\":{"
4938 "\"type\":{"
4939 "\"type\":\"string\","
4940 "\"enum\":[\"u64\"]"
4941 "},"
4942 "\"value\":{"
4943 "\"type\":\"integer\","
4944 "\"minimum\":0,"
4945 "\"maximum\":9007199254740991"
4946 "}"
4947 "},"
4948 "\"required\":[\"type\",\"value\"]"
4949 "},"
4950 "\"strValue\":{"
4951 "\"properties\":{"
4952 "\"type\":{"
4953 "\"type\":\"string\","
4954 "\"enum\":[\"str\"]"
4955 "},"
4956 "\"value\":{\"type\":\"string\"}"
4957 "},"
4958 "\"required\":[\"type\",\"value\"]"
4959 "},"
4960 "\"unknownValue\":{"
4961 "\"properties\":{"
4962 "\"type\":{"
4963 "\"type\":\"integer\","
4964 "\"minimum\":0"
4965 "},"
4966 "\"value\":{"
4967 "\"type\":\"string\","
4968 "\"enum\":[\"unknown\"]"
4969 "}"
4970 "},"
4971 "\"required\":[\"type\",\"value\"]"
4972 "}"
4973 "}"
4974 "}"
4975 "}"
4976 "}");
4977
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004978 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004979 chunk_reset(out);
4980 chunk_appendf(out,
4981 "{\"errorStr\":\"output buffer too short\"}");
4982 }
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02004983 chunk_appendf(out, "\n");
Simon Horman6f6bb382017-01-04 09:37:26 +01004984}
4985
Willy Tarreau4596fe22022-05-17 19:07:51 +02004986/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004987 * It returns 0 as long as it does not complete, non-zero upon completion.
4988 * No state is used.
4989 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004990static int stats_dump_json_schema_to_buffer(struct appctx *appctx)
Simon Horman6f6bb382017-01-04 09:37:26 +01004991{
Simon Horman6f6bb382017-01-04 09:37:26 +01004992
Christopher Fauleta8b76842022-12-16 15:08:36 +01004993 chunk_reset(&trash_chunk);
Simon Horman6f6bb382017-01-04 09:37:26 +01004994
Christopher Fauleta8b76842022-12-16 15:08:36 +01004995 stats_dump_json_schema(&trash_chunk);
4996
4997 if (applet_putchk(appctx, &trash_chunk) == -1)
Simon Horman6f6bb382017-01-04 09:37:26 +01004998 return 0;
Simon Horman6f6bb382017-01-04 09:37:26 +01004999
5000 return 1;
5001}
5002
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005003static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01005004{
5005 struct proxy *px;
5006 struct server *sv;
5007 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005008 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005009 int clrall = 0;
5010
5011 if (strcmp(args[2], "all") == 0)
5012 clrall = 1;
5013
5014 /* check permissions */
5015 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
5016 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
5017 return 1;
5018
Olivier Houchardfbc74e82017-11-24 16:54:05 +01005019 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01005020 if (clrall) {
5021 memset(&px->be_counters, 0, sizeof(px->be_counters));
5022 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
5023 }
5024 else {
5025 px->be_counters.conn_max = 0;
5026 px->be_counters.p.http.rps_max = 0;
5027 px->be_counters.sps_max = 0;
5028 px->be_counters.cps_max = 0;
5029 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005030 px->be_counters.qtime_max = 0;
5031 px->be_counters.ctime_max = 0;
5032 px->be_counters.dtime_max = 0;
5033 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005034
5035 px->fe_counters.conn_max = 0;
5036 px->fe_counters.p.http.rps_max = 0;
5037 px->fe_counters.sps_max = 0;
5038 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005039 }
5040
5041 for (sv = px->srv; sv; sv = sv->next)
5042 if (clrall)
5043 memset(&sv->counters, 0, sizeof(sv->counters));
5044 else {
5045 sv->counters.cur_sess_max = 0;
5046 sv->counters.nbpend_max = 0;
5047 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005048 sv->counters.qtime_max = 0;
5049 sv->counters.ctime_max = 0;
5050 sv->counters.dtime_max = 0;
5051 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005052 }
5053
5054 list_for_each_entry(li, &px->conf.listeners, by_fe)
5055 if (li->counters) {
5056 if (clrall)
5057 memset(li->counters, 0, sizeof(*li->counters));
5058 else
5059 li->counters->conn_max = 0;
5060 }
5061 }
5062
5063 global.cps_max = 0;
5064 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02005065 global.ssl_max = 0;
5066 global.ssl_fe_keys_max = 0;
5067 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005068
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005069 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5070 if (!mod->clearable && !clrall)
5071 continue;
5072
5073 for (px = proxies_list; px; px = px->next) {
5074 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
5075
5076 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
5077 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
5078 mod,
5079 mod->counters,
5080 mod->counters_size);
5081 }
5082
5083 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
5084 EXTRA_COUNTERS_INIT(px->extra_counters_be,
5085 mod,
5086 mod->counters,
5087 mod->counters_size);
5088 }
5089
5090 if (mod_cap & STATS_PX_CAP_SRV) {
5091 for (sv = px->srv; sv; sv = sv->next) {
5092 EXTRA_COUNTERS_INIT(sv->extra_counters,
5093 mod,
5094 mod->counters,
5095 mod->counters_size);
5096 }
5097 }
5098
5099 if (mod_cap & STATS_PX_CAP_LI) {
5100 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5101 EXTRA_COUNTERS_INIT(li->extra_counters,
5102 mod,
5103 mod->counters,
5104 mod->counters_size);
5105 }
5106 }
5107 }
5108 }
5109
Emeric Brunf8642ee2021-10-29 17:59:18 +02005110 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005111
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005112 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01005113 return 1;
5114}
5115
5116
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005117static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005118{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005119 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005120 int arg = 2;
5121
Willy Tarreau91cefca2022-05-03 17:08:29 +02005122 ctx->scope_str = 0;
5123 ctx->scope_len = 0;
5124 ctx->flags = 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01005125 ctx->field = 0; /* explicit default value */
Willy Tarreaud25fc792016-12-16 12:33:47 +01005126
Willy Tarreau2f397382019-10-09 11:43:59 +02005127 while (*args[arg]) {
5128 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005129 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005130 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005131 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005132 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005133 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02005134 else if (strcmp(args[arg], "float") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005135 ctx->flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02005136 arg++;
5137 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005138 return 0;
5139}
5140
5141
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005142static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01005143{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005144 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005145 int arg = 2;
5146
Willy Tarreau91cefca2022-05-03 17:08:29 +02005147 ctx->scope_str = 0;
5148 ctx->scope_len = 0;
5149 ctx->flags = STAT_SHNODE | STAT_SHDESC;
Willy Tarreau578d6e42019-10-09 11:00:22 +02005150
Willy Tarreau0698c802022-05-11 14:09:57 +02005151 if ((strm_li(appctx_strm(appctx))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005152 ctx->flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01005153
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005154 /* proxy is the default domain */
Willy Tarreau91cefca2022-05-03 17:08:29 +02005155 ctx->domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005156 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005157 ++args;
5158
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005159 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005160 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005161 } else if (strcmp(args[arg], "resolvers") == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02005162 ctx->domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005163 ++args;
5164 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005165 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005166 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005167 }
5168
Willy Tarreau91cefca2022-05-03 17:08:29 +02005169 if (ctx->domain == STATS_DOMAIN_PROXY
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005170 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005171 struct proxy *px;
5172
Willy Tarreau2f397382019-10-09 11:43:59 +02005173 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005174 if (px)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005175 ctx->iid = px->uuid;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005176 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02005177 ctx->iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005178
Willy Tarreau91cefca2022-05-03 17:08:29 +02005179 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02005180 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005181
Willy Tarreau91cefca2022-05-03 17:08:29 +02005182 ctx->flags |= STAT_BOUND;
5183 ctx->type = atoi(args[arg+1]);
5184 ctx->sid = atoi(args[arg+2]);
Willy Tarreau2f397382019-10-09 11:43:59 +02005185 arg += 3;
5186 }
5187
5188 while (*args[arg]) {
5189 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005190 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005191 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005192 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005193 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005194 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02005195 else if (strcmp(args[arg], "no-maint") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005196 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02005197 else if (strcmp(args[arg], "up") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005198 ctx->flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02005199 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01005200 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01005201
Willy Tarreau2b812e22016-11-22 16:18:05 +01005202 return 0;
5203}
5204
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005205static int cli_io_handler_dump_info(struct appctx *appctx)
5206{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005207 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005208 return stats_dump_info_to_buffer(appctx_sc(appctx));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005209}
5210
Willy Tarreau4596fe22022-05-17 19:07:51 +02005211/* This I/O handler runs as an applet embedded in a stream connector. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01005212 * used to send raw stats over a socket.
5213 */
5214static int cli_io_handler_dump_stat(struct appctx *appctx)
5215{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005216 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005217 return stats_dump_stat_to_buffer(appctx_sc(appctx), NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01005218}
5219
Simon Horman6f6bb382017-01-04 09:37:26 +01005220static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5221{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005222 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005223 return stats_dump_json_schema_to_buffer(appctx);
Simon Horman6f6bb382017-01-04 09:37:26 +01005224}
5225
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005226int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5227 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005228{
5229 struct stats_module *mod;
5230
5231 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5232
5233 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5234 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5235 continue;
5236
5237 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5238 }
5239
5240 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5241
5242 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5243 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5244 continue;
5245
5246 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5247 }
5248
5249 return 1;
5250
5251 alloc_failed:
5252 return 0;
5253}
5254
5255/* Initialize and allocate all extra counters for a proxy and its attached
5256 * servers/listeners with all already registered stats module
5257 */
5258int stats_allocate_proxy_counters(struct proxy *px)
5259{
5260 struct server *sv;
5261 struct listener *li;
5262
5263 if (px->cap & PR_CAP_FE) {
5264 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5265 COUNTERS_FE,
5266 STATS_PX_CAP_FE)) {
5267 return 0;
5268 }
5269 }
5270
5271 if (px->cap & PR_CAP_BE) {
5272 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5273 COUNTERS_BE,
5274 STATS_PX_CAP_BE)) {
5275 return 0;
5276 }
5277 }
5278
5279 for (sv = px->srv; sv; sv = sv->next) {
5280 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5281 COUNTERS_SV,
5282 STATS_PX_CAP_SRV)) {
5283 return 0;
5284 }
5285 }
5286
5287 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5288 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5289 COUNTERS_LI,
5290 STATS_PX_CAP_LI)) {
5291 return 0;
5292 }
5293 }
5294
5295 return 1;
5296}
5297
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005298void stats_register_module(struct stats_module *m)
5299{
5300 const uint8_t domain = stats_get_domain(m->domain_flags);
5301
Willy Tarreau2b718102021-04-21 07:32:39 +02005302 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005303 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005304}
5305
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005306static int allocate_stats_px_postcheck(void)
5307{
5308 struct stats_module *mod;
5309 size_t i = ST_F_TOTAL_FIELDS;
5310 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005311 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005312
5313 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5314
5315 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5316 if (!stat_f[STATS_DOMAIN_PROXY]) {
5317 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5318 err_code |= ERR_ALERT | ERR_FATAL;
5319 return err_code;
5320 }
5321
5322 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5323 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5324
5325 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5326 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5327 mod->stats,
5328 mod->stats_count * sizeof(struct name_desc));
5329 i += mod->stats_count;
5330 }
5331
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005332 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005333 if (!stats_allocate_proxy_counters(px)) {
5334 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5335 err_code |= ERR_ALERT | ERR_FATAL;
5336 return err_code;
5337 }
5338 }
5339
Christopher Fauletde79cd22021-01-06 07:41:56 +01005340 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005341
5342 return err_code;
5343}
5344
5345REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5346
Emeric Brunf8642ee2021-10-29 17:59:18 +02005347static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005348{
5349 struct stats_module *mod;
5350 size_t i = 0;
5351 int err_code = 0;
5352
Emeric Brunf8642ee2021-10-29 17:59:18 +02005353 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5354 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5355 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005356 err_code |= ERR_ALERT | ERR_FATAL;
5357 return err_code;
5358 }
5359
Emeric Brunf8642ee2021-10-29 17:59:18 +02005360 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5361 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005362 mod->stats,
5363 mod->stats_count * sizeof(struct name_desc));
5364 i += mod->stats_count;
5365 }
5366
Emeric Brunf8642ee2021-10-29 17:59:18 +02005367 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5368 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005369 err_code |= ERR_ALERT | ERR_FATAL;
5370 return err_code;
5371 }
5372
Christopher Fauletde79cd22021-01-06 07:41:56 +01005373 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005374
5375 return err_code;
5376}
5377
Emeric Brunf8642ee2021-10-29 17:59:18 +02005378REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005379
Christopher Fauletde79cd22021-01-06 07:41:56 +01005380static int allocate_stat_lines_per_thread(void)
5381{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005382 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005383
5384 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5385 const int domain = domains[i];
5386
5387 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5388 if (!stat_l[domain])
5389 return 0;
5390 }
5391 return 1;
5392}
5393
5394REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5395
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005396static int allocate_trash_counters(void)
5397{
5398 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005399 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005400 size_t max_counters_size = 0;
5401
5402 /* calculate the greatest counters used by any stats modules */
5403 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5404 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5405 max_counters_size = mod->counters_size > max_counters_size ?
5406 mod->counters_size : max_counters_size;
5407 }
5408 }
5409
5410 /* allocate the trash with the size of the greatest counters */
5411 if (max_counters_size) {
5412 trash_counters = malloc(max_counters_size);
5413 if (!trash_counters) {
5414 ha_alert("stats: cannot allocate trash counters for statistics\n");
5415 return 0;
5416 }
5417 }
5418
5419 return 1;
5420}
5421
5422REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5423
Christopher Fauletde79cd22021-01-06 07:41:56 +01005424static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005425{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005426 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005427
5428 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5429 const int domain = domains[i];
5430
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005431 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005432 }
5433}
5434
5435
5436REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5437
5438static void deinit_stats(void)
5439{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005440 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005441
5442 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5443 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005444
5445 if (stat_f[domain])
5446 free(stat_f[domain]);
5447 }
5448}
5449
5450REGISTER_POST_DEINIT(deinit_stats);
5451
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005452static void free_trash_counters(void)
5453{
5454 if (trash_counters)
5455 free(trash_counters);
5456}
5457
5458REGISTER_PER_THREAD_FREE(free_trash_counters);
5459
Willy Tarreau2b812e22016-11-22 16:18:05 +01005460/* register cli keywords */
5461static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005462 { { "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 +02005463 { { "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 +02005464 { { "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 },
5465 { { "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 +01005466 {{},}
5467}};
5468
Willy Tarreau0108d902018-11-25 19:14:37 +01005469INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5470
William Lallemand74c24fb2016-11-21 17:18:36 +01005471struct applet http_stats_applet = {
5472 .obj_type = OBJ_TYPE_APPLET,
5473 .name = "<STATS>", /* used for logging */
5474 .fct = http_stats_io_handler,
5475 .release = NULL,
5476};
5477
William Lallemand74c24fb2016-11-21 17:18:36 +01005478/*
5479 * Local variables:
5480 * c-indent-level: 8
5481 * c-basic-offset: 8
5482 * End:
5483 */