blob: edee3aaf5e7a2b18d8243a73e61206866f83f31a [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" },
Christopher Fauletaaa70852020-07-10 13:56:30 +0200153 [INF_TOTAL_SPLICED_BYTES_OUT] = { .name = "TotalSplicdedBytesOut", .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
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +0100308int stats_putchk(struct channel *chn, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +0100309{
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +0100310 struct buffer *chk = &trash_chunk;
311
Christopher Fauletef779222018-10-31 08:47:01 +0100312 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100313 if (chk->data >= channel_htx_recv_max(chn, htx))
314 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200315 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100316 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100317 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100318 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100319 }
320 else {
321 if (ci_putchk(chn, chk) == -1)
322 return 0;
323 }
324 return 1;
325}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100326
Willy Tarreaucaff6312022-05-27 10:17:46 +0200327static const char *stats_scope_ptr(struct appctx *appctx, struct stconn *sc)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100328{
Willy Tarreau91cefca2022-05-03 17:08:29 +0200329 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +0200330 struct channel *req = sc_oc(sc);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200331 struct htx *htx = htxbuf(&req->buf);
332 struct htx_blk *blk;
333 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100334
Christopher Fauletb7f88902019-07-15 21:56:43 +0200335 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100336 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200337 ALREADY_CHECKED(blk);
338 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Willy Tarreau91cefca2022-05-03 17:08:29 +0200339 return uri.ptr + ctx->scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100340}
341
William Lallemand74c24fb2016-11-21 17:18:36 +0100342/*
343 * http_stats_io_handler()
344 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
345 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100346 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100347 * -> stats_dump_html_head() // emits the HTML headers
348 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
349 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
350 * -> stats_dump_html_px_hdr()
351 * -> stats_dump_fe_stats()
352 * -> stats_dump_li_stats()
353 * -> stats_dump_sv_stats()
354 * -> stats_dump_be_stats()
355 * -> stats_dump_html_px_end()
356 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100357 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100358 */
359
360
Christopher Fauleta8b76842022-12-16 15:08:36 +0100361/* Dumps the stats CSV header to the local trash buffer. The caller is
362 * responsible for clearing it if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +0100363 * NOTE: Some tools happen to rely on the field position instead of its name,
364 * so please only append new fields at the end, never in the middle.
365 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200366static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100367{
368 int field;
369
Christopher Fauleta8b76842022-12-16 15:08:36 +0100370 chunk_appendf(&trash_chunk, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200371 if (stat_f[domain]) {
372 for (field = 0; field < stat_count[domain]; ++field) {
Christopher Fauleta8b76842022-12-16 15:08:36 +0100373 chunk_appendf(&trash_chunk, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100374
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200375 /* print special delimiter on proxy stats to mark end of
376 static fields */
377 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
Christopher Fauleta8b76842022-12-16 15:08:36 +0100378 chunk_appendf(&trash_chunk, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200379 }
380 }
381
Christopher Fauleta8b76842022-12-16 15:08:36 +0100382 chunk_appendf(&trash_chunk, "\n");
William Lallemand74c24fb2016-11-21 17:18:36 +0100383}
384
William Lallemand74c24fb2016-11-21 17:18:36 +0100385/* Emits a stats field without any surrounding element and properly encoded to
386 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
387 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200388int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100389{
390 switch (field_format(f, 0)) {
391 case FF_EMPTY: return 1;
392 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
393 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
394 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
395 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200396 case FF_FLT: {
397 size_t prev_data = out->data;
398 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
399 return out->data;
400 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100401 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
402 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
403 }
404}
405
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200406const char *field_to_html_str(const struct field *f)
407{
408 switch (field_format(f, 0)) {
409 case FF_S32: return U2H(f->u.s32);
410 case FF_S64: return U2H(f->u.s64);
411 case FF_U64: return U2H(f->u.u64);
412 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200413 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200414 case FF_STR: return field_str(f, 0);
415 case FF_EMPTY:
416 default:
417 return "";
418 }
419}
420
William Lallemand74c24fb2016-11-21 17:18:36 +0100421/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
422 * output is supposed to be used on its own line. Returns non-zero on success, 0
423 * if the buffer is full.
424 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200425int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100426{
427 switch (field_format(f, 0)) {
428 case FF_EMPTY: return 1;
429 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
430 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
431 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
432 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200433 case FF_FLT: {
434 size_t prev_data = out->data;
435 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
436 return out->data;
437 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100438 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
439 default: return chunk_appendf(out, "%08x:?", f->type);
440 }
441}
442
Simon Horman05ee2132017-01-04 09:37:25 +0100443/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
444 * the recommendation for interoperable integers in section 6 of RFC 7159.
445 */
446#define JSON_INT_MAX ((1ULL << 53) - 1)
447#define JSON_INT_MIN (0 - JSON_INT_MAX)
448
449/* Emits a stats field value and its type in JSON.
450 * Returns non-zero on success, 0 on error.
451 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200452int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100453{
454 int old_len;
455 char buf[20];
456 const char *type, *value = buf, *quote = "";
457
458 switch (field_format(f, 0)) {
459 case FF_EMPTY: return 1;
460 case FF_S32: type = "\"s32\"";
461 snprintf(buf, sizeof(buf), "%d", f->u.s32);
462 break;
463 case FF_U32: type = "\"u32\"";
464 snprintf(buf, sizeof(buf), "%u", f->u.u32);
465 break;
466 case FF_S64: type = "\"s64\"";
467 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
468 return 0;
469 type = "\"s64\"";
470 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
471 break;
472 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
473 return 0;
474 type = "\"u64\"";
475 snprintf(buf, sizeof(buf), "%llu",
476 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200477 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200478 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200479 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100480 break;
481 case FF_STR: type = "\"str\"";
482 value = field_str(f, 0);
483 quote = "\"";
484 break;
485 default: snprintf(buf, sizeof(buf), "%u", f->type);
486 type = buf;
487 value = "unknown";
488 quote = "\"";
489 break;
490 }
491
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200492 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100493 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
494 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200495 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100496}
497
William Lallemand74c24fb2016-11-21 17:18:36 +0100498/* Emits an encoding of the field type on 3 characters followed by a delimiter.
499 * Returns non-zero on success, 0 if the buffer is full.
500 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200501int stats_emit_field_tags(struct buffer *out, const struct field *f,
502 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100503{
504 char origin, nature, scope;
505
506 switch (field_origin(f, 0)) {
507 case FO_METRIC: origin = 'M'; break;
508 case FO_STATUS: origin = 'S'; break;
509 case FO_KEY: origin = 'K'; break;
510 case FO_CONFIG: origin = 'C'; break;
511 case FO_PRODUCT: origin = 'P'; break;
512 default: origin = '?'; break;
513 }
514
515 switch (field_nature(f, 0)) {
516 case FN_GAUGE: nature = 'G'; break;
517 case FN_LIMIT: nature = 'L'; break;
518 case FN_MIN: nature = 'm'; break;
519 case FN_MAX: nature = 'M'; break;
520 case FN_RATE: nature = 'R'; break;
521 case FN_COUNTER: nature = 'C'; break;
522 case FN_DURATION: nature = 'D'; break;
523 case FN_AGE: nature = 'A'; break;
524 case FN_TIME: nature = 'T'; break;
525 case FN_NAME: nature = 'N'; break;
526 case FN_OUTPUT: nature = 'O'; break;
527 case FN_AVG: nature = 'a'; break;
528 default: nature = '?'; break;
529 }
530
531 switch (field_scope(f, 0)) {
532 case FS_PROCESS: scope = 'P'; break;
533 case FS_SERVICE: scope = 'S'; break;
534 case FS_SYSTEM: scope = 's'; break;
535 case FS_CLUSTER: scope = 'C'; break;
536 default: scope = '?'; break;
537 }
538
539 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
540}
541
Simon Horman05ee2132017-01-04 09:37:25 +0100542/* Emits an encoding of the field type as JSON.
543 * Returns non-zero on success, 0 if the buffer is full.
544 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200545int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100546{
547 const char *origin, *nature, *scope;
548 int old_len;
549
550 switch (field_origin(f, 0)) {
551 case FO_METRIC: origin = "Metric"; break;
552 case FO_STATUS: origin = "Status"; break;
553 case FO_KEY: origin = "Key"; break;
554 case FO_CONFIG: origin = "Config"; break;
555 case FO_PRODUCT: origin = "Product"; break;
556 default: origin = "Unknown"; break;
557 }
558
559 switch (field_nature(f, 0)) {
560 case FN_GAUGE: nature = "Gauge"; break;
561 case FN_LIMIT: nature = "Limit"; break;
562 case FN_MIN: nature = "Min"; break;
563 case FN_MAX: nature = "Max"; break;
564 case FN_RATE: nature = "Rate"; break;
565 case FN_COUNTER: nature = "Counter"; break;
566 case FN_DURATION: nature = "Duration"; break;
567 case FN_AGE: nature = "Age"; break;
568 case FN_TIME: nature = "Time"; break;
569 case FN_NAME: nature = "Name"; break;
570 case FN_OUTPUT: nature = "Output"; break;
571 case FN_AVG: nature = "Avg"; break;
572 default: nature = "Unknown"; break;
573 }
574
575 switch (field_scope(f, 0)) {
576 case FS_PROCESS: scope = "Process"; break;
577 case FS_SERVICE: scope = "Service"; break;
578 case FS_SYSTEM: scope = "System"; break;
579 case FS_CLUSTER: scope = "Cluster"; break;
580 default: scope = "Unknown"; break;
581 }
582
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200583 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100584 chunk_appendf(out, "\"tags\":{"
585 "\"origin\":\"%s\","
586 "\"nature\":\"%s\","
587 "\"scope\":\"%s\""
588 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200589 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100590}
William Lallemand74c24fb2016-11-21 17:18:36 +0100591
592/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200593static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200594 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100595 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100596{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100597 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100598 int field;
599
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200600 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100601 if (!stats_emit_raw_data_field(out, &stats[field]))
602 return 0;
603 if (!chunk_strcat(out, ","))
604 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200605
606 /* print special delimiter on proxy stats to mark end of
607 static fields */
608 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
609 if (!chunk_strcat(out, "-,"))
610 return 0;
611 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100612 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200613
William Lallemand74c24fb2016-11-21 17:18:36 +0100614 chunk_strcat(out, "\n");
615 return 1;
616}
617
618/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200619static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200620 const struct field *stats,
621 size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100622 struct show_stat_ctx * ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100623{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100624 int flags = ctx->flags;
625 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100626 int field;
627
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200628 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100629 if (!stats[field].type)
630 continue;
631
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200632 switch (domain) {
633 case STATS_DOMAIN_PROXY:
634 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
635 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
636 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
637 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
638 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
639 '?',
640 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
641 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200642 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200643 stats[ST_F_PID].u.u32);
644 break;
645
Emeric Brunf8642ee2021-10-29 17:59:18 +0200646 case STATS_DOMAIN_RESOLVERS:
647 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200648 stat_f[domain][field].name);
649 break;
650
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200651 default:
652 break;
653 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100654
655 if (!stats_emit_field_tags(out, &stats[field], ':'))
656 return 0;
657 if (!stats_emit_typed_data_field(out, &stats[field]))
658 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200659
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200660 if (flags & STAT_SHOW_FDESC &&
661 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200662 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200663 }
664
William Lallemand74c24fb2016-11-21 17:18:36 +0100665 if (!chunk_strcat(out, "\n"))
666 return 0;
667 }
668 return 1;
669}
670
Simon Horman05ee2132017-01-04 09:37:25 +0100671/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200672static int stats_dump_json_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100673 const struct field *info,
674 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100675{
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100676 int started = (ctx->field) ? 1 : 0;
677 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100678
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100679 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100680 return 0;
681
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100682 for (; ctx->field < INF_TOTAL_FIELDS; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100683 int old_len;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100684 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100685
686 if (!field_format(info, field))
687 continue;
688
689 if (started && !chunk_strcat(out, ","))
690 goto err;
691 started = 1;
692
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200693 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100694 chunk_appendf(out,
695 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
696 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200697 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100698 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200699 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100700 goto err;
701
702 if (!stats_emit_json_field_tags(out, &info[field]))
703 goto err;
704
705 if (!stats_emit_json_data_field(out, &info[field]))
706 goto err;
707
708 if (!chunk_strcat(out, "}"))
709 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100710 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100711 }
712
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200713 if (!chunk_strcat(out, "]\n"))
Simon Horman05ee2132017-01-04 09:37:25 +0100714 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100715 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100716 return 1;
717
718err:
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100719 if (!ready_data) {
720 /* not enough buffer space for a single entry.. */
721 chunk_reset(out);
722 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
723 return 0; /* hard error */
724 }
725 /* push ready data and wait for a new buffer to complete the dump */
726 out->data = ready_data;
727 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100728}
729
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200730static void stats_print_proxy_field_json(struct buffer *out,
731 const struct field *stat,
732 const char *name,
733 int pos,
734 uint32_t field_type,
735 uint32_t iid,
736 uint32_t sid,
737 uint32_t pid)
738{
739 const char *obj_type;
740 switch (field_type) {
741 case STATS_TYPE_FE: obj_type = "Frontend"; break;
742 case STATS_TYPE_BE: obj_type = "Backend"; break;
743 case STATS_TYPE_SO: obj_type = "Listener"; break;
744 case STATS_TYPE_SV: obj_type = "Server"; break;
745 default: obj_type = "Unknown"; break;
746 }
747
748 chunk_appendf(out,
749 "{"
750 "\"objType\":\"%s\","
751 "\"proxyId\":%u,"
752 "\"id\":%u,"
753 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
754 "\"processNum\":%u,",
755 obj_type, iid, sid, pos, name, pid);
756}
757
Emeric Brunf8642ee2021-10-29 17:59:18 +0200758static void stats_print_rslv_field_json(struct buffer *out,
759 const struct field *stat,
760 const char *name,
761 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200762{
763 chunk_appendf(out,
764 "{"
765 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
766 pos, name);
767}
768
769
Simon Horman05ee2132017-01-04 09:37:25 +0100770/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200771static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200772 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100773 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100774{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100775 int flags = ctx->flags;
776 int domain = ctx->domain;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100777 int started = (ctx->field) ? 1 : 0;
778 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100779
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100780 if (!started && (flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100781 return 0;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100782 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100783 return 0;
784
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100785 for (; ctx->field < stats_count; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100786 int old_len;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100787 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100788
789 if (!stats[field].type)
790 continue;
791
792 if (started && !chunk_strcat(out, ","))
793 goto err;
794 started = 1;
795
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200796 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200797 if (domain == STATS_DOMAIN_PROXY) {
798 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200799 stat_f[domain][field].name,
800 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200801 stats[ST_F_TYPE].u.u32,
802 stats[ST_F_IID].u.u32,
803 stats[ST_F_SID].u.u32,
804 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200805 } else if (domain == STATS_DOMAIN_RESOLVERS) {
806 stats_print_rslv_field_json(out, &stats[field],
807 stat_f[domain][field].name,
808 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200809 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200810
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200811 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100812 goto err;
813
814 if (!stats_emit_json_field_tags(out, &stats[field]))
815 goto err;
816
817 if (!stats_emit_json_data_field(out, &stats[field]))
818 goto err;
819
820 if (!chunk_strcat(out, "}"))
821 goto err;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100822 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100823 }
824
825 if (!chunk_strcat(out, "]"))
826 goto err;
827
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100828 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100829 return 1;
830
831err:
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100832 if (!ready_data) {
833 /* not enough buffer space for a single entry.. */
834 chunk_reset(out);
835 if (ctx->flags & STAT_STARTED)
836 chunk_strcat(out, ",");
837 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
838 return 0; /* hard error */
839 }
840 /* push ready data and wait for a new buffer to complete the dump */
841 out->data = ready_data;
842 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100843}
844
William Lallemand74c24fb2016-11-21 17:18:36 +0100845/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200846 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200847 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
848 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
849 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100850 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200851static int stats_dump_fields_html(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100852 const struct field *stats,
853 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100854{
Willy Tarreau83061a82018-07-13 11:56:34 +0200855 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200856 struct stats_module *mod;
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100857 int flags = ctx->flags;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200858 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100859
860 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
861 chunk_appendf(out,
862 /* name, queue */
863 "<tr class=\"frontend\">");
864
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200865 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100866 /* Column sub-heading for Enable or Disable server */
867 chunk_appendf(out, "<td></td>");
868 }
869
870 chunk_appendf(out,
871 "<td class=ac>"
872 "<a name=\"%s/Frontend\"></a>"
873 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
874 "<td colspan=3></td>"
875 "",
876 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
877
878 chunk_appendf(out,
879 /* sessions rate : current */
880 "<td><u>%s<div class=tips><table class=det>"
881 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
882 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
883 "",
884 U2H(stats[ST_F_RATE].u.u32),
885 U2H(stats[ST_F_CONN_RATE].u.u32),
886 U2H(stats[ST_F_RATE].u.u32));
887
888 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
889 chunk_appendf(out,
890 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
891 U2H(stats[ST_F_REQ_RATE].u.u32));
892
893 chunk_appendf(out,
894 "</table></div></u></td>"
895 /* sessions rate : max */
896 "<td><u>%s<div class=tips><table class=det>"
897 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
898 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
899 "",
900 U2H(stats[ST_F_RATE_MAX].u.u32),
901 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
902 U2H(stats[ST_F_RATE_MAX].u.u32));
903
904 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
905 chunk_appendf(out,
906 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
907 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
908
909 chunk_appendf(out,
910 "</table></div></u></td>"
911 /* sessions rate : limit */
912 "<td>%s</td>",
913 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
914
915 chunk_appendf(out,
916 /* sessions: current, max, limit, total */
917 "<td>%s</td><td>%s</td><td>%s</td>"
918 "<td><u>%s<div class=tips><table class=det>"
919 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
920 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
921 "",
922 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
923 U2H(stats[ST_F_STOT].u.u64),
924 U2H(stats[ST_F_CONN_TOT].u.u64),
925 U2H(stats[ST_F_STOT].u.u64));
926
927 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
928 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
929 chunk_appendf(out,
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100930 "<tr><th>- HTTP/1 sessions:</th><td>%s</td></tr>"
931 "<tr><th>- HTTP/2 sessions:</th><td>%s</td></tr>"
932 "<tr><th>- HTTP/3 sessions:</th><td>%s</td></tr>"
933 "<tr><th>- other sessions:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100934 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100935 "<tr><th>- HTTP/1 requests:</th><td>%s</td></tr>"
936 "<tr><th>- HTTP/2 requests:</th><td>%s</td></tr>"
937 "<tr><th>- HTTP/3 requests:</th><td>%s</td></tr>"
938 "<tr><th>- other requests:</th><td>%s</td></tr>"
939 "",
940 U2H(stats[ST_F_H1SESS].u.u64),
941 U2H(stats[ST_F_H2SESS].u.u64),
942 U2H(stats[ST_F_H3SESS].u.u64),
943 U2H(stats[ST_F_SESS_OTHER].u.u64),
944 U2H(stats[ST_F_REQ_TOT].u.u64),
945 U2H(stats[ST_F_H1REQ].u.u64),
946 U2H(stats[ST_F_H2REQ].u.u64),
947 U2H(stats[ST_F_H3REQ].u.u64),
948 U2H(stats[ST_F_REQ_OTHER].u.u64));
949
950 chunk_appendf(out,
William Lallemand74c24fb2016-11-21 17:18:36 +0100951 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
952 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
953 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
954 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
955 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
956 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
957 "<tr><th>- other responses:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100958 "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100959 U2H(stats[ST_F_HRSP_1XX].u.u64),
960 U2H(stats[ST_F_HRSP_2XX].u.u64),
961 U2H(stats[ST_F_COMP_RSP].u.u64),
962 stats[ST_F_HRSP_2XX].u.u64 ?
963 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
964 U2H(stats[ST_F_HRSP_3XX].u.u64),
965 U2H(stats[ST_F_HRSP_4XX].u.u64),
966 U2H(stats[ST_F_HRSP_5XX].u.u64),
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100967 U2H(stats[ST_F_HRSP_OTHER].u.u64));
968
969 chunk_appendf(out,
970 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
971 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
972 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
973 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
974 "<tr><th>Internal errors:</th><td>%s</td></tr>"
975 "",
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200976 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100977 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
978 U2H(stats[ST_F_CACHE_HITS].u.u64),
979 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
980 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100981 U2H(stats[ST_F_WREW].u.u64),
982 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100983 }
984
985 chunk_appendf(out,
986 "</table></div></u></td>"
987 /* sessions: lbtot, lastsess */
988 "<td></td><td></td>"
989 /* bytes : in */
990 "<td>%s</td>"
991 "",
992 U2H(stats[ST_F_BIN].u.u64));
993
994 chunk_appendf(out,
995 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
996 "<td>%s%s<div class=tips><table class=det>"
997 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
998 "<tr><th>Compression in:</th><td>%s</td></tr>"
999 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1000 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1001 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1002 "</table></div>%s</td>",
1003 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1004 U2H(stats[ST_F_BOUT].u.u64),
1005 U2H(stats[ST_F_BOUT].u.u64),
1006 U2H(stats[ST_F_COMP_IN].u.u64),
1007 U2H(stats[ST_F_COMP_OUT].u.u64),
1008 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1009 U2H(stats[ST_F_COMP_BYP].u.u64),
1010 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1011 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,
1012 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1013
1014 chunk_appendf(out,
1015 /* denied: req, resp */
1016 "<td>%s</td><td>%s</td>"
1017 /* errors : request, connect, response */
1018 "<td>%s</td><td></td><td></td>"
1019 /* warnings: retries, redispatches */
1020 "<td></td><td></td>"
1021 /* server status : reflect frontend status */
1022 "<td class=ac>%s</td>"
1023 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001024 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001025 "",
1026 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1027 U2H(stats[ST_F_EREQ].u.u64),
1028 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001029
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001030 if (flags & STAT_SHMODULES) {
1031 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001032 chunk_appendf(out, "<td>");
1033
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001034 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001035 chunk_appendf(out,
1036 "<u>%s<div class=tips><table class=det>",
1037 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001038 for (j = 0; j < mod->stats_count; ++j) {
1039 chunk_appendf(out,
1040 "<tr><th>%s</th><td>%s</td></tr>",
1041 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1042 ++i;
1043 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001044 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001045 } else {
1046 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001047 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001048
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001049 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001050 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001051 }
1052
1053 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001054 }
1055 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
1056 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001057 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001058 /* Column sub-heading for Enable or Disable server */
1059 chunk_appendf(out, "<td></td>");
1060 }
1061
1062 chunk_appendf(out,
1063 /* frontend name, listener name */
1064 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1065 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1066 "",
1067 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001068 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001069 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1070
Willy Tarreau708c4162019-10-09 10:19:16 +02001071 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001072 chunk_appendf(out, "<div class=tips>");
1073
Willy Tarreau90807112020-02-25 08:16:33 +01001074 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001075 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1076 else if (*field_str(stats, ST_F_ADDR) == '[')
1077 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1078 else if (*field_str(stats, ST_F_ADDR))
1079 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1080
1081 /* id */
1082 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1083 }
1084
1085 chunk_appendf(out,
1086 /* queue */
1087 "%s</td><td colspan=3></td>"
1088 /* sessions rate: current, max, limit */
1089 "<td colspan=3>&nbsp;</td>"
1090 /* sessions: current, max, limit, total, lbtot, lastsess */
1091 "<td>%s</td><td>%s</td><td>%s</td>"
1092 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1093 /* bytes: in, out */
1094 "<td>%s</td><td>%s</td>"
1095 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001096 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001097 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1098 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1099
1100 chunk_appendf(out,
1101 /* denied: req, resp */
1102 "<td>%s</td><td>%s</td>"
1103 /* errors: request, connect, response */
1104 "<td>%s</td><td></td><td></td>"
1105 /* warnings: retries, redispatches */
1106 "<td></td><td></td>"
1107 /* server status: reflect listener status */
1108 "<td class=ac>%s</td>"
1109 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001110 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001111 "",
1112 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1113 U2H(stats[ST_F_EREQ].u.u64),
1114 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001115
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001116 if (flags & STAT_SHMODULES) {
1117 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001118 chunk_appendf(out, "<td>");
1119
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001120 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001121 chunk_appendf(out,
1122 "<u>%s<div class=tips><table class=det>",
1123 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001124 for (j = 0; j < mod->stats_count; ++j) {
1125 chunk_appendf(out,
1126 "<tr><th>%s</th><td>%s</td></tr>",
1127 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1128 ++i;
1129 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001130 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001131 } else {
1132 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001133 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001134
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001135 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001136 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001137 }
1138
1139 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001140 }
1141 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1142 const char *style;
1143
1144 /* determine the style to use depending on the server's state,
1145 * its health and weight. There isn't a 1-to-1 mapping between
1146 * state and styles for the cases where the server is (still)
1147 * up. The reason is that we don't want to report nolb and
1148 * drain with the same color.
1149 */
1150
1151 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1152 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1153 style = "down";
1154 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001155 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001156 style = "going_up";
1157 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001158 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1159 style = "draining";
1160 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001161 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001162 style = "going_down";
1163 }
1164 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1165 style = "nolb";
1166 }
1167 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1168 style = "no_check";
1169 }
1170 else if (!stats[ST_F_CHKFAIL].type ||
1171 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1172 /* no check or max health = UP */
1173 if (stats[ST_F_WEIGHT].u.u32)
1174 style = "up";
1175 else
1176 style = "draining";
1177 }
1178 else {
1179 style = "going_down";
1180 }
1181
Willy Tarreau7b524852020-08-11 10:26:36 +02001182 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001183 chunk_appendf(out, "<tr class=\"maintain\">");
1184 else
1185 chunk_appendf(out,
1186 "<tr class=\"%s_%s\">",
1187 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1188
1189
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001190 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001191 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001192 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1193 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001194 field_str(stats, ST_F_SVNAME));
1195
1196 chunk_appendf(out,
1197 "<td class=ac><a name=\"%s/%s\"></a>%s"
1198 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1199 "",
1200 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001201 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001202 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1203
Willy Tarreau708c4162019-10-09 10:19:16 +02001204 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001205 chunk_appendf(out, "<div class=tips>");
1206
Willy Tarreau90807112020-02-25 08:16:33 +01001207 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001208 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1209 else if (*field_str(stats, ST_F_ADDR) == '[')
1210 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1211 else if (*field_str(stats, ST_F_ADDR))
1212 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1213
1214 /* id */
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01001215 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 +01001216
1217 /* cookie */
1218 if (stats[ST_F_COOKIE].type) {
1219 chunk_appendf(out, ", cookie: '");
1220 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1221 chunk_htmlencode(out, &src);
1222 chunk_appendf(out, "'");
1223 }
1224
1225 chunk_appendf(out, "</div>");
1226 }
1227
1228 chunk_appendf(out,
1229 /* queue : current, max, limit */
1230 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1231 /* sessions rate : current, max, limit */
1232 "<td>%s</td><td>%s</td><td></td>"
1233 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001234 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001235 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1236 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1237
1238 chunk_appendf(out,
1239 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001240 "<td><u>%s<div class=tips>"
1241 "<table class=det>"
1242 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001243 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001244 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001245 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1246 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001247 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001248 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1249 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1250 "</table></div></u>"
1251 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001252 "<td><u>%s<div class=tips><table class=det>"
1253 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1254 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001255 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001256 U2H(stats[ST_F_SCUR].u.u32),
1257 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1258 U2H(stats[ST_F_SRV_ICUR].u.u32),
1259 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1260 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001261 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001262
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001263 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1264 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1265 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001266 U2H(stats[ST_F_STOT].u.u64),
1267 U2H(stats[ST_F_STOT].u.u64));
1268
1269 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1270 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001271 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001272 "<tr><th>New connections:</th><td>%s</td></tr>"
1273 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001274 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001275 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1276 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1277 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1278 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1279 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1280 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001281 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001282 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001283 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001284 U2H(stats[ST_F_CONNECT].u.u64),
1285 U2H(stats[ST_F_REUSE].u.u64),
1286 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1287 (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 +02001288 U2H(stats[ST_F_REQ_TOT].u.u64),
1289 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1290 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1291 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1292 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1293 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1294 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1295 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1296 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1297 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1298 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1299 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1300 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001301 U2H(stats[ST_F_WREW].u.u64),
1302 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001303 }
1304
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001305 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1306 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1307 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1308 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1309 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001310 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001311 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1312 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1313 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1314 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001315
1316 chunk_appendf(out,
1317 "</table></div></u></td>"
1318 /* sessions: lbtot, last */
1319 "<td>%s</td><td>%s</td>",
1320 U2H(stats[ST_F_LBTOT].u.u64),
1321 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1322
1323 chunk_appendf(out,
1324 /* bytes : in, out */
1325 "<td>%s</td><td>%s</td>"
1326 /* denied: req, resp */
1327 "<td></td><td>%s</td>"
1328 /* errors : request, connect */
1329 "<td></td><td>%s</td>"
1330 /* errors : response */
1331 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1332 /* warnings: retries, redispatches */
1333 "<td>%lld</td><td>%lld</td>"
1334 "",
1335 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1336 U2H(stats[ST_F_DRESP].u.u64),
1337 U2H(stats[ST_F_ECON].u.u64),
1338 U2H(stats[ST_F_ERESP].u.u64),
1339 (long long)stats[ST_F_CLI_ABRT].u.u64,
1340 (long long)stats[ST_F_SRV_ABRT].u.u64,
1341 (long long)stats[ST_F_WRETR].u.u64,
1342 (long long)stats[ST_F_WREDIS].u.u64);
1343
1344 /* status, last change */
1345 chunk_appendf(out, "<td class=ac>");
1346
1347 /* FIXME!!!!
1348 * LASTCHG should contain the last change for *this* server and must be computed
1349 * properly above, as was done below, ie: this server if maint, otherwise ref server
1350 * if tracking. Note that ref is either local or remote depending on tracking.
1351 */
1352
1353
Willy Tarreau7b524852020-08-11 10:26:36 +02001354 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001355 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1356 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001357 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001358 chunk_strcat(out, "<i>no check</i>");
1359 }
1360 else {
1361 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 +02001362 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001363 if (stats[ST_F_CHECK_HEALTH].u.u32)
1364 chunk_strcat(out, " &uarr;");
1365 }
1366 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1367 chunk_strcat(out, " &darr;");
1368 }
1369
Willy Tarreau7b524852020-08-11 10:26:36 +02001370 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001371 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1372 chunk_appendf(out,
1373 "</td><td class=ac><u> %s",
1374 field_str(stats, ST_F_AGENT_STATUS));
1375
1376 if (stats[ST_F_AGENT_CODE].type)
1377 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1378
1379 if (stats[ST_F_AGENT_DURATION].type)
1380 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1381
1382 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1383
1384 if (*field_str(stats, ST_F_LAST_AGT)) {
1385 chunk_appendf(out, ": ");
1386 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1387 chunk_htmlencode(out, &src);
1388 }
1389 chunk_appendf(out, "</div></u>");
1390 }
1391 else if (stats[ST_F_CHECK_STATUS].type) {
1392 chunk_appendf(out,
1393 "</td><td class=ac><u> %s",
1394 field_str(stats, ST_F_CHECK_STATUS));
1395
1396 if (stats[ST_F_CHECK_CODE].type)
1397 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1398
1399 if (stats[ST_F_CHECK_DURATION].type)
1400 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1401
1402 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1403
1404 if (*field_str(stats, ST_F_LAST_CHK)) {
1405 chunk_appendf(out, ": ");
1406 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1407 chunk_htmlencode(out, &src);
1408 }
1409 chunk_appendf(out, "</div></u>");
1410 }
1411 else
1412 chunk_appendf(out, "</td><td>");
1413
1414 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001415 /* weight / uweight */
1416 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001417 /* act, bck */
1418 "<td class=ac>%s</td><td class=ac>%s</td>"
1419 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001420 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001421 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1422 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1423
1424 /* check failures: unique, fatal, down time */
1425 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1426 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1427 }
1428 else if (stats[ST_F_CHKFAIL].type) {
1429 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1430
1431 if (stats[ST_F_HANAFAIL].type)
1432 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1433
1434 chunk_appendf(out,
1435 "<div class=tips>Failed Health Checks%s</div></u></td>"
1436 "<td>%lld</td><td>%s</td>"
1437 "",
1438 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1439 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1440 }
1441 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1442 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1443 chunk_appendf(out,
1444 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1445 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1446 }
1447 else
1448 chunk_appendf(out, "<td colspan=3></td>");
1449
1450 /* throttle */
1451 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001452 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001453 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001454 chunk_appendf(out, "<td class=ac>-</td>");
1455
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001456 if (flags & STAT_SHMODULES) {
1457 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001458 chunk_appendf(out, "<td>");
1459
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001460 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001461 chunk_appendf(out,
1462 "<u>%s<div class=tips><table class=det>",
1463 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001464 for (j = 0; j < mod->stats_count; ++j) {
1465 chunk_appendf(out,
1466 "<tr><th>%s</th><td>%s</td></tr>",
1467 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1468 ++i;
1469 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001470 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001471 } else {
1472 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001473 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001474
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001475 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001476 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001477 }
1478
1479 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001480 }
1481 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1482 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001483 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001484 /* Column sub-heading for Enable or Disable server */
1485 chunk_appendf(out, "<td></td>");
1486 }
1487 chunk_appendf(out,
1488 "<td class=ac>"
1489 /* name */
1490 "%s<a name=\"%s/Backend\"></a>"
1491 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1492 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001493 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001494 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1495
Willy Tarreau708c4162019-10-09 10:19:16 +02001496 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001497 /* balancing */
1498 chunk_appendf(out, "<div class=tips>balancing: %s",
1499 field_str(stats, ST_F_ALGO));
1500
1501 /* cookie */
1502 if (stats[ST_F_COOKIE].type) {
1503 chunk_appendf(out, ", cookie: '");
1504 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1505 chunk_htmlencode(out, &src);
1506 chunk_appendf(out, "'");
1507 }
1508 chunk_appendf(out, "</div>");
1509 }
1510
1511 chunk_appendf(out,
1512 "%s</td>"
1513 /* queue : current, max */
1514 "<td>%s</td><td>%s</td><td></td>"
1515 /* sessions rate : current, max, limit */
1516 "<td>%s</td><td>%s</td><td></td>"
1517 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001518 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001519 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1520 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1521
1522 chunk_appendf(out,
1523 /* sessions: current, max, limit, total */
1524 "<td>%s</td><td>%s</td><td>%s</td>"
1525 "<td><u>%s<div class=tips><table class=det>"
1526 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1527 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001528 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 +01001529 U2H(stats[ST_F_STOT].u.u64),
1530 U2H(stats[ST_F_STOT].u.u64));
1531
1532 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1533 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1534 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001535 "<tr><th>New connections:</th><td>%s</td></tr>"
1536 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001537 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1538 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1539 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1540 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1541 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1542 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1543 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1544 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001545 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1546 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001547 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001548 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001549 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001550 U2H(stats[ST_F_CONNECT].u.u64),
1551 U2H(stats[ST_F_REUSE].u.u64),
1552 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1553 (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 +01001554 U2H(stats[ST_F_REQ_TOT].u.u64),
1555 U2H(stats[ST_F_HRSP_1XX].u.u64),
1556 U2H(stats[ST_F_HRSP_2XX].u.u64),
1557 U2H(stats[ST_F_COMP_RSP].u.u64),
1558 stats[ST_F_HRSP_2XX].u.u64 ?
1559 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1560 U2H(stats[ST_F_HRSP_3XX].u.u64),
1561 U2H(stats[ST_F_HRSP_4XX].u.u64),
1562 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001563 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001564 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1565 U2H(stats[ST_F_CACHE_HITS].u.u64),
1566 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1567 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001568 U2H(stats[ST_F_WREW].u.u64),
1569 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001570 }
1571
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001572 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1573 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1574 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1575 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1576 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001577 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001578 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1579 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1580 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1581 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001582
1583 chunk_appendf(out,
1584 "</table></div></u></td>"
1585 /* sessions: lbtot, last */
1586 "<td>%s</td><td>%s</td>"
1587 /* bytes: in */
1588 "<td>%s</td>"
1589 "",
1590 U2H(stats[ST_F_LBTOT].u.u64),
1591 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1592 U2H(stats[ST_F_BIN].u.u64));
1593
1594 chunk_appendf(out,
1595 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1596 "<td>%s%s<div class=tips><table class=det>"
1597 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1598 "<tr><th>Compression in:</th><td>%s</td></tr>"
1599 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1600 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1601 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1602 "</table></div>%s</td>",
1603 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1604 U2H(stats[ST_F_BOUT].u.u64),
1605 U2H(stats[ST_F_BOUT].u.u64),
1606 U2H(stats[ST_F_COMP_IN].u.u64),
1607 U2H(stats[ST_F_COMP_OUT].u.u64),
1608 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1609 U2H(stats[ST_F_COMP_BYP].u.u64),
1610 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1611 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,
1612 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1613
1614 chunk_appendf(out,
1615 /* denied: req, resp */
1616 "<td>%s</td><td>%s</td>"
1617 /* errors : request, connect */
1618 "<td></td><td>%s</td>"
1619 /* errors : response */
1620 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1621 /* warnings: retries, redispatches */
1622 "<td>%lld</td><td>%lld</td>"
1623 /* backend status: reflect backend status (up/down): we display UP
1624 * if the backend has known working servers or if it has no server at
1625 * all (eg: for stats). Then we display the total weight, number of
1626 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001627 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001628 "<td class=ac>%d</td><td class=ac>%d</td>"
1629 "",
1630 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1631 U2H(stats[ST_F_ECON].u.u64),
1632 U2H(stats[ST_F_ERESP].u.u64),
1633 (long long)stats[ST_F_CLI_ABRT].u.u64,
1634 (long long)stats[ST_F_SRV_ABRT].u.u64,
1635 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1636 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1637 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 +02001638 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001639 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1640
1641 chunk_appendf(out,
1642 /* rest of backend: nothing, down transitions, total downtime, throttle */
1643 "<td class=ac>&nbsp;</td><td>%d</td>"
1644 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001645 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001646 stats[ST_F_CHKDOWN].u.u32,
1647 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001648
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001649 if (flags & STAT_SHMODULES) {
1650 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001651 chunk_appendf(out, "<td>");
1652
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001653 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001654 chunk_appendf(out,
1655 "<u>%s<div class=tips><table class=det>",
1656 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001657 for (j = 0; j < mod->stats_count; ++j) {
1658 chunk_appendf(out,
1659 "<tr><th>%s</th><td>%s</td></tr>",
1660 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1661 ++i;
1662 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001663 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001664 } else {
1665 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001666 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001667
1668 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001669 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001670 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001671
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001672 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001673 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001674
William Lallemand74c24fb2016-11-21 17:18:36 +01001675 return 1;
1676}
1677
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001678int stats_dump_one_line(const struct field *stats, size_t stats_count,
1679 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001680{
Willy Tarreau91cefca2022-05-03 17:08:29 +02001681 struct show_stat_ctx *ctx = appctx->svcctx;
Simon Horman05ee2132017-01-04 09:37:25 +01001682 int ret;
1683
Willy Tarreau91cefca2022-05-03 17:08:29 +02001684 if (ctx->flags & STAT_FMT_HTML)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001685 ret = stats_dump_fields_html(&trash_chunk, stats, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001686 else if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001687 ret = stats_dump_fields_typed(&trash_chunk, stats, stats_count, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001688 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001689 ret = stats_dump_fields_json(&trash_chunk, stats, stats_count, ctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001690 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01001691 ret = stats_dump_fields_csv(&trash_chunk, stats, stats_count, ctx);
Simon Horman05ee2132017-01-04 09:37:25 +01001692
Simon Horman05ee2132017-01-04 09:37:25 +01001693 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001694}
1695
William Dauchy0ef54392021-01-17 18:27:45 +01001696/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001697 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1698 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1699 * this value, or if the selected field is not implemented for frontends, the
1700 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001701 */
William Dauchy0ef54392021-01-17 18:27:45 +01001702int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1703 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001704{
William Dauchy0ef54392021-01-17 18:27:45 +01001705 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001706
William Lallemand74c24fb2016-11-21 17:18:36 +01001707 if (len < ST_F_TOTAL_FIELDS)
1708 return 0;
1709
William Dauchy0ef54392021-01-17 18:27:45 +01001710 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001711 struct field metric = { 0 };
1712
William Dauchy0ef54392021-01-17 18:27:45 +01001713 switch (current_field) {
1714 case ST_F_PXNAME:
1715 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1716 break;
1717 case ST_F_SVNAME:
1718 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1719 break;
1720 case ST_F_MODE:
1721 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1722 break;
1723 case ST_F_SCUR:
1724 metric = mkf_u32(0, px->feconn);
1725 break;
1726 case ST_F_SMAX:
1727 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1728 break;
1729 case ST_F_SLIM:
1730 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1731 break;
1732 case ST_F_STOT:
1733 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1734 break;
1735 case ST_F_BIN:
1736 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1737 break;
1738 case ST_F_BOUT:
1739 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1740 break;
1741 case ST_F_DREQ:
1742 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1743 break;
1744 case ST_F_DRESP:
1745 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1746 break;
1747 case ST_F_EREQ:
1748 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1749 break;
1750 case ST_F_DCON:
1751 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1752 break;
1753 case ST_F_DSES:
1754 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1755 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001756 case ST_F_STATUS: {
1757 const char *state;
1758
1759 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1760 state = "STOP";
1761 else if (px->flags & PR_FL_PAUSED)
1762 state = "PAUSED";
1763 else
1764 state = "OPEN";
1765 metric = mkf_str(FO_STATUS, state);
William Dauchy0ef54392021-01-17 18:27:45 +01001766 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001767 }
William Dauchy0ef54392021-01-17 18:27:45 +01001768 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001769 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001770 break;
1771 case ST_F_IID:
1772 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1773 break;
1774 case ST_F_SID:
1775 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1776 break;
1777 case ST_F_TYPE:
1778 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1779 break;
1780 case ST_F_RATE:
1781 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1782 break;
1783 case ST_F_RATE_LIM:
1784 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1785 break;
1786 case ST_F_RATE_MAX:
1787 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1788 break;
1789 case ST_F_WREW:
1790 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1791 break;
1792 case ST_F_EINT:
1793 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1794 break;
1795 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001796 if (px->mode == PR_MODE_HTTP)
1797 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001798 break;
1799 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001800 if (px->mode == PR_MODE_HTTP)
1801 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001802 break;
1803 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001804 if (px->mode == PR_MODE_HTTP)
1805 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001806 break;
1807 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001808 if (px->mode == PR_MODE_HTTP)
1809 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001810 break;
1811 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001812 if (px->mode == PR_MODE_HTTP)
1813 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001814 break;
1815 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001816 if (px->mode == PR_MODE_HTTP)
1817 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001818 break;
1819 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001820 if (px->mode == PR_MODE_HTTP)
1821 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001822 break;
1823 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001824 if (px->mode == PR_MODE_HTTP)
1825 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001826 break;
1827 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001828 if (px->mode == PR_MODE_HTTP)
1829 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001830 break;
1831 case ST_F_REQ_RATE:
1832 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1833 break;
1834 case ST_F_REQ_RATE_MAX:
1835 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1836 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001837 case ST_F_REQ_TOT: {
1838 int i;
1839 uint64_t total_req;
1840 size_t nb_reqs =
1841 sizeof(px->fe_counters.p.http.cum_req) / sizeof(*px->fe_counters.p.http.cum_req);
1842
1843 total_req = 0;
1844 for (i = 0; i < nb_reqs; i++)
1845 total_req += px->fe_counters.p.http.cum_req[i];
1846 metric = mkf_u64(FN_COUNTER, total_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001847 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001848 }
William Dauchy0ef54392021-01-17 18:27:45 +01001849 case ST_F_COMP_IN:
1850 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1851 break;
1852 case ST_F_COMP_OUT:
1853 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1854 break;
1855 case ST_F_COMP_BYP:
1856 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1857 break;
1858 case ST_F_COMP_RSP:
1859 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1860 break;
1861 case ST_F_CONN_RATE:
1862 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1863 break;
1864 case ST_F_CONN_RATE_MAX:
1865 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1866 break;
1867 case ST_F_CONN_TOT:
1868 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1869 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001870 case ST_F_SESS_OTHER: {
1871 int i;
1872 uint64_t total_sess;
1873 size_t nb_sess =
1874 sizeof(px->fe_counters.cum_sess_ver) / sizeof(*px->fe_counters.cum_sess_ver);
1875
1876 total_sess = px->fe_counters.cum_sess;
1877 for (i = 0; i < nb_sess; i++)
1878 total_sess -= px->fe_counters.cum_sess_ver[i];
Frédéric Lécailled97d1d72023-02-06 09:23:56 +01001879 total_sess = (int64_t)total_sess < 0 ? 0 : total_sess;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001880 metric = mkf_u64(FN_COUNTER, total_sess);
1881 break;
1882 }
1883 case ST_F_H1SESS:
1884 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[0]);
1885 break;
1886 case ST_F_H2SESS:
1887 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[1]);
1888 break;
1889 case ST_F_H3SESS:
1890 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[2]);
1891 break;
1892 case ST_F_REQ_OTHER:
1893 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[0]);
1894 break;
1895 case ST_F_H1REQ:
1896 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[1]);
1897 break;
1898 case ST_F_H2REQ:
1899 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[2]);
1900 break;
1901 case ST_F_H3REQ:
1902 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[3]);
1903 break;
William Dauchy0ef54392021-01-17 18:27:45 +01001904 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001905 /* not used for frontends. If a specific metric
1906 * is requested, return an error. Otherwise continue.
1907 */
1908 if (selected_field != NULL)
1909 return 0;
1910 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001911 }
1912 stats[current_field] = metric;
1913 if (selected_field != NULL)
1914 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001915 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001916 return 1;
1917}
1918
Christopher Fauleta8b76842022-12-16 15:08:36 +01001919/* Dumps a frontend's line to the local trash buffer for the current proxy <px>
1920 * and uses the state from stream connector <sc>. The caller is responsible for
1921 * clearing the local trash buffer if needed. Returns non-zero if it emits
1922 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001923 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001924static int stats_dump_fe_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001925{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001926 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001927 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001928 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1929 struct stats_module *mod;
1930 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001931
1932 if (!(px->cap & PR_CAP_FE))
1933 return 0;
1934
Willy Tarreau91cefca2022-05-03 17:08:29 +02001935 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_FE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001936 return 0;
1937
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001938 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1939
William Dauchy0ef54392021-01-17 18:27:45 +01001940 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001941 return 0;
1942
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001943 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1944 void *counters;
1945
1946 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1947 stats_count += mod->stats_count;
1948 continue;
1949 }
1950
1951 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1952 mod->fill_stats(counters, stats + stats_count);
1953 stats_count += mod->stats_count;
1954 }
1955
1956 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001957}
1958
William Dauchy655e14e2021-02-14 23:22:54 +01001959/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1960 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1961 * this length is less then this value, the function returns 0, otherwise, it
1962 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1963 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001964 */
1965int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001966 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001967{
William Dauchy655e14e2021-02-14 23:22:54 +01001968 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001969 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001970
1971 if (len < ST_F_TOTAL_FIELDS)
1972 return 0;
1973
1974 if (!l->counters)
1975 return 0;
1976
1977 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001978
William Dauchy655e14e2021-02-14 23:22:54 +01001979 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1980 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001981
William Dauchy655e14e2021-02-14 23:22:54 +01001982 switch (current_field) {
1983 case ST_F_PXNAME:
1984 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1985 break;
1986 case ST_F_SVNAME:
1987 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1988 break;
1989 case ST_F_MODE:
1990 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1991 break;
1992 case ST_F_SCUR:
1993 metric = mkf_u32(0, l->nbconn);
1994 break;
1995 case ST_F_SMAX:
1996 metric = mkf_u32(FN_MAX, l->counters->conn_max);
1997 break;
1998 case ST_F_SLIM:
Willy Tarreau758c69d2023-01-12 18:59:37 +01001999 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->bind_conf->maxconn);
William Dauchy655e14e2021-02-14 23:22:54 +01002000 break;
2001 case ST_F_STOT:
2002 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
2003 break;
2004 case ST_F_BIN:
2005 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
2006 break;
2007 case ST_F_BOUT:
2008 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
2009 break;
2010 case ST_F_DREQ:
2011 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
2012 break;
2013 case ST_F_DRESP:
2014 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
2015 break;
2016 case ST_F_EREQ:
2017 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
2018 break;
2019 case ST_F_DCON:
2020 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
2021 break;
2022 case ST_F_DSES:
2023 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
2024 break;
2025 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01002026 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01002027 break;
2028 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002029 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01002030 break;
2031 case ST_F_IID:
2032 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2033 break;
2034 case ST_F_SID:
2035 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
2036 break;
2037 case ST_F_TYPE:
2038 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
2039 break;
2040 case ST_F_WREW:
2041 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
2042 break;
2043 case ST_F_EINT:
2044 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
2045 break;
2046 case ST_F_ADDR:
2047 if (flags & STAT_SHLGNDS) {
2048 char str[INET6_ADDRSTRLEN];
2049 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01002050
William Dauchy655e14e2021-02-14 23:22:54 +01002051 port = get_host_port(&l->rx.addr);
2052 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
2053 case AF_INET:
2054 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2055 chunk_appendf(out, "%s:%d", str, port);
2056 break;
2057 case AF_INET6:
2058 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2059 chunk_appendf(out, "[%s]:%d", str, port);
2060 break;
2061 case AF_UNIX:
2062 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2063 break;
2064 case -1:
2065 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2066 chunk_strcat(out, strerror(errno));
2067 break;
2068 default: /* address family not supported */
2069 break;
2070 }
2071 }
2072 break;
2073 default:
2074 /* not used for listen. If a specific metric
2075 * is requested, return an error. Otherwise continue.
2076 */
2077 if (selected_field != NULL)
2078 return 0;
2079 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002080 }
William Dauchy655e14e2021-02-14 23:22:54 +01002081 stats[current_field] = metric;
2082 if (selected_field != NULL)
2083 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002084 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002085 return 1;
2086}
2087
Christopher Fauleta8b76842022-12-16 15:08:36 +01002088/* Dumps a line for listener <l> and proxy <px> to the local trash buffer and
2089 * uses the state from stream connector <sc>. The caller is responsible for
2090 * clearing the local trash buffer if needed. Returns non-zero if it emits
2091 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002092 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002093static int stats_dump_li_stats(struct stconn *sc, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01002094{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002095 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002096 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002097 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2098 struct stats_module *mod;
2099 size_t stats_count = ST_F_TOTAL_FIELDS;
2100
2101 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002102
Willy Tarreau91cefca2022-05-03 17:08:29 +02002103 if (!stats_fill_li_stats(px, l, ctx->flags, stats,
William Dauchy655e14e2021-02-14 23:22:54 +01002104 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002105 return 0;
2106
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002107 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2108 void *counters;
2109
2110 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
2111 stats_count += mod->stats_count;
2112 continue;
2113 }
2114
2115 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2116 mod->fill_stats(counters, stats + stats_count);
2117 stats_count += mod->stats_count;
2118 }
2119
2120 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002121}
2122
2123enum srv_stats_state {
2124 SRV_STATS_STATE_DOWN = 0,
2125 SRV_STATS_STATE_DOWN_AGENT,
2126 SRV_STATS_STATE_GOING_UP,
2127 SRV_STATS_STATE_UP_GOING_DOWN,
2128 SRV_STATS_STATE_UP,
2129 SRV_STATS_STATE_NOLB_GOING_DOWN,
2130 SRV_STATS_STATE_NOLB,
2131 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2132 SRV_STATS_STATE_DRAIN,
2133 SRV_STATS_STATE_DRAIN_AGENT,
2134 SRV_STATS_STATE_NO_CHECK,
2135
2136 SRV_STATS_STATE_COUNT, /* Must be last */
2137};
2138
2139static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2140 [SRV_STATS_STATE_DOWN] = "DOWN",
2141 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2142 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2143 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2144 [SRV_STATS_STATE_UP] = "UP",
2145 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2146 [SRV_STATS_STATE_NOLB] = "NOLB",
2147 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2148 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2149 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2150 [SRV_STATS_STATE_NO_CHECK] = "no check"
2151};
2152
William Dauchyd3a9a492021-01-25 17:29:03 +01002153/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002154 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002155static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2156 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002157{
Emeric Brun52a91d32017-08-31 14:41:55 +02002158 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002159 if ((ref->check.state & CHK_ST_ENABLED) &&
2160 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002161 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002162 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002163 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002164 }
2165
Emeric Brun52a91d32017-08-31 14:41:55 +02002166 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002167 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002168 *state = SRV_STATS_STATE_DRAIN_AGENT;
2169 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2170 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002171 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002172 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002173 }
2174
William Dauchyd3a9a492021-01-25 17:29:03 +01002175 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2176 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002177 }
2178 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002179 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002180 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2181 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002182 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002183 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002184 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002185 }
2186 }
2187 else { /* stopped */
2188 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002189 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002190 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002191 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002192 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002193 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002194 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002195 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002196 }
2197 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002198}
William Lallemand74c24fb2016-11-21 17:18:36 +01002199
William Dauchyd3a9a492021-01-25 17:29:03 +01002200/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2201 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2202 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2203 * this value, or if the selected field is not implemented for servers, the
2204 * function returns 0, otherwise, it returns 1. <flags> can take the value
2205 * STAT_SHLGNDS.
2206 */
2207int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2208 struct field *stats, int len,
2209 enum stat_field *selected_field)
2210{
2211 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2212 struct server *via = sv->track ? sv->track : sv;
2213 struct server *ref = via;
2214 enum srv_stats_state state = 0;
2215 char str[INET6_ADDRSTRLEN];
2216 struct buffer *out = get_trash_chunk();
2217 char *fld_status;
2218 long long srv_samples_counter;
2219 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002220
William Dauchyd3a9a492021-01-25 17:29:03 +01002221 if (len < ST_F_TOTAL_FIELDS)
2222 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002223
William Dauchyd3a9a492021-01-25 17:29:03 +01002224 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002225
William Dauchyd3a9a492021-01-25 17:29:03 +01002226 /* compute state for later use */
2227 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2228 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2229 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2230 /* we have "via" which is the tracked server as described in the configuration,
2231 * and "ref" which is the checked server and the end of the chain.
2232 */
2233 while (ref->track)
2234 ref = ref->track;
2235 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002236 }
2237
William Dauchyd3a9a492021-01-25 17:29:03 +01002238 /* compue time values for later use */
2239 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2240 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2241 *selected_field == ST_F_TTIME) {
2242 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2243 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2244 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002245 }
2246
William Dauchyd3a9a492021-01-25 17:29:03 +01002247 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2248 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002249
William Dauchyd3a9a492021-01-25 17:29:03 +01002250 switch (current_field) {
2251 case ST_F_PXNAME:
2252 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2253 break;
2254 case ST_F_SVNAME:
2255 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2256 break;
2257 case ST_F_MODE:
2258 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002259 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002260 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002261 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002262 break;
2263 case ST_F_QMAX:
2264 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2265 break;
2266 case ST_F_SCUR:
2267 metric = mkf_u32(0, sv->cur_sess);
2268 break;
2269 case ST_F_SMAX:
2270 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2271 break;
2272 case ST_F_SLIM:
2273 if (sv->maxconn)
2274 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2275 break;
2276 case ST_F_SRV_ICUR:
2277 metric = mkf_u32(0, sv->curr_idle_conns);
2278 break;
2279 case ST_F_SRV_ILIM:
2280 if (sv->max_idle_conns != -1)
2281 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2282 break;
2283 case ST_F_STOT:
2284 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2285 break;
2286 case ST_F_BIN:
2287 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2288 break;
2289 case ST_F_BOUT:
2290 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2291 break;
2292 case ST_F_DRESP:
2293 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2294 break;
2295 case ST_F_ECON:
2296 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2297 break;
2298 case ST_F_ERESP:
2299 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2300 break;
2301 case ST_F_WRETR:
2302 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2303 break;
2304 case ST_F_WREDIS:
2305 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2306 break;
2307 case ST_F_WREW:
2308 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2309 break;
2310 case ST_F_EINT:
2311 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2312 break;
2313 case ST_F_CONNECT:
2314 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2315 break;
2316 case ST_F_REUSE:
2317 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2318 break;
2319 case ST_F_IDLE_CONN_CUR:
2320 metric = mkf_u32(0, sv->curr_idle_nb);
2321 break;
2322 case ST_F_SAFE_CONN_CUR:
2323 metric = mkf_u32(0, sv->curr_safe_nb);
2324 break;
2325 case ST_F_USED_CONN_CUR:
2326 metric = mkf_u32(0, sv->curr_used_conns);
2327 break;
2328 case ST_F_NEED_CONN_EST:
2329 metric = mkf_u32(0, sv->est_need_conns);
2330 break;
2331 case ST_F_STATUS:
2332 fld_status = chunk_newstr(out);
2333 if (sv->cur_admin & SRV_ADMF_RMAINT)
2334 chunk_appendf(out, "MAINT (resolution)");
2335 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2336 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2337 else if (sv->cur_admin & SRV_ADMF_MAINT)
2338 chunk_appendf(out, "MAINT");
2339 else
2340 chunk_appendf(out,
2341 srv_hlt_st[state],
2342 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2343 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002344
William Dauchyd3a9a492021-01-25 17:29:03 +01002345 metric = mkf_str(FO_STATUS, fld_status);
2346 break;
2347 case ST_F_LASTCHG:
2348 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
2349 break;
2350 case ST_F_WEIGHT:
2351 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2352 break;
2353 case ST_F_UWEIGHT:
2354 metric = mkf_u32(FN_AVG, sv->uweight);
2355 break;
2356 case ST_F_ACT:
2357 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2358 break;
2359 case ST_F_BCK:
2360 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2361 break;
2362 case ST_F_CHKFAIL:
2363 if (sv->check.state & CHK_ST_ENABLED)
2364 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2365 break;
2366 case ST_F_CHKDOWN:
2367 if (sv->check.state & CHK_ST_ENABLED)
2368 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2369 break;
2370 case ST_F_DOWNTIME:
2371 if (sv->check.state & CHK_ST_ENABLED)
2372 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2373 break;
2374 case ST_F_QLIMIT:
2375 if (sv->maxqueue)
2376 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2377 break;
2378 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002379 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002380 break;
2381 case ST_F_IID:
2382 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2383 break;
2384 case ST_F_SID:
2385 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2386 break;
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01002387 case ST_F_SRID:
2388 metric = mkf_u32(FN_COUNTER, sv->rid);
2389 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002390 case ST_F_THROTTLE:
2391 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2392 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2393 break;
2394 case ST_F_LBTOT:
2395 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2396 break;
2397 case ST_F_TRACKED:
2398 if (sv->track) {
2399 char *fld_track = chunk_newstr(out);
2400 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2401 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2402 }
2403 break;
2404 case ST_F_TYPE:
2405 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2406 break;
2407 case ST_F_RATE:
2408 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2409 break;
2410 case ST_F_RATE_MAX:
2411 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2412 break;
2413 case ST_F_CHECK_STATUS:
2414 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2415 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002416
William Dauchyd3a9a492021-01-25 17:29:03 +01002417 fld_chksts = chunk_newstr(out);
2418 chunk_strcat(out, "* "); // for check in progress
2419 chunk_strcat(out, get_check_status_info(sv->check.status));
2420 if (!(sv->check.state & CHK_ST_INPROGRESS))
2421 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002422 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002423 }
2424 break;
2425 case ST_F_CHECK_CODE:
2426 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2427 sv->check.status >= HCHK_STATUS_L57DATA)
2428 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2429 break;
2430 case ST_F_CHECK_DURATION:
2431 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2432 sv->check.status >= HCHK_STATUS_CHECKED)
Aurelien DARRAGONb118f2f2022-12-07 14:52:10 +01002433 metric = mkf_u64(FN_DURATION, MAX(sv->check.duration, 0));
William Dauchyd3a9a492021-01-25 17:29:03 +01002434 break;
2435 case ST_F_CHECK_DESC:
2436 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2437 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2438 break;
2439 case ST_F_LAST_CHK:
2440 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2441 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2442 break;
2443 case ST_F_CHECK_RISE:
2444 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2445 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2446 break;
2447 case ST_F_CHECK_FALL:
2448 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2449 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2450 break;
2451 case ST_F_CHECK_HEALTH:
2452 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2453 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2454 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002455 case ST_F_AGENT_STATUS:
2456 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2457 const char *fld_chksts;
2458
2459 fld_chksts = chunk_newstr(out);
2460 chunk_strcat(out, "* "); // for check in progress
2461 chunk_strcat(out, get_check_status_info(sv->agent.status));
2462 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2463 fld_chksts += 2; // skip "* "
2464 metric = mkf_str(FN_OUTPUT, fld_chksts);
2465 }
2466 break;
2467 case ST_F_AGENT_CODE:
2468 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2469 (sv->agent.status >= HCHK_STATUS_L57DATA))
2470 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2471 break;
2472 case ST_F_AGENT_DURATION:
2473 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2474 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2475 break;
2476 case ST_F_AGENT_DESC:
2477 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2478 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2479 break;
2480 case ST_F_LAST_AGT:
2481 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2482 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2483 break;
2484 case ST_F_AGENT_RISE:
2485 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2486 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2487 break;
2488 case ST_F_AGENT_FALL:
2489 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2490 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2491 break;
2492 case ST_F_AGENT_HEALTH:
2493 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2494 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2495 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002496 case ST_F_REQ_TOT:
2497 if (px->mode == PR_MODE_HTTP)
2498 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2499 break;
2500 case ST_F_HRSP_1XX:
2501 if (px->mode == PR_MODE_HTTP)
2502 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2503 break;
2504 case ST_F_HRSP_2XX:
2505 if (px->mode == PR_MODE_HTTP)
2506 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2507 break;
2508 case ST_F_HRSP_3XX:
2509 if (px->mode == PR_MODE_HTTP)
2510 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2511 break;
2512 case ST_F_HRSP_4XX:
2513 if (px->mode == PR_MODE_HTTP)
2514 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2515 break;
2516 case ST_F_HRSP_5XX:
2517 if (px->mode == PR_MODE_HTTP)
2518 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2519 break;
2520 case ST_F_HRSP_OTHER:
2521 if (px->mode == PR_MODE_HTTP)
2522 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2523 break;
2524 case ST_F_HANAFAIL:
2525 if (ref->observe)
2526 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2527 break;
2528 case ST_F_CLI_ABRT:
2529 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2530 break;
2531 case ST_F_SRV_ABRT:
2532 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2533 break;
2534 case ST_F_LASTSESS:
2535 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2536 break;
2537 case ST_F_QTIME:
2538 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2539 break;
2540 case ST_F_CTIME:
2541 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2542 break;
2543 case ST_F_RTIME:
2544 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2545 break;
2546 case ST_F_TTIME:
2547 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2548 break;
2549 case ST_F_QT_MAX:
2550 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2551 break;
2552 case ST_F_CT_MAX:
2553 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2554 break;
2555 case ST_F_RT_MAX:
2556 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2557 break;
2558 case ST_F_TT_MAX:
2559 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2560 break;
2561 case ST_F_ADDR:
2562 if (flags & STAT_SHLGNDS) {
2563 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2564 case AF_INET:
2565 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2566 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2567 break;
2568 case AF_INET6:
2569 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2570 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2571 break;
2572 case AF_UNIX:
2573 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2574 break;
2575 case -1:
2576 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2577 chunk_strcat(out, strerror(errno));
2578 break;
2579 default: /* address family not supported */
2580 break;
2581 }
2582 }
2583 break;
2584 case ST_F_COOKIE:
2585 if (flags & STAT_SHLGNDS && sv->cookie)
2586 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2587 break;
2588 default:
2589 /* not used for servers. If a specific metric
2590 * is requested, return an error. Otherwise continue.
2591 */
2592 if (selected_field != NULL)
2593 return 0;
2594 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002595 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002596 stats[current_field] = metric;
2597 if (selected_field != NULL)
2598 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002599 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002600 return 1;
2601}
2602
Christopher Fauleta8b76842022-12-16 15:08:36 +01002603/* Dumps a line for server <sv> and proxy <px> to the local trash vbuffer and
2604 * uses the state from stream connector <sc>, and server state <state>. The
2605 * caller is responsible for clearing the local trash buffer if needed. Returns
2606 * non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002607 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002608static int stats_dump_sv_stats(struct stconn *sc, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002609{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002610 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002611 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002612 struct stats_module *mod;
2613 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2614 size_t stats_count = ST_F_TOTAL_FIELDS;
2615
2616 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002617
Willy Tarreau91cefca2022-05-03 17:08:29 +02002618 if (!stats_fill_sv_stats(px, sv, ctx->flags, stats,
William Dauchyd3a9a492021-01-25 17:29:03 +01002619 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002620 return 0;
2621
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002622 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2623 void *counters;
2624
2625 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2626 continue;
2627
2628 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2629 stats_count += mod->stats_count;
2630 continue;
2631 }
2632
2633 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2634 mod->fill_stats(counters, stats + stats_count);
2635 stats_count += mod->stats_count;
2636 }
2637
2638 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002639}
2640
William Dauchyda3b4662021-01-25 17:29:01 +01002641/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002642 */
William Dauchyda3b4662021-01-25 17:29:01 +01002643static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002644{
William Dauchyda3b4662021-01-25 17:29:01 +01002645 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002646 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002647
William Dauchyda3b4662021-01-25 17:29:01 +01002648 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002649 for (srv = px->srv; srv; srv = srv->next) {
2650 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002651 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002652 if (srv_currently_usable(srv) &&
2653 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002654 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002655 }
William Dauchyda3b4662021-01-25 17:29:01 +01002656 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002657 }
2658
Willy Tarreaubd715102020-10-23 22:44:30 +02002659 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2660 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002661 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002662 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2663
William Dauchyda3b4662021-01-25 17:29:01 +01002664 /* use tmp variable then assign result to make gcc happy */
2665 *nbup = nbup_tmp;
2666 *nbsrv = nbsrv_tmp;
2667 *totuw = totuw_tmp;
2668}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002669
William Dauchyda3b4662021-01-25 17:29:01 +01002670/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2671 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2672 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2673 * this value, or if the selected field is not implemented for backends, the
2674 * function returns 0, otherwise, it returns 1. <flags> can take the value
2675 * STAT_SHLGNDS.
2676 */
2677int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2678 enum stat_field *selected_field)
2679{
2680 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2681 long long be_samples_counter;
2682 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2683 struct buffer *out = get_trash_chunk();
2684 int nbup, nbsrv, totuw;
2685 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002686
William Dauchyda3b4662021-01-25 17:29:01 +01002687 if (len < ST_F_TOTAL_FIELDS)
2688 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002689
William Dauchyda3b4662021-01-25 17:29:01 +01002690 nbup = nbsrv = totuw = 0;
2691 /* some srv values compute for later if we either select all fields or
2692 * need them for one of the mentioned ones */
2693 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2694 *selected_field == ST_F_UWEIGHT)
2695 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002696
William Dauchyda3b4662021-01-25 17:29:01 +01002697 /* same here but specific to time fields */
2698 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2699 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2700 *selected_field == ST_F_TTIME) {
2701 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2702 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2703 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002704 }
2705
William Dauchyda3b4662021-01-25 17:29:01 +01002706 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2707 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002708
William Dauchyda3b4662021-01-25 17:29:01 +01002709 switch (current_field) {
2710 case ST_F_PXNAME:
2711 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2712 break;
2713 case ST_F_SVNAME:
2714 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2715 break;
2716 case ST_F_MODE:
2717 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2718 break;
2719 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002720 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002721 break;
2722 case ST_F_QMAX:
2723 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2724 break;
2725 case ST_F_SCUR:
2726 metric = mkf_u32(0, px->beconn);
2727 break;
2728 case ST_F_SMAX:
2729 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2730 break;
2731 case ST_F_SLIM:
2732 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2733 break;
2734 case ST_F_STOT:
2735 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2736 break;
2737 case ST_F_BIN:
2738 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2739 break;
2740 case ST_F_BOUT:
2741 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2742 break;
2743 case ST_F_DREQ:
2744 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2745 break;
2746 case ST_F_DRESP:
2747 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2748 break;
2749 case ST_F_ECON:
2750 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2751 break;
2752 case ST_F_ERESP:
2753 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2754 break;
2755 case ST_F_WRETR:
2756 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2757 break;
2758 case ST_F_WREDIS:
2759 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2760 break;
2761 case ST_F_WREW:
2762 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2763 break;
2764 case ST_F_EINT:
2765 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2766 break;
2767 case ST_F_CONNECT:
2768 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2769 break;
2770 case ST_F_REUSE:
2771 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2772 break;
2773 case ST_F_STATUS:
2774 fld = chunk_newstr(out);
2775 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2776 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2777 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2778 metric = mkf_str(FO_STATUS, fld);
2779 break;
Cedric Paillet7d6644e2022-12-08 09:17:00 +00002780 case ST_F_AGG_SRV_CHECK_STATUS: // DEPRECATED
2781 case ST_F_AGG_SRV_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +01002782 metric = mkf_u32(FN_GAUGE, 0);
2783 break;
Cedric Paillete06e31e2022-12-08 09:17:01 +00002784 case ST_F_AGG_CHECK_STATUS:
2785 metric = mkf_u32(FN_GAUGE, 0);
2786 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002787 case ST_F_WEIGHT:
2788 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2789 break;
2790 case ST_F_UWEIGHT:
2791 metric = mkf_u32(FN_AVG, totuw);
2792 break;
2793 case ST_F_ACT:
2794 metric = mkf_u32(0, px->srv_act);
2795 break;
2796 case ST_F_BCK:
2797 metric = mkf_u32(0, px->srv_bck);
2798 break;
2799 case ST_F_CHKDOWN:
2800 metric = mkf_u64(FN_COUNTER, px->down_trans);
2801 break;
2802 case ST_F_LASTCHG:
2803 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2804 break;
2805 case ST_F_DOWNTIME:
2806 if (px->srv)
2807 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2808 break;
2809 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002810 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002811 break;
2812 case ST_F_IID:
2813 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2814 break;
2815 case ST_F_SID:
2816 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2817 break;
2818 case ST_F_LBTOT:
2819 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2820 break;
2821 case ST_F_TYPE:
2822 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2823 break;
2824 case ST_F_RATE:
2825 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2826 break;
2827 case ST_F_RATE_MAX:
2828 metric = mkf_u32(0, px->be_counters.sps_max);
2829 break;
2830 case ST_F_COOKIE:
2831 if (flags & STAT_SHLGNDS && px->cookie_name)
2832 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2833 break;
2834 case ST_F_ALGO:
2835 if (flags & STAT_SHLGNDS)
2836 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2837 break;
2838 case ST_F_REQ_TOT:
2839 if (px->mode == PR_MODE_HTTP)
2840 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2841 break;
2842 case ST_F_HRSP_1XX:
2843 if (px->mode == PR_MODE_HTTP)
2844 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2845 break;
2846 case ST_F_HRSP_2XX:
2847 if (px->mode == PR_MODE_HTTP)
2848 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2849 break;
2850 case ST_F_HRSP_3XX:
2851 if (px->mode == PR_MODE_HTTP)
2852 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2853 break;
2854 case ST_F_HRSP_4XX:
2855 if (px->mode == PR_MODE_HTTP)
2856 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2857 break;
2858 case ST_F_HRSP_5XX:
2859 if (px->mode == PR_MODE_HTTP)
2860 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2861 break;
2862 case ST_F_HRSP_OTHER:
2863 if (px->mode == PR_MODE_HTTP)
2864 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2865 break;
2866 case ST_F_CACHE_LOOKUPS:
2867 if (px->mode == PR_MODE_HTTP)
2868 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2869 break;
2870 case ST_F_CACHE_HITS:
2871 if (px->mode == PR_MODE_HTTP)
2872 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2873 break;
2874 case ST_F_CLI_ABRT:
2875 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2876 break;
2877 case ST_F_SRV_ABRT:
2878 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2879 break;
2880 case ST_F_COMP_IN:
2881 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
2882 break;
2883 case ST_F_COMP_OUT:
2884 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
2885 break;
2886 case ST_F_COMP_BYP:
2887 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
2888 break;
2889 case ST_F_COMP_RSP:
2890 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2891 break;
2892 case ST_F_LASTSESS:
2893 metric = mkf_s32(FN_AGE, be_lastsession(px));
2894 break;
2895 case ST_F_QTIME:
2896 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2897 break;
2898 case ST_F_CTIME:
2899 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2900 break;
2901 case ST_F_RTIME:
2902 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2903 break;
2904 case ST_F_TTIME:
2905 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2906 break;
2907 case ST_F_QT_MAX:
2908 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2909 break;
2910 case ST_F_CT_MAX:
2911 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2912 break;
2913 case ST_F_RT_MAX:
2914 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2915 break;
2916 case ST_F_TT_MAX:
2917 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2918 break;
2919 default:
2920 /* not used for backends. If a specific metric
2921 * is requested, return an error. Otherwise continue.
2922 */
2923 if (selected_field != NULL)
2924 return 0;
2925 continue;
2926 }
2927 stats[current_field] = metric;
2928 if (selected_field != NULL)
2929 break;
2930 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002931 return 1;
2932}
2933
Christopher Fauleta8b76842022-12-16 15:08:36 +01002934/* Dumps a line for backend <px> to the local trash buffer for and uses the
2935 * state from stream interface <si>. The caller is responsible for clearing the
2936 * local trash buffer if needed. Returns non-zero if it emits anything, zero
2937 * otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002938 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002939static int stats_dump_be_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002940{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002941 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002942 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002943 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2944 struct stats_module *mod;
2945 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002946
2947 if (!(px->cap & PR_CAP_BE))
2948 return 0;
2949
Willy Tarreau91cefca2022-05-03 17:08:29 +02002950 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_BE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01002951 return 0;
2952
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002953 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2954
Willy Tarreau91cefca2022-05-03 17:08:29 +02002955 if (!stats_fill_be_stats(px, ctx->flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002956 return 0;
2957
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002958 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2959 struct extra_counters *counters;
2960
2961 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2962 continue;
2963
2964 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2965 stats_count += mod->stats_count;
2966 continue;
2967 }
2968
2969 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2970 mod->fill_stats(counters, stats + stats_count);
2971 stats_count += mod->stats_count;
2972 }
2973
2974 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002975}
2976
Christopher Fauleta8b76842022-12-16 15:08:36 +01002977/* Dumps the HTML table header for proxy <px> to the local trash buffer for and
2978 * uses the state from stream connector <sc> and per-uri parameters <uri>. The
2979 * caller is responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01002980 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002981static void stats_dump_html_px_hdr(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002982{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002983 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002984 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01002985 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002986 struct stats_module *mod;
2987 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002988
Willy Tarreau91cefca2022-05-03 17:08:29 +02002989 if (px->cap & PR_CAP_BE && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002990 /* A form to enable/disable this proxy servers */
2991
Willy Tarreau91cefca2022-05-03 17:08:29 +02002992 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002993 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02002994 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02002995 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002996
William Lallemand74c24fb2016-11-21 17:18:36 +01002997 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002998 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
2999 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003000 }
3001
Christopher Fauleta8b76842022-12-16 15:08:36 +01003002 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003003 "<form method=\"post\">");
3004 }
3005
3006 /* print a new table */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003007 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003008 "<table class=\"tbl\" width=\"100%%\">\n"
3009 "<tr class=\"titre\">"
3010 "<th class=\"pxname\" width=\"10%%\">");
3011
Christopher Fauleta8b76842022-12-16 15:08:36 +01003012 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003013 "<a name=\"%s\"></a>%s"
3014 "<a class=px href=\"#%s\">%s</a>",
3015 px->id,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003016 (ctx->flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01003017 px->id, px->id);
3018
Willy Tarreau91cefca2022-05-03 17:08:29 +02003019 if (ctx->flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003020 /* cap, mode, id */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003021 chunk_appendf(&trash_chunk, "<div class=tips>cap: %s, mode: %s, id: %d",
William Lallemand74c24fb2016-11-21 17:18:36 +01003022 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3023 px->uuid);
Christopher Fauleta8b76842022-12-16 15:08:36 +01003024 chunk_appendf(&trash_chunk, "</div>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003025 }
3026
Christopher Fauleta8b76842022-12-16 15:08:36 +01003027 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003028 "%s</th>"
3029 "<th class=\"%s\" width=\"90%%\">%s</th>"
3030 "</tr>\n"
3031 "</table>\n"
3032 "<table class=\"tbl\" width=\"100%%\">\n"
3033 "<tr class=\"titre\">",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003034 (ctx->flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01003035 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3036
Willy Tarreau91cefca2022-05-03 17:08:29 +02003037 if (ctx->flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003038 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01003039 if ((px->cap & PR_CAP_BE) && px->srv)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003040 chunk_appendf(&trash_chunk,
Christopher Fauletbc271ec2019-12-02 11:29:04 +01003041 "<th rowspan=2 width=1><input type=\"checkbox\" "
3042 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
3043 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
3044 px->id,
3045 px->id);
3046 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003047 chunk_appendf(&trash_chunk, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003048 }
3049
Christopher Fauleta8b76842022-12-16 15:08:36 +01003050 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003051 "<th rowspan=2></th>"
3052 "<th colspan=3>Queue</th>"
3053 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
3054 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3055 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003056 "<th colspan=9>Server</th>");
3057
Willy Tarreau91cefca2022-05-03 17:08:29 +02003058 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003059 // calculate the count of module for colspan attribute
3060 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
3061 ++stats_module_len;
3062 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003063 chunk_appendf(&trash_chunk, "<th colspan=%d>Extra modules</th>",
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003064 stats_module_len);
3065 }
3066
Christopher Fauleta8b76842022-12-16 15:08:36 +01003067 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003068 "</tr>\n"
3069 "<tr class=\"titre\">"
3070 "<th>Cur</th><th>Max</th><th>Limit</th>"
3071 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
3072 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
3073 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3074 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3075 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3076 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003077 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003078
Willy Tarreau91cefca2022-05-03 17:08:29 +02003079 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003080 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Christopher Fauleta8b76842022-12-16 15:08:36 +01003081 chunk_appendf(&trash_chunk, "<th>%s</th>", mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003082 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003083 }
3084
Christopher Fauleta8b76842022-12-16 15:08:36 +01003085 chunk_appendf(&trash_chunk, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003086}
3087
Christopher Fauleta8b76842022-12-16 15:08:36 +01003088/* Dumps the HTML table trailer for proxy <px> to the local trash buffer for and
3089 * uses the state from stream connector <sc>. The caller is responsible for
3090 * clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003091 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003092static void stats_dump_html_px_end(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01003093{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003094 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003095 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet693b23b2022-02-28 09:09:05 +01003096
Christopher Fauleta8b76842022-12-16 15:08:36 +01003097 chunk_appendf(&trash_chunk, "</table>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003098
Willy Tarreau91cefca2022-05-03 17:08:29 +02003099 if ((px->cap & PR_CAP_BE) && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003100 /* close the form used to enable/disable this proxy servers */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003101 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003102 "Choose the action to perform on the checked servers : "
3103 "<select name=action>"
3104 "<option value=\"\"></option>"
3105 "<option value=\"ready\">Set state to READY</option>"
3106 "<option value=\"drain\">Set state to DRAIN</option>"
3107 "<option value=\"maint\">Set state to MAINT</option>"
3108 "<option value=\"dhlth\">Health: disable checks</option>"
3109 "<option value=\"ehlth\">Health: enable checks</option>"
3110 "<option value=\"hrunn\">Health: force UP</option>"
3111 "<option value=\"hnolb\">Health: force NOLB</option>"
3112 "<option value=\"hdown\">Health: force DOWN</option>"
3113 "<option value=\"dagent\">Agent: disable checks</option>"
3114 "<option value=\"eagent\">Agent: enable checks</option>"
3115 "<option value=\"arunn\">Agent: force UP</option>"
3116 "<option value=\"adown\">Agent: force DOWN</option>"
3117 "<option value=\"shutdown\">Kill Sessions</option>"
3118 "</select>"
3119 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3120 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3121 "</form>",
3122 px->uuid);
3123 }
3124
Christopher Fauleta8b76842022-12-16 15:08:36 +01003125 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003126}
3127
3128/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003129 * Dumps statistics for a proxy. The output is sent to the stream connector's
William Lallemand74c24fb2016-11-21 17:18:36 +01003130 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3131 * buffer space, or non-zero if everything completed. This function is used
3132 * both by the CLI and the HTTP entry points, and is able to dump the output
3133 * in HTML or CSV formats. If the later, <uri> must be NULL.
3134 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003135int stats_dump_proxy_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003136 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003137{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003138 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003139 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003140 struct stream *s = __sc_strm(sc);
3141 struct channel *rep = sc_ic(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +01003142 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3143 struct listener *l;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003144 int current_field;
William Lallemand74c24fb2016-11-21 17:18:36 +01003145
Christopher Fauleta8b76842022-12-16 15:08:36 +01003146 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003147more:
3148 current_field = ctx->field;
William Lallemand74c24fb2016-11-21 17:18:36 +01003149
Willy Tarreau91cefca2022-05-03 17:08:29 +02003150 switch (ctx->px_st) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003151 case STAT_PX_ST_INIT:
3152 /* we are on a new proxy */
3153 if (uri && uri->scope) {
3154 /* we have a limited scope, we have to check the proxy name */
3155 struct stat_scope *scope;
3156 int len;
3157
3158 len = strlen(px->id);
3159 scope = uri->scope;
3160
3161 while (scope) {
3162 /* match exact proxy name */
3163 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3164 break;
3165
3166 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003167 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003168 break;
3169 scope = scope->next;
3170 }
3171
3172 /* proxy name not found : don't dump anything */
3173 if (scope == NULL)
3174 return 1;
3175 }
3176
3177 /* if the user has requested a limited output and the proxy
3178 * name does not match, skip it.
3179 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003180 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003181 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003182
Willy Tarreau91cefca2022-05-03 17:08:29 +02003183 if (strnistr(px->id, strlen(px->id), scope_ptr, ctx->scope_len) == NULL)
Christopher Fauleted7a0662019-01-14 11:07:34 +01003184 return 1;
3185 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003186
Willy Tarreau91cefca2022-05-03 17:08:29 +02003187 if ((ctx->flags & STAT_BOUND) &&
3188 (ctx->iid != -1) &&
3189 (px->uuid != ctx->iid))
William Lallemand74c24fb2016-11-21 17:18:36 +01003190 return 1;
3191
Willy Tarreau91cefca2022-05-03 17:08:29 +02003192 ctx->px_st = STAT_PX_ST_TH;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003193 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003194
3195 case STAT_PX_ST_TH:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003196 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003197 stats_dump_html_px_hdr(sc, px);
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003198 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003199 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003200 }
3201
Willy Tarreau91cefca2022-05-03 17:08:29 +02003202 ctx->px_st = STAT_PX_ST_FE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003203 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003204
3205 case STAT_PX_ST_FE:
3206 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003207 if (stats_dump_fe_stats(sc, px)) {
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003208 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003209 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003210 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003211 if (ctx->field)
3212 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003213 }
3214
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003215 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003216 ctx->obj2 = px->conf.listeners.n;
3217 ctx->px_st = STAT_PX_ST_LI;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003218 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003219
3220 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003221 /* obj2 points to listeners list as initialized above */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003222 for (; ctx->obj2 != &px->conf.listeners; ctx->obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003223 if (htx) {
3224 if (htx_almost_full(htx))
3225 goto full;
3226 }
3227 else {
3228 if (buffer_almost_full(&rep->buf))
3229 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003230 }
3231
Willy Tarreau91cefca2022-05-03 17:08:29 +02003232 l = LIST_ELEM(ctx->obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003233 if (!l->counters)
3234 continue;
3235
Willy Tarreau91cefca2022-05-03 17:08:29 +02003236 if (ctx->flags & STAT_BOUND) {
3237 if (!(ctx->type & (1 << STATS_TYPE_SO)))
William Lallemand74c24fb2016-11-21 17:18:36 +01003238 break;
3239
Willy Tarreau91cefca2022-05-03 17:08:29 +02003240 if (ctx->sid != -1 && l->luid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003241 continue;
3242 }
3243
3244 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003245 if (stats_dump_li_stats(sc, px, l)) {
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003246 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003247 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003248 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003249 if (ctx->field)
3250 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003251 }
Aurelien DARRAGON9b07d4f2023-02-03 11:43:05 +01003252 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003253 }
3254
Willy Tarreau91cefca2022-05-03 17:08:29 +02003255 ctx->obj2 = px->srv; /* may be NULL */
3256 ctx->px_st = STAT_PX_ST_SV;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003257 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003258
3259 case STAT_PX_ST_SV:
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003260 /* obj2 points to servers list as initialized above.
3261 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003262 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003263 * Temporarily increment its refcount to prevent its
3264 * anticipated cleaning. Call free_server to release it.
3265 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003266 for (; ctx->obj2 != NULL;
3267 ctx->obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003268
Willy Tarreau91cefca2022-05-03 17:08:29 +02003269 sv = ctx->obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003270 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003271
Christopher Fauletef779222018-10-31 08:47:01 +01003272 if (htx) {
3273 if (htx_almost_full(htx))
3274 goto full;
3275 }
3276 else {
3277 if (buffer_almost_full(&rep->buf))
3278 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003279 }
3280
Willy Tarreau91cefca2022-05-03 17:08:29 +02003281 if (ctx->flags & STAT_BOUND) {
3282 if (!(ctx->type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003283 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003284 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003285 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003286
Willy Tarreau91cefca2022-05-03 17:08:29 +02003287 if (ctx->sid != -1 && sv->puid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003288 continue;
3289 }
3290
Willy Tarreau3e320362020-10-23 17:28:57 +02003291 /* do not report disabled servers */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003292 if (ctx->flags & STAT_HIDE_MAINT &&
Willy Tarreau3e320362020-10-23 17:28:57 +02003293 sv->cur_admin & SRV_ADMF_MAINT) {
3294 continue;
3295 }
3296
William Lallemand74c24fb2016-11-21 17:18:36 +01003297 svs = sv;
3298 while (svs->track)
3299 svs = svs->track;
3300
3301 /* do not report servers which are DOWN and not changing state */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003302 if ((ctx->flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003303 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3304 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003305 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3306 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3307 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3308 continue;
3309 }
3310
Willy Tarreaucaff6312022-05-27 10:17:46 +02003311 if (stats_dump_sv_stats(sc, px, sv)) {
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003312 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003313 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003314 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003315 if (ctx->field)
3316 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003317 }
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003318 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003319 } /* for sv */
3320
Willy Tarreau91cefca2022-05-03 17:08:29 +02003321 ctx->px_st = STAT_PX_ST_BE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003322 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003323
3324 case STAT_PX_ST_BE:
3325 /* print the backend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003326 if (stats_dump_be_stats(sc, px)) {
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003327 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003328 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003329 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003330 if (ctx->field)
3331 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003332 }
3333
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003334 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003335 ctx->px_st = STAT_PX_ST_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003336 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003337
3338 case STAT_PX_ST_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003339 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003340 stats_dump_html_px_end(sc, px);
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003341 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003342 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003343 }
3344
Willy Tarreau91cefca2022-05-03 17:08:29 +02003345 ctx->px_st = STAT_PX_ST_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003346 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003347
3348 case STAT_PX_ST_FIN:
3349 return 1;
3350
3351 default:
3352 /* unknown state, we should put an abort() here ! */
3353 return 1;
3354 }
Christopher Fauletef779222018-10-31 08:47:01 +01003355
3356 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003357 sc_need_room(sc);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003358 /* restore previous field */
3359 ctx->field = current_field;
Christopher Fauletef779222018-10-31 08:47:01 +01003360 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003361}
3362
Christopher Fauleta8b76842022-12-16 15:08:36 +01003363/* Dumps the HTTP stats head block to the local trash buffer for and uses the
3364 * per-uri parameters <uri>. The caller is responsible for clearing the local
3365 * trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003366 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003367static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003368{
Willy Tarreau91cefca2022-05-03 17:08:29 +02003369 struct show_stat_ctx *ctx = appctx->svcctx;
3370
William Lallemand74c24fb2016-11-21 17:18:36 +01003371 /* WARNING! This must fit in the first buffer !!! */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003372 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003373 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3374 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3375 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003376 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003377 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3378 "<style type=\"text/css\"><!--\n"
3379 "body {"
3380 " font-family: arial, helvetica, sans-serif;"
3381 " font-size: 12px;"
3382 " font-weight: normal;"
3383 " color: black;"
3384 " background: white;"
3385 "}\n"
3386 "th,td {"
3387 " font-size: 10px;"
3388 "}\n"
3389 "h1 {"
3390 " font-size: x-large;"
3391 " margin-bottom: 0.5em;"
3392 "}\n"
3393 "h2 {"
3394 " font-family: helvetica, arial;"
3395 " font-size: x-large;"
3396 " font-weight: bold;"
3397 " font-style: italic;"
3398 " color: #6020a0;"
3399 " margin-top: 0em;"
3400 " margin-bottom: 0em;"
3401 "}\n"
3402 "h3 {"
3403 " font-family: helvetica, arial;"
3404 " font-size: 16px;"
3405 " font-weight: bold;"
3406 " color: #b00040;"
3407 " background: #e8e8d0;"
3408 " margin-top: 0em;"
3409 " margin-bottom: 0em;"
3410 "}\n"
3411 "li {"
3412 " margin-top: 0.25em;"
3413 " margin-right: 2em;"
3414 "}\n"
3415 ".hr {margin-top: 0.25em;"
3416 " border-color: black;"
3417 " border-bottom-style: solid;"
3418 "}\n"
3419 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3420 ".total {background: #20D0D0;color: #ffff80;}\n"
3421 ".frontend {background: #e8e8d0;}\n"
3422 ".socket {background: #d0d0d0;}\n"
3423 ".backend {background: #e8e8d0;}\n"
3424 ".active_down {background: #ff9090;}\n"
3425 ".active_going_up {background: #ffd020;}\n"
3426 ".active_going_down {background: #ffffa0;}\n"
3427 ".active_up {background: #c0ffc0;}\n"
3428 ".active_nolb {background: #20a0ff;}\n"
3429 ".active_draining {background: #20a0FF;}\n"
3430 ".active_no_check {background: #e0e0e0;}\n"
3431 ".backup_down {background: #ff9090;}\n"
3432 ".backup_going_up {background: #ff80ff;}\n"
3433 ".backup_going_down {background: #c060ff;}\n"
3434 ".backup_up {background: #b0d0ff;}\n"
3435 ".backup_nolb {background: #90b0e0;}\n"
3436 ".backup_draining {background: #cc9900;}\n"
3437 ".backup_no_check {background: #e0e0e0;}\n"
3438 ".maintain {background: #c07820;}\n"
3439 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3440 "\n"
3441 "a.px:link {color: #ffff40; text-decoration: none;}"
3442 "a.px:visited {color: #ffff40; text-decoration: none;}"
3443 "a.px:hover {color: #ffffff; text-decoration: none;}"
3444 "a.lfsb:link {color: #000000; text-decoration: none;}"
3445 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3446 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3447 "\n"
3448 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3449 "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"
3450 "table.tbl td.ac { text-align: center;}\n"
3451 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3452 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3453 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3454 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3455 "\n"
3456 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3457 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3458 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3459 "table.det { border-collapse: collapse; border-style: none; }\n"
3460 "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"
3461 "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"
3462 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003463 "div.tips {\n"
3464 " display:block;\n"
3465 " visibility:hidden;\n"
3466 " z-index:2147483647;\n"
3467 " position:absolute;\n"
3468 " padding:2px 4px 3px;\n"
3469 " background:#f0f060; color:#000000;\n"
3470 " border:1px solid #7040c0;\n"
3471 " white-space:nowrap;\n"
3472 " font-style:normal;font-size:11px;font-weight:normal;\n"
3473 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3474 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3475 "}\n"
3476 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003477 "@media (prefers-color-scheme: dark) {\n"
3478 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3479 " h1 { color: #a265e0!important; }\n"
3480 " h2 { color: #a265e0; }\n"
3481 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3482 " a { color: #3391ff; }\n"
3483 " input { background-color: #2f3437; }\n"
3484 " .hr { border-color: #8c8273; }\n"
3485 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3486 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003487 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003488 " .backend {background: #2f3437;}\n"
3489 " .active_down {background: #760000;}\n"
3490 " .active_going_up {background: #b99200;}\n"
3491 " .active_going_down {background: #6c6c00;}\n"
3492 " .active_up {background: #165900;}\n"
3493 " .active_nolb {background: #006ab9;}\n"
3494 " .active_draining {background: #006ab9;}\n"
3495 " .active_no_check {background: #2a2d2f;}\n"
3496 " .backup_down {background: #760000;}\n"
3497 " .backup_going_up {background: #7f007f;}\n"
3498 " .backup_going_down {background: #580092;}\n"
3499 " .backup_up {background: #2e3234;}\n"
3500 " .backup_nolb {background: #1e3c6a;}\n"
3501 " .backup_draining {background: #a37a00;}\n"
3502 " .backup_no_check {background: #2a2d2f;}\n"
3503 " .maintain {background: #9a601a;}\n"
3504 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3505 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3506 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3507 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3508 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3509 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3510 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003511 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003512 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3513 " table.tbl th { border-color: #808080; }\n"
3514 " table.tbl td { border-color: #808080; }\n"
3515 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3516 " div.tips {\n"
3517 " background:#8e8e0d;\n"
3518 " color:#e8e6e3;\n"
3519 " border-color: #4e2c86;\n"
3520 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3521 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3522 " box-shadow: #60686c 2px 2px 3px;\n"
3523 " }\n"
3524 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003525 "-->\n"
3526 "</style></head>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003527 (ctx->flags & STAT_SHNODE) ? " on " : "",
3528 (ctx->flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003529 );
3530}
3531
Christopher Fauleta8b76842022-12-16 15:08:36 +01003532/* Dumps the HTML stats information block to the local trash buffer for and uses
3533 * the state from stream connector <sc> and per-uri parameters <uri>. The caller
3534 * is responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003535 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003536static void stats_dump_html_info(struct stconn *sc, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003537{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003538 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003539 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaub685ad02023-02-10 16:53:35 +01003540 unsigned int up = (now.tv_sec - start_time.tv_sec);
William Lallemand74c24fb2016-11-21 17:18:36 +01003541 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02003542 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Willy Tarreau6be8d092023-01-12 16:08:41 +01003543 unsigned long long bps;
3544 int thr;
3545
3546 for (bps = thr = 0; thr < global.nbthread; thr++)
3547 bps += 32ULL * read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
Willy Tarreau1713c032019-05-23 12:23:55 +02003548
3549 /* Turn the bytes per second to bits per second and take care of the
3550 * usual ethernet overhead in order to help figure how far we are from
3551 * interface saturation since it's the only case which usually matters.
3552 * For this we count the total size of an Ethernet frame on the wire
3553 * including preamble and IFG (1538) for the largest TCP segment it
3554 * transports (1448 with TCP timestamps). This is not valid for smaller
3555 * packets (under-estimated), but it gives a reasonably accurate
3556 * estimation of how far we are from uplink saturation.
3557 */
3558 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003559
3560 /* WARNING! this has to fit the first packet too.
3561 * We are around 3.5 kB, add adding entries will
3562 * become tricky if we want to support 4kB buffers !
3563 */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003564 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003565 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3566 PRODUCT_NAME "%s</a></h1>\n"
3567 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3568 "<hr width=\"100%%\" class=\"hr\">\n"
3569 "<h3>&gt; General process information</h3>\n"
3570 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003571 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003572 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3573 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3574 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003575 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003576 "Running tasks: %d/%d; idle = %d %%<br>\n"
3577 "</td><td align=\"center\" nowrap>\n"
3578 "<table class=\"lgd\"><tr>\n"
3579 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3580 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3581 "</tr><tr>\n"
3582 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3583 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3584 "</tr><tr>\n"
3585 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3586 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3587 "</tr><tr>\n"
3588 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3589 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3590 "</tr><tr>\n"
3591 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3592 "</tr><tr>\n"
3593 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3594 "</tr></table>\n"
3595 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3596 "</td>"
3597 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3598 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3599 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003600 (ctx->flags & STAT_HIDEVER) ? "" : (stats_version_string),
3601 pid, (ctx->flags & STAT_SHNODE) ? " on " : "",
3602 (ctx->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3603 (ctx->flags & STAT_SHDESC) ? ": " : "",
3604 (ctx->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003605 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003606 up / 86400, (up % 86400) / 3600,
3607 (up % 3600) / 60, (up % 60),
3608 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3609 global.rlimit_memmax ? " MB" : "",
3610 global.rlimit_nofile,
3611 global.maxsock, global.maxconn, global.maxpipes,
3612 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003613 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3614 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003615 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003616 );
3617
Willy Tarreau91cefca2022-05-03 17:08:29 +02003618 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3619 memcpy(scope_txt, scope_ptr, ctx->scope_len);
3620 scope_txt[ctx->scope_len] = '\0';
William Lallemand74c24fb2016-11-21 17:18:36 +01003621
Christopher Fauleta8b76842022-12-16 15:08:36 +01003622 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003623 "<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 +02003624 (ctx->scope_len > 0) ? scope_txt : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003625 STAT_SCOPE_TXT_MAXLEN);
3626
Willy Tarreau91cefca2022-05-03 17:08:29 +02003627 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003628 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003629 if (ctx->scope_len) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003630 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003631 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3632 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003633 }
3634
Willy Tarreau91cefca2022-05-03 17:08:29 +02003635 if (ctx->flags & STAT_HIDE_DOWN)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003636 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003637 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3638 uri->uri_prefix,
3639 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003640 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003641 scope_txt);
3642 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003643 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003644 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3645 uri->uri_prefix,
3646 ";up",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003647 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003648 scope_txt);
3649
3650 if (uri->refresh > 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003651 if (ctx->flags & STAT_NO_REFRESH)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003652 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003653 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3654 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003655 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003656 "",
3657 scope_txt);
3658 else
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\">Disable refresh</a><br>\n",
3661 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003662 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003663 ";norefresh",
3664 scope_txt);
3665 }
3666
Christopher Fauleta8b76842022-12-16 15:08:36 +01003667 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003668 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3669 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003670 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3671 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003672 scope_txt);
3673
Christopher Fauleta8b76842022-12-16 15:08:36 +01003674 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003675 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3676 uri->uri_prefix,
3677 (uri->refresh > 0) ? ";norefresh" : "",
3678 scope_txt);
3679
Christopher Fauleta8b76842022-12-16 15:08:36 +01003680 chunk_appendf(&trash_chunk,
Christopher Faulet6338a082019-09-09 15:50:54 +02003681 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3682 uri->uri_prefix,
3683 (uri->refresh > 0) ? ";norefresh" : "",
3684 scope_txt, uri->uri_prefix);
3685
Christopher Fauleta8b76842022-12-16 15:08:36 +01003686 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003687 "</ul></td>"
3688 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3689 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3690 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3691 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3692 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3693 "</ul>"
3694 "</td>"
3695 "</tr></table>\n"
3696 ""
3697 );
3698
Willy Tarreau91cefca2022-05-03 17:08:29 +02003699 if (ctx->st_code) {
3700 switch (ctx->st_code) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003701 case STAT_STATUS_DONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003702 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003703 "<p><div class=active_up>"
3704 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3705 "Action processed successfully."
3706 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003707 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3708 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003709 scope_txt);
3710 break;
3711 case STAT_STATUS_NONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003712 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003713 "<p><div class=active_going_down>"
3714 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3715 "Nothing has changed."
3716 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003717 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3718 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003719 scope_txt);
3720 break;
3721 case STAT_STATUS_PART:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003722 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003723 "<p><div class=active_going_down>"
3724 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3725 "Action partially processed.<br>"
3726 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3727 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003728 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3729 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003730 scope_txt);
3731 break;
3732 case STAT_STATUS_ERRP:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003733 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003734 "<p><div class=active_down>"
3735 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3736 "Action not processed because of invalid parameters."
3737 "<ul>"
3738 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003739 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003740 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3741 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3742 "</ul>"
3743 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003744 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3745 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003746 scope_txt);
3747 break;
3748 case STAT_STATUS_EXCD:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003749 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003750 "<p><div class=active_down>"
3751 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3752 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3753 "You should retry with less servers at a time.</b>"
3754 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003755 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3756 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003757 scope_txt);
3758 break;
3759 case STAT_STATUS_DENY:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003760 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003761 "<p><div class=active_down>"
3762 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3763 "<b>Action denied.</b>"
3764 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003765 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3766 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003767 scope_txt);
3768 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003769 case STAT_STATUS_IVAL:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003770 chunk_appendf(&trash_chunk,
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003771 "<p><div class=active_down>"
3772 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3773 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3774 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003775 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3776 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003777 scope_txt);
3778 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003779 default:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003780 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003781 "<p><div class=active_no_check>"
3782 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3783 "Unexpected result."
3784 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003785 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3786 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003787 scope_txt);
3788 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003789 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003790 }
3791}
3792
Christopher Fauleta8b76842022-12-16 15:08:36 +01003793/* Dumps the HTML stats trailer block to the local trash buffer. The caller is
3794 * responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003795 */
3796static void stats_dump_html_end()
3797{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003798 chunk_appendf(&trash_chunk, "</body></html>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003799}
3800
Christopher Fauleta8b76842022-12-16 15:08:36 +01003801/* Dumps the stats JSON header to the local trash buffer buffer which. The
3802 * caller is responsible for clearing it if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003803 */
3804static void stats_dump_json_header()
3805{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003806 chunk_strcat(&trash_chunk, "[");
Simon Horman05ee2132017-01-04 09:37:25 +01003807}
3808
3809
Christopher Fauleta8b76842022-12-16 15:08:36 +01003810/* Dumps the JSON stats trailer block to the local trash buffer. The caller is
3811 * responsible for clearing the local trash buffer if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003812 */
3813static void stats_dump_json_end()
3814{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003815 chunk_strcat(&trash_chunk, "]\n");
Simon Horman05ee2132017-01-04 09:37:25 +01003816}
3817
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003818/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3819 * a pointer to the current server/listener.
3820 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003821static int stats_dump_proxies(struct stconn *sc,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003822 struct htx *htx,
3823 struct uri_auth *uri)
3824{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003825 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003826 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003827 struct channel *rep = sc_ic(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003828 struct proxy *px;
3829
3830 /* dump proxies */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003831 while (ctx->obj1) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003832 if (htx) {
3833 if (htx_almost_full(htx))
3834 goto full;
3835 }
3836 else {
3837 if (buffer_almost_full(&rep->buf))
3838 goto full;
3839 }
3840
Willy Tarreau91cefca2022-05-03 17:08:29 +02003841 px = ctx->obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003842 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003843 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003844 * This change allows retrieving stats from "old" proxies after a reload.
3845 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003846 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003847 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003848 if (stats_dump_proxy_to_buffer(sc, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003849 return 0;
3850 }
3851
Willy Tarreau91cefca2022-05-03 17:08:29 +02003852 ctx->obj1 = px->next;
3853 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003854 ctx->field = 0;
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003855 }
3856
3857 return 1;
3858
3859 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003860 sc_need_room(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003861 return 0;
3862}
3863
Willy Tarreau4596fe22022-05-17 19:07:51 +02003864/* This function dumps statistics onto the stream connector's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003865 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3866 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3867 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3868 * and the stream must be closed, or -1 in case of any error. This function is
3869 * used by both the CLI and the HTTP handlers.
3870 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003871static int stats_dump_stat_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003872 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003873{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003874 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003875 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003876 struct channel *rep = sc_ic(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003877 enum stats_domain domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003878
Christopher Fauleta8b76842022-12-16 15:08:36 +01003879 chunk_reset(&trash_chunk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003880
Willy Tarreau41f88522022-05-03 18:39:27 +02003881 switch (ctx->state) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003882 case STAT_STATE_INIT:
3883 ctx->state = STAT_STATE_HEAD; /* let's start producing data */
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003884 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003885
Willy Tarreau6ef16482022-05-06 18:07:53 +02003886 case STAT_STATE_HEAD:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003887 if (ctx->flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003888 stats_dump_html_head(appctx, uri);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003889 else if (ctx->flags & STAT_JSON_SCHM)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003890 stats_dump_json_schema(&trash_chunk);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003891 else if (ctx->flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003892 stats_dump_json_header();
Willy Tarreau91cefca2022-05-03 17:08:29 +02003893 else if (!(ctx->flags & STAT_FMT_TYPED))
3894 stats_dump_csv_header(ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003895
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003896 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003897 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003898
Willy Tarreau91cefca2022-05-03 17:08:29 +02003899 if (ctx->flags & STAT_JSON_SCHM) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003900 ctx->state = STAT_STATE_FIN;
Christopher Faulet6338a082019-09-09 15:50:54 +02003901 return 1;
3902 }
Willy Tarreau6ef16482022-05-06 18:07:53 +02003903 ctx->state = STAT_STATE_INFO;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003904 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003905
Willy Tarreau6ef16482022-05-06 18:07:53 +02003906 case STAT_STATE_INFO:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003907 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003908 stats_dump_html_info(sc, uri);
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003909 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003910 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003911 }
3912
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003913 if (domain == STATS_DOMAIN_PROXY)
Willy Tarreau91cefca2022-05-03 17:08:29 +02003914 ctx->obj1 = proxies_list;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003915
Willy Tarreau91cefca2022-05-03 17:08:29 +02003916 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003917 ctx->field = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003918 ctx->state = STAT_STATE_LIST;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003919 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003920
Willy Tarreau6ef16482022-05-06 18:07:53 +02003921 case STAT_STATE_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003922 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003923 case STATS_DOMAIN_RESOLVERS:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003924 if (!stats_dump_resolvers(sc, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003925 stat_count[domain],
3926 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003927 return 0;
3928 }
3929 break;
3930
3931 case STATS_DOMAIN_PROXY:
3932 default:
3933 /* dump proxies */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003934 if (!stats_dump_proxies(sc, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003935 return 0;
3936 break;
3937 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003938
Willy Tarreau6ef16482022-05-06 18:07:53 +02003939 ctx->state = STAT_STATE_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003940 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003941
Willy Tarreau6ef16482022-05-06 18:07:53 +02003942 case STAT_STATE_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003943 if (ctx->flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3944 if (ctx->flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01003945 stats_dump_html_end();
3946 else
3947 stats_dump_json_end();
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003948 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003949 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003950 }
3951
Willy Tarreau6ef16482022-05-06 18:07:53 +02003952 ctx->state = STAT_STATE_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003953 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003954
Willy Tarreau6ef16482022-05-06 18:07:53 +02003955 case STAT_STATE_FIN:
William Lallemand74c24fb2016-11-21 17:18:36 +01003956 return 1;
3957
3958 default:
3959 /* unknown state ! */
Willy Tarreau6ef16482022-05-06 18:07:53 +02003960 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01003961 return -1;
3962 }
Christopher Fauletef779222018-10-31 08:47:01 +01003963
3964 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003965 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01003966 return 0;
3967
William Lallemand74c24fb2016-11-21 17:18:36 +01003968}
3969
3970/* We reached the stats page through a POST request. The appctx is
3971 * expected to have already been allocated by the caller.
3972 * Parse the posted data and enable/disable servers if necessary.
3973 * Returns 1 if request was parsed or zero if it needs more data.
3974 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003975static int stats_process_http_post(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01003976{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003977 struct stream *s = __sc_strm(sc);
3978 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003979 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003980
3981 struct proxy *px = NULL;
3982 struct server *sv = NULL;
3983
3984 char key[LINESIZE];
3985 int action = ST_ADM_ACTION_NONE;
3986 int reprocess = 0;
3987
3988 int total_servers = 0;
3989 int altered_servers = 0;
3990
3991 char *first_param, *cur_param, *next_param, *end_params;
3992 char *st_cur_param = NULL;
3993 char *st_next_param = NULL;
3994
Christopher Fauleta3618372018-12-13 21:59:56 +01003995 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003996
Christopher Fauletb7f88902019-07-15 21:56:43 +02003997 struct htx *htx = htxbuf(&s->req.buf);
3998 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003999
Christopher Fauletb7f88902019-07-15 21:56:43 +02004000 /* we need more data */
4001 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
4002 /* check if we can receive more */
4003 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004004 ctx->st_code = STAT_STATUS_EXCD;
Christopher Fauletb7f88902019-07-15 21:56:43 +02004005 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01004006 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004007 goto wait;
4008 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004009
Christopher Fauletb7f88902019-07-15 21:56:43 +02004010 /* The request was fully received. Copy data */
4011 blk = htx_get_head_blk(htx);
4012 while (blk) {
4013 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004014
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004015 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02004016 break;
4017 if (type == HTX_BLK_DATA) {
4018 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004019
Christopher Fauletb7f88902019-07-15 21:56:43 +02004020 if (!chunk_memcat(temp, v.ptr, v.len)) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004021 ctx->st_code = STAT_STATUS_EXCD;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004022 goto out;
4023 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004024 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004025 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01004026 }
4027
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004028 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01004029 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01004030 cur_param = next_param = end_params;
4031 *end_params = '\0';
4032
Willy Tarreau91cefca2022-05-03 17:08:29 +02004033 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004034
4035 /*
4036 * Parse the parameters in reverse order to only store the last value.
4037 * From the html form, the backend and the action are at the end.
4038 */
4039 while (cur_param > first_param) {
4040 char *value;
4041 int poffset, plen;
4042
4043 cur_param--;
4044
4045 if ((*cur_param == '&') || (cur_param == first_param)) {
4046 reprocess_servers:
4047 /* Parse the key */
4048 poffset = (cur_param != first_param ? 1 : 0);
4049 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4050 if ((plen > 0) && (plen <= sizeof(key))) {
4051 strncpy(key, cur_param + poffset, plen);
4052 key[plen - 1] = '\0';
4053 } else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004054 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004055 goto out;
4056 }
4057
4058 /* Parse the value */
4059 value = key;
4060 while (*value != '\0' && *value != '=') {
4061 value++;
4062 }
4063 if (*value == '=') {
4064 /* Ok, a value is found, we can mark the end of the key */
4065 *value++ = '\0';
4066 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02004067 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01004068 break;
4069
4070 /* Now we can check the key to see what to do */
4071 if (!px && (strcmp(key, "b") == 0)) {
4072 if ((px = proxy_be_by_name(value)) == NULL) {
4073 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004074 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004075 goto out;
4076 }
4077 }
4078 else if (!action && (strcmp(key, "action") == 0)) {
4079 if (strcmp(value, "ready") == 0) {
4080 action = ST_ADM_ACTION_READY;
4081 }
4082 else if (strcmp(value, "drain") == 0) {
4083 action = ST_ADM_ACTION_DRAIN;
4084 }
4085 else if (strcmp(value, "maint") == 0) {
4086 action = ST_ADM_ACTION_MAINT;
4087 }
4088 else if (strcmp(value, "shutdown") == 0) {
4089 action = ST_ADM_ACTION_SHUTDOWN;
4090 }
4091 else if (strcmp(value, "dhlth") == 0) {
4092 action = ST_ADM_ACTION_DHLTH;
4093 }
4094 else if (strcmp(value, "ehlth") == 0) {
4095 action = ST_ADM_ACTION_EHLTH;
4096 }
4097 else if (strcmp(value, "hrunn") == 0) {
4098 action = ST_ADM_ACTION_HRUNN;
4099 }
4100 else if (strcmp(value, "hnolb") == 0) {
4101 action = ST_ADM_ACTION_HNOLB;
4102 }
4103 else if (strcmp(value, "hdown") == 0) {
4104 action = ST_ADM_ACTION_HDOWN;
4105 }
4106 else if (strcmp(value, "dagent") == 0) {
4107 action = ST_ADM_ACTION_DAGENT;
4108 }
4109 else if (strcmp(value, "eagent") == 0) {
4110 action = ST_ADM_ACTION_EAGENT;
4111 }
4112 else if (strcmp(value, "arunn") == 0) {
4113 action = ST_ADM_ACTION_ARUNN;
4114 }
4115 else if (strcmp(value, "adown") == 0) {
4116 action = ST_ADM_ACTION_ADOWN;
4117 }
4118 /* else these are the old supported methods */
4119 else if (strcmp(value, "disable") == 0) {
4120 action = ST_ADM_ACTION_DISABLE;
4121 }
4122 else if (strcmp(value, "enable") == 0) {
4123 action = ST_ADM_ACTION_ENABLE;
4124 }
4125 else if (strcmp(value, "stop") == 0) {
4126 action = ST_ADM_ACTION_STOP;
4127 }
4128 else if (strcmp(value, "start") == 0) {
4129 action = ST_ADM_ACTION_START;
4130 }
4131 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004132 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004133 goto out;
4134 }
4135 }
4136 else if (strcmp(key, "s") == 0) {
4137 if (!(px && action)) {
4138 /*
4139 * Indicates that we'll need to reprocess the parameters
4140 * as soon as backend and action are known
4141 */
4142 if (!reprocess) {
4143 st_cur_param = cur_param;
4144 st_next_param = next_param;
4145 }
4146 reprocess = 1;
4147 }
4148 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004149 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004150 switch (action) {
4151 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004152 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004153 altered_servers++;
4154 total_servers++;
4155 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
4156 }
4157 break;
4158 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004159 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004160 altered_servers++;
4161 total_servers++;
4162 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4163 }
4164 break;
4165 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004166 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004167 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
4168 altered_servers++;
4169 total_servers++;
4170 }
4171 break;
4172 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004173 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004174 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4175 altered_servers++;
4176 total_servers++;
4177 }
4178 break;
4179 case ST_ADM_ACTION_DHLTH:
4180 if (sv->check.state & CHK_ST_CONFIGURED) {
4181 sv->check.state &= ~CHK_ST_ENABLED;
4182 altered_servers++;
4183 total_servers++;
4184 }
4185 break;
4186 case ST_ADM_ACTION_EHLTH:
4187 if (sv->check.state & CHK_ST_CONFIGURED) {
4188 sv->check.state |= CHK_ST_ENABLED;
4189 altered_servers++;
4190 total_servers++;
4191 }
4192 break;
4193 case ST_ADM_ACTION_HRUNN:
4194 if (!(sv->track)) {
4195 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004196 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004197 altered_servers++;
4198 total_servers++;
4199 }
4200 break;
4201 case ST_ADM_ACTION_HNOLB:
4202 if (!(sv->track)) {
4203 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004204 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004205 altered_servers++;
4206 total_servers++;
4207 }
4208 break;
4209 case ST_ADM_ACTION_HDOWN:
4210 if (!(sv->track)) {
4211 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004212 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004213 altered_servers++;
4214 total_servers++;
4215 }
4216 break;
4217 case ST_ADM_ACTION_DAGENT:
4218 if (sv->agent.state & CHK_ST_CONFIGURED) {
4219 sv->agent.state &= ~CHK_ST_ENABLED;
4220 altered_servers++;
4221 total_servers++;
4222 }
4223 break;
4224 case ST_ADM_ACTION_EAGENT:
4225 if (sv->agent.state & CHK_ST_CONFIGURED) {
4226 sv->agent.state |= CHK_ST_ENABLED;
4227 altered_servers++;
4228 total_servers++;
4229 }
4230 break;
4231 case ST_ADM_ACTION_ARUNN:
4232 if (sv->agent.state & CHK_ST_ENABLED) {
4233 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004234 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004235 altered_servers++;
4236 total_servers++;
4237 }
4238 break;
4239 case ST_ADM_ACTION_ADOWN:
4240 if (sv->agent.state & CHK_ST_ENABLED) {
4241 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004242 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004243 altered_servers++;
4244 total_servers++;
4245 }
4246 break;
4247 case ST_ADM_ACTION_READY:
4248 srv_adm_set_ready(sv);
4249 altered_servers++;
4250 total_servers++;
4251 break;
4252 case ST_ADM_ACTION_DRAIN:
4253 srv_adm_set_drain(sv);
4254 altered_servers++;
4255 total_servers++;
4256 break;
4257 case ST_ADM_ACTION_MAINT:
4258 srv_adm_set_maint(sv);
4259 altered_servers++;
4260 total_servers++;
4261 break;
4262 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004263 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004264 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004265 altered_servers++;
4266 total_servers++;
4267 }
4268 break;
4269 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004270 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004271 } else {
4272 /* the server name is unknown or ambiguous (duplicate names) */
4273 total_servers++;
4274 }
4275 }
4276 if (reprocess && px && action) {
4277 /* Now, we know the backend and the action chosen by the user.
4278 * We can safely restart from the first server parameter
4279 * to reprocess them
4280 */
4281 cur_param = st_cur_param;
4282 next_param = st_next_param;
4283 reprocess = 0;
4284 goto reprocess_servers;
4285 }
4286
4287 next_param = cur_param;
4288 }
4289 }
4290
4291 if (total_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004292 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004293 }
4294 else if (altered_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004295 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004296 }
4297 else if (altered_servers == total_servers) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004298 ctx->st_code = STAT_STATUS_DONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004299 }
4300 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004301 ctx->st_code = STAT_STATUS_PART;
William Lallemand74c24fb2016-11-21 17:18:36 +01004302 }
4303 out:
4304 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004305 wait:
Willy Tarreau91cefca2022-05-03 17:08:29 +02004306 ctx->st_code = STAT_STATUS_NONE;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004307 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004308}
4309
4310
Willy Tarreaucaff6312022-05-27 10:17:46 +02004311static int stats_send_http_headers(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004312{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004313 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004314 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004315 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004316 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004317 struct htx_sl *sl;
4318 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004319
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004320 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);
4321 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4322 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004323 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004324 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004325
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004326 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004327 goto full;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004328 if (ctx->flags & STAT_FMT_HTML) {
Christopher Fauletef779222018-10-31 08:47:01 +01004329 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4330 goto full;
4331 }
Willy Tarreau91cefca2022-05-03 17:08:29 +02004332 else if (ctx->flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004333 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4334 goto full;
4335 }
Christopher Fauletef779222018-10-31 08:47:01 +01004336 else {
4337 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4338 goto full;
4339 }
4340
Willy Tarreau91cefca2022-05-03 17:08:29 +02004341 if (uri->refresh > 0 && !(ctx->flags & STAT_NO_REFRESH)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004342 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004343 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004344 goto full;
4345 }
4346
Willy Tarreau91cefca2022-05-03 17:08:29 +02004347 if (ctx->flags & STAT_CHUNKED) {
Christopher Fauletef779222018-10-31 08:47:01 +01004348 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4349 goto full;
4350 }
4351
4352 if (!htx_add_endof(htx, HTX_BLK_EOH))
4353 goto full;
4354
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004355 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004356 return 1;
4357
4358 full:
4359 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004360 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004361 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004362}
4363
Christopher Fauletef779222018-10-31 08:47:01 +01004364
Willy Tarreaucaff6312022-05-27 10:17:46 +02004365static int stats_send_http_redirect(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004366{
4367 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02004368 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004369 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004370 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004371 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004372 struct htx_sl *sl;
4373 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004374
Willy Tarreau91cefca2022-05-03 17:08:29 +02004375 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauletef779222018-10-31 08:47:01 +01004376 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004377 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004378 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004379
Christopher Fauletef779222018-10-31 08:47:01 +01004380 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004381 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
4382 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004383 }
4384
4385 /* We don't want to land on the posted stats page because a refresh will
4386 * repost the data. We don't want this to happen on accident so we redirect
4387 * the browse to the stats page with a GET.
4388 */
4389 chunk_printf(&trash, "%s;st=%s%s%s%s",
4390 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02004391 ((ctx->st_code > STAT_STATUS_INIT) &&
4392 (ctx->st_code < STAT_STATUS_SIZE) &&
4393 stat_status_codes[ctx->st_code]) ?
4394 stat_status_codes[ctx->st_code] :
Christopher Fauletef779222018-10-31 08:47:01 +01004395 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau91cefca2022-05-03 17:08:29 +02004396 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
4397 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Fauletef779222018-10-31 08:47:01 +01004398 scope_txt);
4399
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004400 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4401 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4402 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004403 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004404 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004405
4406 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004407 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4408 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4409 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4410 goto full;
4411
4412 if (!htx_add_endof(htx, HTX_BLK_EOH))
4413 goto full;
4414
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004415 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004416 return 1;
4417
4418full:
4419 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004420 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004421 return 0;
4422}
William Lallemand74c24fb2016-11-21 17:18:36 +01004423
Simon Horman05ee2132017-01-04 09:37:25 +01004424
Willy Tarreau4596fe22022-05-17 19:07:51 +02004425/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004426 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4427 * appctx->st0 contains the operation in progress (dump, done). The handler
4428 * automatically unregisters itself once transfer is complete.
4429 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004430static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004431{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004432 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02004433 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004434 struct stream *s = __sc_strm(sc);
4435 struct channel *req = sc_oc(sc);
4436 struct channel *res = sc_ic(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004437 struct htx *req_htx, *res_htx;
4438
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004439 /* only proxy stats are available via http */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004440 ctx->domain = STATS_DOMAIN_PROXY;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004441
Christopher Fauletef779222018-10-31 08:47:01 +01004442 res_htx = htx_from_buf(&res->buf);
4443
Willy Tarreaucaff6312022-05-27 10:17:46 +02004444 if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO))
Christopher Fauletef779222018-10-31 08:47:01 +01004445 goto out;
4446
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004447 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004448 if (!b_size(&res->buf)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004449 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004450 goto out;
4451 }
4452
4453 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004454 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
4455 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004456
4457 /* all states are processed in sequence */
4458 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004459 if (stats_send_http_headers(sc, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004460 if (s->txn->meth == HTTP_METH_HEAD)
4461 appctx->st0 = STAT_HTTP_DONE;
4462 else
4463 appctx->st0 = STAT_HTTP_DUMP;
4464 }
4465 }
4466
4467 if (appctx->st0 == STAT_HTTP_DUMP) {
Aurelien DARRAGON14656842023-02-03 08:31:42 +01004468 trash_chunk = b_make(trash.area, res->buf.size, 0, 0);
Aurelien DARRAGON5e7ecbe2023-02-02 15:03:12 +01004469 /* adjust buffer size to take htx overhead into account,
4470 * make sure to perform this call on an empty buffer
4471 */
4472 trash_chunk.size = buf_room_for_htx_data(&trash_chunk);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004473 if (stats_dump_stat_to_buffer(sc, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004474 appctx->st0 = STAT_HTTP_DONE;
4475 }
4476
4477 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004478 if (stats_process_http_post(sc))
Christopher Fauletef779222018-10-31 08:47:01 +01004479 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004480 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004481 appctx->st0 = STAT_HTTP_DONE;
4482 }
4483
4484 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004485 if (stats_send_http_redirect(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004486 appctx->st0 = STAT_HTTP_DONE;
4487 }
4488
4489 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004490 /* no more data are expected. If the response buffer is empty,
4491 * be sure to add something (EOT block in this case) to have
4492 * something to send. It is important to be sure the EOM flags
4493 * will be handled by the endpoint.
4494 */
4495 if (htx_is_empty(res_htx)) {
4496 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004497 sc_need_room(sc);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004498 goto out;
4499 }
4500 channel_add_input(res, 1);
4501 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004502 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet3fa5d192022-03-07 15:52:42 +01004503 res->flags |= CF_EOI;
Willy Tarreaud869e132022-05-17 18:05:31 +02004504 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004505 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004506 }
4507
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004508 if (appctx->st0 == STAT_HTTP_END) {
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004509 se_fl_set(appctx->sedesc, SE_FL_EOS);
4510 applet_will_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004511 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004512
Christopher Fauletef779222018-10-31 08:47:01 +01004513 out:
4514 /* we have left the request in the buffer for the case where we
4515 * process a POST, and this automatically re-enables activity on
4516 * read. It's better to indicate that we want to stop reading when
4517 * we're sending, so that we know there's at most one direction
4518 * deciding to wake the applet up. It saves it from looping when
4519 * emitting large blocks into small TCP windows.
4520 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004521 htx_to_buf(res_htx, &res->buf);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004522 if (appctx->st0 == STAT_HTTP_END) {
4523 /* eat the whole request */
4524 if (co_data(req)) {
4525 req_htx = htx_from_buf(&req->buf);
4526 co_htx_skip(req, req_htx, co_data(req));
4527 htx_to_buf(req_htx, &req->buf);
4528 }
4529 }
4530 else if (!channel_is_empty(res))
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02004531 applet_wont_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004532}
4533
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004534/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004535static int stats_dump_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004536 const struct field *info,
4537 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004538{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004539 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004540 int field;
4541
4542 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4543 if (!field_format(info, field))
4544 continue;
4545
Willy Tarreaueaa55372019-10-09 07:39:11 +02004546 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004547 return 0;
4548 if (!stats_emit_raw_data_field(out, &info[field]))
4549 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004550 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4551 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004552 if (!chunk_strcat(out, "\n"))
4553 return 0;
4554 }
4555 return 1;
4556}
4557
4558/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004559static int stats_dump_typed_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004560 const struct field *info,
4561 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004562{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004563 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004564 int field;
4565
4566 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4567 if (!field_format(info, field))
4568 continue;
4569
Willy Tarreaueaa55372019-10-09 07:39:11 +02004570 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 +01004571 return 0;
4572 if (!stats_emit_field_tags(out, &info[field], ':'))
4573 return 0;
4574 if (!stats_emit_typed_data_field(out, &info[field]))
4575 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004576 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4577 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004578 if (!chunk_strcat(out, "\n"))
4579 return 0;
4580 }
4581 return 1;
4582}
4583
Willy Tarreau0b26b382021-05-08 07:43:53 +02004584/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4585 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4586 * less then this value, the function returns 0, otherwise, it returns 1. Some
4587 * fields' presence or precision may depend on some of the STAT_* flags present
4588 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004589 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004590int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004591{
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004592 struct timeval up;
Willy Tarreau83061a82018-07-13 11:56:34 +02004593 struct buffer *out = get_trash_chunk();
Willy Tarreau6be8d092023-01-12 16:08:41 +01004594 uint64_t glob_out_bytes, glob_spl_bytes, glob_out_b32;
4595 int thr;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004596
4597#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004598 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4599 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4600 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004601
Willy Tarreauc5977722021-05-08 08:14:04 +02004602 if (ssl_key_rate < ssl_sess_rate)
4603 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004604#endif
4605
Willy Tarreau6be8d092023-01-12 16:08:41 +01004606 /* sum certain per-thread totals (mostly byte counts) */
4607 glob_out_bytes = glob_spl_bytes = glob_out_b32 = 0;
4608 for (thr = 0; thr < global.nbthread; thr++) {
4609 glob_out_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].out_bytes);
4610 glob_spl_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].spliced_out_bytes);
4611 glob_out_b32 += read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
4612 }
4613 glob_out_b32 *= 32; // values are 32-byte units
4614
Willy Tarreau6093ba42023-02-07 15:52:14 +01004615 tv_remain(&start_time, &now, &up);
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004616
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004617 if (len < INF_TOTAL_FIELDS)
4618 return 0;
4619
4620 chunk_reset(out);
4621 memset(info, 0, sizeof(*info) * len);
4622
4623 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004624 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004625 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004626 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004627
Yves Lafon95317282018-02-26 11:10:37 +01004628 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004629 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004630 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004631 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4632
4633 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004634 chunk_appendf(out, "%ud %uh%02um%02us", (uint)up.tv_sec / 86400, ((uint)up.tv_sec % 86400) / 3600, ((uint)up.tv_sec % 3600) / 60, ((uint)up.tv_sec % 60));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004635
Willy Tarreaue8abc322021-05-08 07:56:56 +02004636 info[INF_UPTIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, up.tv_sec + up.tv_usec / 1000000.0) : mkf_u32(FN_DURATION, up.tv_sec);
Willy Tarreau6093ba42023-02-07 15:52:14 +01004637 info[INF_START_TIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, start_time.tv_sec + start_time.tv_usec / 1000000.0) : mkf_u32(FN_DURATION, start_time.tv_sec);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004638 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004639 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004640 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004641 info[INF_POOL_ALLOC_BYTES] = mkf_u64(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004642 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004643 info[INF_POOL_USED_BYTES] = mkf_u64(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004644 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4645 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4646 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4647 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4648 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4649 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4650 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4651 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4652#ifdef USE_OPENSSL
4653 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004654 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4655 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004656#endif
4657 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4658 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4659 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004660 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 +01004661 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4662 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004663 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 +01004664 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4665 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4666
4667#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004668 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 +01004669 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4670 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004671 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 +01004672 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004673 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4674 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 +01004675 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4676 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4677 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4678#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004679 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));
4680 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 +01004681 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4682#ifdef USE_ZLIB
4683 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4684 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4685#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004686 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004687 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004688 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004689 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4690 if (global.desc)
4691 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004692 info[INF_STOPPING] = mkf_u32(0, stopping);
4693 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004694 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004695 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004696 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004697 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004698 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004699 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004700 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau6be8d092023-01-12 16:08:41 +01004701 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, glob_out_bytes);
4702 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, glob_spl_bytes);
4703 info[INF_BYTES_OUT_RATE] = mkf_u64(FN_RATE, glob_out_b32);
Willy Tarreau9b013702019-10-24 18:18:02 +02004704 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004705 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004706
4707 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4708 chunk_appendf(out, "%#x", get_tainted());
4709
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004710 return 1;
4711}
4712
Willy Tarreau4596fe22022-05-17 19:07:51 +02004713/* This function dumps information onto the stream connector's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004714 * It returns 0 as long as it does not complete, non-zero upon completion.
4715 * No state is used.
4716 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004717static int stats_dump_info_to_buffer(struct stconn *sc)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004718{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004719 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004720 struct show_stat_ctx *ctx = appctx->svcctx;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004721 int ret;
4722 int current_field;
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004723
Willy Tarreau91cefca2022-05-03 17:08:29 +02004724 if (!stats_fill_info(info, INF_TOTAL_FIELDS, ctx->flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004725 return 0;
4726
Christopher Fauleta8b76842022-12-16 15:08:36 +01004727 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004728more:
4729 current_field = ctx->field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004730
Willy Tarreau91cefca2022-05-03 17:08:29 +02004731 if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004732 ret = stats_dump_typed_info_fields(&trash_chunk, info, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004733 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004734 ret = stats_dump_json_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004735 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01004736 ret = stats_dump_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004737
Christopher Fauleta8b76842022-12-16 15:08:36 +01004738 if (applet_putchk(appctx, &trash_chunk) == -1) {
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004739 /* restore previous field */
4740 ctx->field = current_field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004741 return 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004742 }
4743 if (ret && ctx->field) {
4744 /* partial dump */
4745 goto more;
4746 }
4747 ctx->field = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004748 return 1;
4749}
4750
Willy Tarreau4596fe22022-05-17 19:07:51 +02004751/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004752 * It returns 0 as long as it does not complete, non-zero upon completion.
4753 * No state is used.
4754 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004755 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004756 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4757 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004758static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004759{
4760
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004761 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004762
4763 chunk_strcat(out,
4764 "{"
4765 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4766 "\"oneOf\":["
4767 "{"
4768 "\"title\":\"Info\","
4769 "\"type\":\"array\","
4770 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004771 "\"title\":\"InfoItem\","
4772 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004773 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004774 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4775 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4776 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4777 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4778 "},"
4779 "\"required\":[\"field\",\"processNum\",\"tags\","
4780 "\"value\"]"
4781 "}"
4782 "},"
4783 "{"
4784 "\"title\":\"Stat\","
4785 "\"type\":\"array\","
4786 "\"items\":{"
4787 "\"title\":\"InfoItem\","
4788 "\"type\":\"object\","
4789 "\"properties\":{"
4790 "\"objType\":{"
4791 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4792 "\"Server\",\"Unknown\"]"
4793 "},"
4794 "\"proxyId\":{"
4795 "\"type\":\"integer\","
4796 "\"minimum\":0"
4797 "},"
4798 "\"id\":{"
4799 "\"type\":\"integer\","
4800 "\"minimum\":0"
4801 "},"
4802 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4803 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4804 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4805 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4806 "},"
4807 "\"required\":[\"objType\",\"proxyId\",\"id\","
4808 "\"field\",\"processNum\",\"tags\","
4809 "\"value\"]"
4810 "}"
4811 "},"
4812 "{"
4813 "\"title\":\"Error\","
4814 "\"type\":\"object\","
4815 "\"properties\":{"
4816 "\"errorStr\":{"
4817 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004818 "}"
4819 "},"
4820 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004821 "}"
4822 "],"
4823 "\"definitions\":{"
4824 "\"field\":{"
4825 "\"type\":\"object\","
4826 "\"pos\":{"
4827 "\"type\":\"integer\","
4828 "\"minimum\":0"
4829 "},"
4830 "\"name\":{"
4831 "\"type\":\"string\""
4832 "},"
4833 "\"required\":[\"pos\",\"name\"]"
4834 "},"
4835 "\"processNum\":{"
4836 "\"type\":\"integer\","
4837 "\"minimum\":1"
4838 "},"
4839 "\"tags\":{"
4840 "\"type\":\"object\","
4841 "\"origin\":{"
4842 "\"type\":\"string\","
4843 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4844 "\"Config\",\"Product\",\"Unknown\"]"
4845 "},"
4846 "\"nature\":{"
4847 "\"type\":\"string\","
4848 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4849 "\"Rate\",\"Counter\",\"Duration\","
4850 "\"Age\",\"Time\",\"Name\",\"Output\","
4851 "\"Avg\", \"Unknown\"]"
4852 "},"
4853 "\"scope\":{"
4854 "\"type\":\"string\","
4855 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4856 "\"System\",\"Unknown\"]"
4857 "},"
4858 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4859 "},"
4860 "\"typedValue\":{"
4861 "\"type\":\"object\","
4862 "\"oneOf\":["
4863 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4864 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4865 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4866 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4867 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4868 "],"
4869 "\"definitions\":{"
4870 "\"s32Value\":{"
4871 "\"properties\":{"
4872 "\"type\":{"
4873 "\"type\":\"string\","
4874 "\"enum\":[\"s32\"]"
4875 "},"
4876 "\"value\":{"
4877 "\"type\":\"integer\","
4878 "\"minimum\":-2147483648,"
4879 "\"maximum\":2147483647"
4880 "}"
4881 "},"
4882 "\"required\":[\"type\",\"value\"]"
4883 "},"
4884 "\"s64Value\":{"
4885 "\"properties\":{"
4886 "\"type\":{"
4887 "\"type\":\"string\","
4888 "\"enum\":[\"s64\"]"
4889 "},"
4890 "\"value\":{"
4891 "\"type\":\"integer\","
4892 "\"minimum\":-9007199254740991,"
4893 "\"maximum\":9007199254740991"
4894 "}"
4895 "},"
4896 "\"required\":[\"type\",\"value\"]"
4897 "},"
4898 "\"u32Value\":{"
4899 "\"properties\":{"
4900 "\"type\":{"
4901 "\"type\":\"string\","
4902 "\"enum\":[\"u32\"]"
4903 "},"
4904 "\"value\":{"
4905 "\"type\":\"integer\","
4906 "\"minimum\":0,"
4907 "\"maximum\":4294967295"
4908 "}"
4909 "},"
4910 "\"required\":[\"type\",\"value\"]"
4911 "},"
4912 "\"u64Value\":{"
4913 "\"properties\":{"
4914 "\"type\":{"
4915 "\"type\":\"string\","
4916 "\"enum\":[\"u64\"]"
4917 "},"
4918 "\"value\":{"
4919 "\"type\":\"integer\","
4920 "\"minimum\":0,"
4921 "\"maximum\":9007199254740991"
4922 "}"
4923 "},"
4924 "\"required\":[\"type\",\"value\"]"
4925 "},"
4926 "\"strValue\":{"
4927 "\"properties\":{"
4928 "\"type\":{"
4929 "\"type\":\"string\","
4930 "\"enum\":[\"str\"]"
4931 "},"
4932 "\"value\":{\"type\":\"string\"}"
4933 "},"
4934 "\"required\":[\"type\",\"value\"]"
4935 "},"
4936 "\"unknownValue\":{"
4937 "\"properties\":{"
4938 "\"type\":{"
4939 "\"type\":\"integer\","
4940 "\"minimum\":0"
4941 "},"
4942 "\"value\":{"
4943 "\"type\":\"string\","
4944 "\"enum\":[\"unknown\"]"
4945 "}"
4946 "},"
4947 "\"required\":[\"type\",\"value\"]"
4948 "}"
4949 "}"
4950 "}"
4951 "}"
4952 "}");
4953
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004954 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004955 chunk_reset(out);
4956 chunk_appendf(out,
4957 "{\"errorStr\":\"output buffer too short\"}");
4958 }
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02004959 chunk_appendf(out, "\n");
Simon Horman6f6bb382017-01-04 09:37:26 +01004960}
4961
Willy Tarreau4596fe22022-05-17 19:07:51 +02004962/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004963 * It returns 0 as long as it does not complete, non-zero upon completion.
4964 * No state is used.
4965 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004966static int stats_dump_json_schema_to_buffer(struct appctx *appctx)
Simon Horman6f6bb382017-01-04 09:37:26 +01004967{
Simon Horman6f6bb382017-01-04 09:37:26 +01004968
Christopher Fauleta8b76842022-12-16 15:08:36 +01004969 chunk_reset(&trash_chunk);
Simon Horman6f6bb382017-01-04 09:37:26 +01004970
Christopher Fauleta8b76842022-12-16 15:08:36 +01004971 stats_dump_json_schema(&trash_chunk);
4972
4973 if (applet_putchk(appctx, &trash_chunk) == -1)
Simon Horman6f6bb382017-01-04 09:37:26 +01004974 return 0;
Simon Horman6f6bb382017-01-04 09:37:26 +01004975
4976 return 1;
4977}
4978
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004979static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004980{
4981 struct proxy *px;
4982 struct server *sv;
4983 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004984 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004985 int clrall = 0;
4986
4987 if (strcmp(args[2], "all") == 0)
4988 clrall = 1;
4989
4990 /* check permissions */
4991 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4992 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4993 return 1;
4994
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004995 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004996 if (clrall) {
4997 memset(&px->be_counters, 0, sizeof(px->be_counters));
4998 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4999 }
5000 else {
5001 px->be_counters.conn_max = 0;
5002 px->be_counters.p.http.rps_max = 0;
5003 px->be_counters.sps_max = 0;
5004 px->be_counters.cps_max = 0;
5005 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005006 px->be_counters.qtime_max = 0;
5007 px->be_counters.ctime_max = 0;
5008 px->be_counters.dtime_max = 0;
5009 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005010
5011 px->fe_counters.conn_max = 0;
5012 px->fe_counters.p.http.rps_max = 0;
5013 px->fe_counters.sps_max = 0;
5014 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005015 }
5016
5017 for (sv = px->srv; sv; sv = sv->next)
5018 if (clrall)
5019 memset(&sv->counters, 0, sizeof(sv->counters));
5020 else {
5021 sv->counters.cur_sess_max = 0;
5022 sv->counters.nbpend_max = 0;
5023 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005024 sv->counters.qtime_max = 0;
5025 sv->counters.ctime_max = 0;
5026 sv->counters.dtime_max = 0;
5027 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005028 }
5029
5030 list_for_each_entry(li, &px->conf.listeners, by_fe)
5031 if (li->counters) {
5032 if (clrall)
5033 memset(li->counters, 0, sizeof(*li->counters));
5034 else
5035 li->counters->conn_max = 0;
5036 }
5037 }
5038
5039 global.cps_max = 0;
5040 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02005041 global.ssl_max = 0;
5042 global.ssl_fe_keys_max = 0;
5043 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005044
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005045 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5046 if (!mod->clearable && !clrall)
5047 continue;
5048
5049 for (px = proxies_list; px; px = px->next) {
5050 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
5051
5052 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
5053 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
5054 mod,
5055 mod->counters,
5056 mod->counters_size);
5057 }
5058
5059 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
5060 EXTRA_COUNTERS_INIT(px->extra_counters_be,
5061 mod,
5062 mod->counters,
5063 mod->counters_size);
5064 }
5065
5066 if (mod_cap & STATS_PX_CAP_SRV) {
5067 for (sv = px->srv; sv; sv = sv->next) {
5068 EXTRA_COUNTERS_INIT(sv->extra_counters,
5069 mod,
5070 mod->counters,
5071 mod->counters_size);
5072 }
5073 }
5074
5075 if (mod_cap & STATS_PX_CAP_LI) {
5076 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5077 EXTRA_COUNTERS_INIT(li->extra_counters,
5078 mod,
5079 mod->counters,
5080 mod->counters_size);
5081 }
5082 }
5083 }
5084 }
5085
Emeric Brunf8642ee2021-10-29 17:59:18 +02005086 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005087
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005088 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01005089 return 1;
5090}
5091
5092
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005093static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005094{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005095 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005096 int arg = 2;
5097
Willy Tarreau91cefca2022-05-03 17:08:29 +02005098 ctx->scope_str = 0;
5099 ctx->scope_len = 0;
5100 ctx->flags = 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01005101 ctx->field = 0; /* explicit default value */
Willy Tarreaud25fc792016-12-16 12:33:47 +01005102
Willy Tarreau2f397382019-10-09 11:43:59 +02005103 while (*args[arg]) {
5104 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005105 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005106 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005107 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005108 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005109 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02005110 else if (strcmp(args[arg], "float") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005111 ctx->flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02005112 arg++;
5113 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005114 return 0;
5115}
5116
5117
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005118static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01005119{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005120 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005121 int arg = 2;
5122
Willy Tarreau91cefca2022-05-03 17:08:29 +02005123 ctx->scope_str = 0;
5124 ctx->scope_len = 0;
5125 ctx->flags = STAT_SHNODE | STAT_SHDESC;
Willy Tarreau578d6e42019-10-09 11:00:22 +02005126
Willy Tarreau0698c802022-05-11 14:09:57 +02005127 if ((strm_li(appctx_strm(appctx))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005128 ctx->flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01005129
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005130 /* proxy is the default domain */
Willy Tarreau91cefca2022-05-03 17:08:29 +02005131 ctx->domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005132 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005133 ++args;
5134
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005135 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005136 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005137 } else if (strcmp(args[arg], "resolvers") == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02005138 ctx->domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005139 ++args;
5140 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005141 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005142 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005143 }
5144
Willy Tarreau91cefca2022-05-03 17:08:29 +02005145 if (ctx->domain == STATS_DOMAIN_PROXY
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005146 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005147 struct proxy *px;
5148
Willy Tarreau2f397382019-10-09 11:43:59 +02005149 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005150 if (px)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005151 ctx->iid = px->uuid;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005152 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02005153 ctx->iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005154
Willy Tarreau91cefca2022-05-03 17:08:29 +02005155 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02005156 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005157
Willy Tarreau91cefca2022-05-03 17:08:29 +02005158 ctx->flags |= STAT_BOUND;
5159 ctx->type = atoi(args[arg+1]);
5160 ctx->sid = atoi(args[arg+2]);
Willy Tarreau2f397382019-10-09 11:43:59 +02005161 arg += 3;
5162 }
5163
5164 while (*args[arg]) {
5165 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005166 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005167 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005168 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005169 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005170 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02005171 else if (strcmp(args[arg], "no-maint") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005172 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02005173 else if (strcmp(args[arg], "up") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005174 ctx->flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02005175 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01005176 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01005177
Willy Tarreau2b812e22016-11-22 16:18:05 +01005178 return 0;
5179}
5180
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005181static int cli_io_handler_dump_info(struct appctx *appctx)
5182{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005183 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005184 return stats_dump_info_to_buffer(appctx_sc(appctx));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005185}
5186
Willy Tarreau4596fe22022-05-17 19:07:51 +02005187/* This I/O handler runs as an applet embedded in a stream connector. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01005188 * used to send raw stats over a socket.
5189 */
5190static int cli_io_handler_dump_stat(struct appctx *appctx)
5191{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005192 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005193 return stats_dump_stat_to_buffer(appctx_sc(appctx), NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01005194}
5195
Simon Horman6f6bb382017-01-04 09:37:26 +01005196static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5197{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005198 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005199 return stats_dump_json_schema_to_buffer(appctx);
Simon Horman6f6bb382017-01-04 09:37:26 +01005200}
5201
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005202int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5203 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005204{
5205 struct stats_module *mod;
5206
5207 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5208
5209 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5210 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5211 continue;
5212
5213 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5214 }
5215
5216 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5217
5218 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5219 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5220 continue;
5221
5222 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5223 }
5224
5225 return 1;
5226
5227 alloc_failed:
5228 return 0;
5229}
5230
5231/* Initialize and allocate all extra counters for a proxy and its attached
5232 * servers/listeners with all already registered stats module
5233 */
5234int stats_allocate_proxy_counters(struct proxy *px)
5235{
5236 struct server *sv;
5237 struct listener *li;
5238
5239 if (px->cap & PR_CAP_FE) {
5240 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5241 COUNTERS_FE,
5242 STATS_PX_CAP_FE)) {
5243 return 0;
5244 }
5245 }
5246
5247 if (px->cap & PR_CAP_BE) {
5248 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5249 COUNTERS_BE,
5250 STATS_PX_CAP_BE)) {
5251 return 0;
5252 }
5253 }
5254
5255 for (sv = px->srv; sv; sv = sv->next) {
5256 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5257 COUNTERS_SV,
5258 STATS_PX_CAP_SRV)) {
5259 return 0;
5260 }
5261 }
5262
5263 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5264 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5265 COUNTERS_LI,
5266 STATS_PX_CAP_LI)) {
5267 return 0;
5268 }
5269 }
5270
5271 return 1;
5272}
5273
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005274void stats_register_module(struct stats_module *m)
5275{
5276 const uint8_t domain = stats_get_domain(m->domain_flags);
5277
Willy Tarreau2b718102021-04-21 07:32:39 +02005278 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005279 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005280}
5281
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005282static int allocate_stats_px_postcheck(void)
5283{
5284 struct stats_module *mod;
5285 size_t i = ST_F_TOTAL_FIELDS;
5286 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005287 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005288
5289 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5290
5291 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5292 if (!stat_f[STATS_DOMAIN_PROXY]) {
5293 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5294 err_code |= ERR_ALERT | ERR_FATAL;
5295 return err_code;
5296 }
5297
5298 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5299 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5300
5301 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5302 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5303 mod->stats,
5304 mod->stats_count * sizeof(struct name_desc));
5305 i += mod->stats_count;
5306 }
5307
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005308 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005309 if (!stats_allocate_proxy_counters(px)) {
5310 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5311 err_code |= ERR_ALERT | ERR_FATAL;
5312 return err_code;
5313 }
5314 }
5315
Christopher Fauletde79cd22021-01-06 07:41:56 +01005316 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005317
5318 return err_code;
5319}
5320
5321REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5322
Emeric Brunf8642ee2021-10-29 17:59:18 +02005323static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005324{
5325 struct stats_module *mod;
5326 size_t i = 0;
5327 int err_code = 0;
5328
Emeric Brunf8642ee2021-10-29 17:59:18 +02005329 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5330 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5331 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005332 err_code |= ERR_ALERT | ERR_FATAL;
5333 return err_code;
5334 }
5335
Emeric Brunf8642ee2021-10-29 17:59:18 +02005336 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5337 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005338 mod->stats,
5339 mod->stats_count * sizeof(struct name_desc));
5340 i += mod->stats_count;
5341 }
5342
Emeric Brunf8642ee2021-10-29 17:59:18 +02005343 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5344 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005345 err_code |= ERR_ALERT | ERR_FATAL;
5346 return err_code;
5347 }
5348
Christopher Fauletde79cd22021-01-06 07:41:56 +01005349 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005350
5351 return err_code;
5352}
5353
Emeric Brunf8642ee2021-10-29 17:59:18 +02005354REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005355
Christopher Fauletde79cd22021-01-06 07:41:56 +01005356static int allocate_stat_lines_per_thread(void)
5357{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005358 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005359
5360 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5361 const int domain = domains[i];
5362
5363 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5364 if (!stat_l[domain])
5365 return 0;
5366 }
5367 return 1;
5368}
5369
5370REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5371
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005372static int allocate_trash_counters(void)
5373{
5374 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005375 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005376 size_t max_counters_size = 0;
5377
5378 /* calculate the greatest counters used by any stats modules */
5379 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5380 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5381 max_counters_size = mod->counters_size > max_counters_size ?
5382 mod->counters_size : max_counters_size;
5383 }
5384 }
5385
5386 /* allocate the trash with the size of the greatest counters */
5387 if (max_counters_size) {
5388 trash_counters = malloc(max_counters_size);
5389 if (!trash_counters) {
5390 ha_alert("stats: cannot allocate trash counters for statistics\n");
5391 return 0;
5392 }
5393 }
5394
5395 return 1;
5396}
5397
5398REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5399
Christopher Fauletde79cd22021-01-06 07:41:56 +01005400static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005401{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005402 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005403
5404 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5405 const int domain = domains[i];
5406
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005407 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005408 }
5409}
5410
5411
5412REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5413
5414static void deinit_stats(void)
5415{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005416 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005417
5418 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5419 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005420
5421 if (stat_f[domain])
5422 free(stat_f[domain]);
5423 }
5424}
5425
5426REGISTER_POST_DEINIT(deinit_stats);
5427
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005428static void free_trash_counters(void)
5429{
5430 if (trash_counters)
5431 free(trash_counters);
5432}
5433
5434REGISTER_PER_THREAD_FREE(free_trash_counters);
5435
Willy Tarreau2b812e22016-11-22 16:18:05 +01005436/* register cli keywords */
5437static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005438 { { "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 +02005439 { { "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 +02005440 { { "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 },
5441 { { "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 +01005442 {{},}
5443}};
5444
Willy Tarreau0108d902018-11-25 19:14:37 +01005445INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5446
William Lallemand74c24fb2016-11-21 17:18:36 +01005447struct applet http_stats_applet = {
5448 .obj_type = OBJ_TYPE_APPLET,
5449 .name = "<STATS>", /* used for logging */
5450 .fct = http_stats_io_handler,
5451 .release = NULL,
5452};
5453
William Lallemand74c24fb2016-11-21 17:18:36 +01005454/*
5455 * Local variables:
5456 * c-indent-level: 8
5457 * c-basic-offset: 8
5458 * End:
5459 */