blob: 791b7927976ebfe4ceef9e9791da7a4a44e79d59 [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
Amaury Denoyelle3ca927e2020-10-02 18:32:00 +0200308int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
Christopher Fauletef779222018-10-31 08:47:01 +0100309{
310 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100311 if (chk->data >= channel_htx_recv_max(chn, htx))
312 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200313 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100314 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100315 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100316 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100317 }
318 else {
319 if (ci_putchk(chn, chk) == -1)
320 return 0;
321 }
322 return 1;
323}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100324
Willy Tarreaucaff6312022-05-27 10:17:46 +0200325static const char *stats_scope_ptr(struct appctx *appctx, struct stconn *sc)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100326{
Willy Tarreau91cefca2022-05-03 17:08:29 +0200327 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +0200328 struct channel *req = sc_oc(sc);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200329 struct htx *htx = htxbuf(&req->buf);
330 struct htx_blk *blk;
331 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100332
Christopher Fauletb7f88902019-07-15 21:56:43 +0200333 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100334 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200335 ALREADY_CHECKED(blk);
336 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Willy Tarreau91cefca2022-05-03 17:08:29 +0200337 return uri.ptr + ctx->scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100338}
339
William Lallemand74c24fb2016-11-21 17:18:36 +0100340/*
341 * http_stats_io_handler()
342 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
343 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100344 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100345 * -> stats_dump_html_head() // emits the HTML headers
346 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
347 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
348 * -> stats_dump_html_px_hdr()
349 * -> stats_dump_fe_stats()
350 * -> stats_dump_li_stats()
351 * -> stats_dump_sv_stats()
352 * -> stats_dump_be_stats()
353 * -> stats_dump_html_px_end()
354 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100355 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100356 */
357
358
Christopher Fauleta8b76842022-12-16 15:08:36 +0100359/* Dumps the stats CSV header to the local trash buffer. The caller is
360 * responsible for clearing it if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +0100361 * NOTE: Some tools happen to rely on the field position instead of its name,
362 * so please only append new fields at the end, never in the middle.
363 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200364static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100365{
366 int field;
367
Christopher Fauleta8b76842022-12-16 15:08:36 +0100368 chunk_appendf(&trash_chunk, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200369 if (stat_f[domain]) {
370 for (field = 0; field < stat_count[domain]; ++field) {
Christopher Fauleta8b76842022-12-16 15:08:36 +0100371 chunk_appendf(&trash_chunk, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100372
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200373 /* print special delimiter on proxy stats to mark end of
374 static fields */
375 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
Christopher Fauleta8b76842022-12-16 15:08:36 +0100376 chunk_appendf(&trash_chunk, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200377 }
378 }
379
Christopher Fauleta8b76842022-12-16 15:08:36 +0100380 chunk_appendf(&trash_chunk, "\n");
William Lallemand74c24fb2016-11-21 17:18:36 +0100381}
382
William Lallemand74c24fb2016-11-21 17:18:36 +0100383/* Emits a stats field without any surrounding element and properly encoded to
384 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
385 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200386int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100387{
388 switch (field_format(f, 0)) {
389 case FF_EMPTY: return 1;
390 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
391 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
392 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
393 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200394 case FF_FLT: {
395 size_t prev_data = out->data;
396 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
397 return out->data;
398 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100399 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
400 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
401 }
402}
403
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200404const char *field_to_html_str(const struct field *f)
405{
406 switch (field_format(f, 0)) {
407 case FF_S32: return U2H(f->u.s32);
408 case FF_S64: return U2H(f->u.s64);
409 case FF_U64: return U2H(f->u.u64);
410 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200411 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200412 case FF_STR: return field_str(f, 0);
413 case FF_EMPTY:
414 default:
415 return "";
416 }
417}
418
William Lallemand74c24fb2016-11-21 17:18:36 +0100419/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
420 * output is supposed to be used on its own line. Returns non-zero on success, 0
421 * if the buffer is full.
422 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200423int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100424{
425 switch (field_format(f, 0)) {
426 case FF_EMPTY: return 1;
427 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
428 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
429 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
430 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200431 case FF_FLT: {
432 size_t prev_data = out->data;
433 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
434 return out->data;
435 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100436 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
437 default: return chunk_appendf(out, "%08x:?", f->type);
438 }
439}
440
Simon Horman05ee2132017-01-04 09:37:25 +0100441/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
442 * the recommendation for interoperable integers in section 6 of RFC 7159.
443 */
444#define JSON_INT_MAX ((1ULL << 53) - 1)
445#define JSON_INT_MIN (0 - JSON_INT_MAX)
446
447/* Emits a stats field value and its type in JSON.
448 * Returns non-zero on success, 0 on error.
449 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200450int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100451{
452 int old_len;
453 char buf[20];
454 const char *type, *value = buf, *quote = "";
455
456 switch (field_format(f, 0)) {
457 case FF_EMPTY: return 1;
458 case FF_S32: type = "\"s32\"";
459 snprintf(buf, sizeof(buf), "%d", f->u.s32);
460 break;
461 case FF_U32: type = "\"u32\"";
462 snprintf(buf, sizeof(buf), "%u", f->u.u32);
463 break;
464 case FF_S64: type = "\"s64\"";
465 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
466 return 0;
467 type = "\"s64\"";
468 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
469 break;
470 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
471 return 0;
472 type = "\"u64\"";
473 snprintf(buf, sizeof(buf), "%llu",
474 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200475 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200476 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200477 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100478 break;
479 case FF_STR: type = "\"str\"";
480 value = field_str(f, 0);
481 quote = "\"";
482 break;
483 default: snprintf(buf, sizeof(buf), "%u", f->type);
484 type = buf;
485 value = "unknown";
486 quote = "\"";
487 break;
488 }
489
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200490 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100491 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
492 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200493 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100494}
495
William Lallemand74c24fb2016-11-21 17:18:36 +0100496/* Emits an encoding of the field type on 3 characters followed by a delimiter.
497 * Returns non-zero on success, 0 if the buffer is full.
498 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200499int stats_emit_field_tags(struct buffer *out, const struct field *f,
500 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100501{
502 char origin, nature, scope;
503
504 switch (field_origin(f, 0)) {
505 case FO_METRIC: origin = 'M'; break;
506 case FO_STATUS: origin = 'S'; break;
507 case FO_KEY: origin = 'K'; break;
508 case FO_CONFIG: origin = 'C'; break;
509 case FO_PRODUCT: origin = 'P'; break;
510 default: origin = '?'; break;
511 }
512
513 switch (field_nature(f, 0)) {
514 case FN_GAUGE: nature = 'G'; break;
515 case FN_LIMIT: nature = 'L'; break;
516 case FN_MIN: nature = 'm'; break;
517 case FN_MAX: nature = 'M'; break;
518 case FN_RATE: nature = 'R'; break;
519 case FN_COUNTER: nature = 'C'; break;
520 case FN_DURATION: nature = 'D'; break;
521 case FN_AGE: nature = 'A'; break;
522 case FN_TIME: nature = 'T'; break;
523 case FN_NAME: nature = 'N'; break;
524 case FN_OUTPUT: nature = 'O'; break;
525 case FN_AVG: nature = 'a'; break;
526 default: nature = '?'; break;
527 }
528
529 switch (field_scope(f, 0)) {
530 case FS_PROCESS: scope = 'P'; break;
531 case FS_SERVICE: scope = 'S'; break;
532 case FS_SYSTEM: scope = 's'; break;
533 case FS_CLUSTER: scope = 'C'; break;
534 default: scope = '?'; break;
535 }
536
537 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
538}
539
Simon Horman05ee2132017-01-04 09:37:25 +0100540/* Emits an encoding of the field type as JSON.
541 * Returns non-zero on success, 0 if the buffer is full.
542 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200543int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100544{
545 const char *origin, *nature, *scope;
546 int old_len;
547
548 switch (field_origin(f, 0)) {
549 case FO_METRIC: origin = "Metric"; break;
550 case FO_STATUS: origin = "Status"; break;
551 case FO_KEY: origin = "Key"; break;
552 case FO_CONFIG: origin = "Config"; break;
553 case FO_PRODUCT: origin = "Product"; break;
554 default: origin = "Unknown"; break;
555 }
556
557 switch (field_nature(f, 0)) {
558 case FN_GAUGE: nature = "Gauge"; break;
559 case FN_LIMIT: nature = "Limit"; break;
560 case FN_MIN: nature = "Min"; break;
561 case FN_MAX: nature = "Max"; break;
562 case FN_RATE: nature = "Rate"; break;
563 case FN_COUNTER: nature = "Counter"; break;
564 case FN_DURATION: nature = "Duration"; break;
565 case FN_AGE: nature = "Age"; break;
566 case FN_TIME: nature = "Time"; break;
567 case FN_NAME: nature = "Name"; break;
568 case FN_OUTPUT: nature = "Output"; break;
569 case FN_AVG: nature = "Avg"; break;
570 default: nature = "Unknown"; break;
571 }
572
573 switch (field_scope(f, 0)) {
574 case FS_PROCESS: scope = "Process"; break;
575 case FS_SERVICE: scope = "Service"; break;
576 case FS_SYSTEM: scope = "System"; break;
577 case FS_CLUSTER: scope = "Cluster"; break;
578 default: scope = "Unknown"; break;
579 }
580
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200581 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100582 chunk_appendf(out, "\"tags\":{"
583 "\"origin\":\"%s\","
584 "\"nature\":\"%s\","
585 "\"scope\":\"%s\""
586 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200587 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100588}
William Lallemand74c24fb2016-11-21 17:18:36 +0100589
590/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200591static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200592 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100593 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100594{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100595 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100596 int field;
597
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200598 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100599 if (!stats_emit_raw_data_field(out, &stats[field]))
600 return 0;
601 if (!chunk_strcat(out, ","))
602 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200603
604 /* print special delimiter on proxy stats to mark end of
605 static fields */
606 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
607 if (!chunk_strcat(out, "-,"))
608 return 0;
609 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100610 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200611
William Lallemand74c24fb2016-11-21 17:18:36 +0100612 chunk_strcat(out, "\n");
613 return 1;
614}
615
616/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200617static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200618 const struct field *stats,
619 size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100620 struct show_stat_ctx * ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100621{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100622 int flags = ctx->flags;
623 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100624 int field;
625
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200626 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100627 if (!stats[field].type)
628 continue;
629
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200630 switch (domain) {
631 case STATS_DOMAIN_PROXY:
632 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
633 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
634 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
635 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
636 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
637 '?',
638 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
639 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200640 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200641 stats[ST_F_PID].u.u32);
642 break;
643
Emeric Brunf8642ee2021-10-29 17:59:18 +0200644 case STATS_DOMAIN_RESOLVERS:
645 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200646 stat_f[domain][field].name);
647 break;
648
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200649 default:
650 break;
651 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100652
653 if (!stats_emit_field_tags(out, &stats[field], ':'))
654 return 0;
655 if (!stats_emit_typed_data_field(out, &stats[field]))
656 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200657
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200658 if (flags & STAT_SHOW_FDESC &&
659 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200660 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200661 }
662
William Lallemand74c24fb2016-11-21 17:18:36 +0100663 if (!chunk_strcat(out, "\n"))
664 return 0;
665 }
666 return 1;
667}
668
Simon Horman05ee2132017-01-04 09:37:25 +0100669/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200670static int stats_dump_json_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100671 const struct field *info,
672 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100673{
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100674 int started = (ctx->field) ? 1 : 0;
675 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100676
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100677 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100678 return 0;
679
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100680 for (; ctx->field < INF_TOTAL_FIELDS; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100681 int old_len;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100682 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100683
684 if (!field_format(info, field))
685 continue;
686
687 if (started && !chunk_strcat(out, ","))
688 goto err;
689 started = 1;
690
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200691 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100692 chunk_appendf(out,
693 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
694 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200695 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100696 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200697 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100698 goto err;
699
700 if (!stats_emit_json_field_tags(out, &info[field]))
701 goto err;
702
703 if (!stats_emit_json_data_field(out, &info[field]))
704 goto err;
705
706 if (!chunk_strcat(out, "}"))
707 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100708 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100709 }
710
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200711 if (!chunk_strcat(out, "]\n"))
Simon Horman05ee2132017-01-04 09:37:25 +0100712 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100713 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100714 return 1;
715
716err:
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100717 if (!ready_data) {
718 /* not enough buffer space for a single entry.. */
719 chunk_reset(out);
720 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
721 return 0; /* hard error */
722 }
723 /* push ready data and wait for a new buffer to complete the dump */
724 out->data = ready_data;
725 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100726}
727
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200728static void stats_print_proxy_field_json(struct buffer *out,
729 const struct field *stat,
730 const char *name,
731 int pos,
732 uint32_t field_type,
733 uint32_t iid,
734 uint32_t sid,
735 uint32_t pid)
736{
737 const char *obj_type;
738 switch (field_type) {
739 case STATS_TYPE_FE: obj_type = "Frontend"; break;
740 case STATS_TYPE_BE: obj_type = "Backend"; break;
741 case STATS_TYPE_SO: obj_type = "Listener"; break;
742 case STATS_TYPE_SV: obj_type = "Server"; break;
743 default: obj_type = "Unknown"; break;
744 }
745
746 chunk_appendf(out,
747 "{"
748 "\"objType\":\"%s\","
749 "\"proxyId\":%u,"
750 "\"id\":%u,"
751 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
752 "\"processNum\":%u,",
753 obj_type, iid, sid, pos, name, pid);
754}
755
Emeric Brunf8642ee2021-10-29 17:59:18 +0200756static void stats_print_rslv_field_json(struct buffer *out,
757 const struct field *stat,
758 const char *name,
759 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200760{
761 chunk_appendf(out,
762 "{"
763 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
764 pos, name);
765}
766
767
Simon Horman05ee2132017-01-04 09:37:25 +0100768/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200769static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200770 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100771 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100772{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100773 int flags = ctx->flags;
774 int domain = ctx->domain;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100775 int started = (ctx->field) ? 1 : 0;
776 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100777
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100778 if (!started && (flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100779 return 0;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100780 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100781 return 0;
782
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100783 for (; ctx->field < stats_count; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100784 int old_len;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100785 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100786
787 if (!stats[field].type)
788 continue;
789
790 if (started && !chunk_strcat(out, ","))
791 goto err;
792 started = 1;
793
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200794 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200795 if (domain == STATS_DOMAIN_PROXY) {
796 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200797 stat_f[domain][field].name,
798 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200799 stats[ST_F_TYPE].u.u32,
800 stats[ST_F_IID].u.u32,
801 stats[ST_F_SID].u.u32,
802 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200803 } else if (domain == STATS_DOMAIN_RESOLVERS) {
804 stats_print_rslv_field_json(out, &stats[field],
805 stat_f[domain][field].name,
806 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200807 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200808
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200809 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100810 goto err;
811
812 if (!stats_emit_json_field_tags(out, &stats[field]))
813 goto err;
814
815 if (!stats_emit_json_data_field(out, &stats[field]))
816 goto err;
817
818 if (!chunk_strcat(out, "}"))
819 goto err;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100820 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100821 }
822
823 if (!chunk_strcat(out, "]"))
824 goto err;
825
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100826 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100827 return 1;
828
829err:
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100830 if (!ready_data) {
831 /* not enough buffer space for a single entry.. */
832 chunk_reset(out);
833 if (ctx->flags & STAT_STARTED)
834 chunk_strcat(out, ",");
835 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
836 return 0; /* hard error */
837 }
838 /* push ready data and wait for a new buffer to complete the dump */
839 out->data = ready_data;
840 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100841}
842
William Lallemand74c24fb2016-11-21 17:18:36 +0100843/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200844 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200845 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
846 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
847 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100848 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200849static int stats_dump_fields_html(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100850 const struct field *stats,
851 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100852{
Willy Tarreau83061a82018-07-13 11:56:34 +0200853 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200854 struct stats_module *mod;
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100855 int flags = ctx->flags;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200856 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100857
858 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
859 chunk_appendf(out,
860 /* name, queue */
861 "<tr class=\"frontend\">");
862
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200863 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100864 /* Column sub-heading for Enable or Disable server */
865 chunk_appendf(out, "<td></td>");
866 }
867
868 chunk_appendf(out,
869 "<td class=ac>"
870 "<a name=\"%s/Frontend\"></a>"
871 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
872 "<td colspan=3></td>"
873 "",
874 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
875
876 chunk_appendf(out,
877 /* sessions rate : current */
878 "<td><u>%s<div class=tips><table class=det>"
879 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
880 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
881 "",
882 U2H(stats[ST_F_RATE].u.u32),
883 U2H(stats[ST_F_CONN_RATE].u.u32),
884 U2H(stats[ST_F_RATE].u.u32));
885
886 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
887 chunk_appendf(out,
888 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
889 U2H(stats[ST_F_REQ_RATE].u.u32));
890
891 chunk_appendf(out,
892 "</table></div></u></td>"
893 /* sessions rate : max */
894 "<td><u>%s<div class=tips><table class=det>"
895 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
896 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
897 "",
898 U2H(stats[ST_F_RATE_MAX].u.u32),
899 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
900 U2H(stats[ST_F_RATE_MAX].u.u32));
901
902 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
903 chunk_appendf(out,
904 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
905 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
906
907 chunk_appendf(out,
908 "</table></div></u></td>"
909 /* sessions rate : limit */
910 "<td>%s</td>",
911 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
912
913 chunk_appendf(out,
914 /* sessions: current, max, limit, total */
915 "<td>%s</td><td>%s</td><td>%s</td>"
916 "<td><u>%s<div class=tips><table class=det>"
917 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
918 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
919 "",
920 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
921 U2H(stats[ST_F_STOT].u.u64),
922 U2H(stats[ST_F_CONN_TOT].u.u64),
923 U2H(stats[ST_F_STOT].u.u64));
924
925 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
926 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
927 chunk_appendf(out,
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100928 "<tr><th>- HTTP/1 sessions:</th><td>%s</td></tr>"
929 "<tr><th>- HTTP/2 sessions:</th><td>%s</td></tr>"
930 "<tr><th>- HTTP/3 sessions:</th><td>%s</td></tr>"
931 "<tr><th>- other sessions:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100932 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100933 "<tr><th>- HTTP/1 requests:</th><td>%s</td></tr>"
934 "<tr><th>- HTTP/2 requests:</th><td>%s</td></tr>"
935 "<tr><th>- HTTP/3 requests:</th><td>%s</td></tr>"
936 "<tr><th>- other requests:</th><td>%s</td></tr>"
937 "",
938 U2H(stats[ST_F_H1SESS].u.u64),
939 U2H(stats[ST_F_H2SESS].u.u64),
940 U2H(stats[ST_F_H3SESS].u.u64),
941 U2H(stats[ST_F_SESS_OTHER].u.u64),
942 U2H(stats[ST_F_REQ_TOT].u.u64),
943 U2H(stats[ST_F_H1REQ].u.u64),
944 U2H(stats[ST_F_H2REQ].u.u64),
945 U2H(stats[ST_F_H3REQ].u.u64),
946 U2H(stats[ST_F_REQ_OTHER].u.u64));
947
948 chunk_appendf(out,
William Lallemand74c24fb2016-11-21 17:18:36 +0100949 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
950 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
951 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
952 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
953 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
954 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
955 "<tr><th>- other responses:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100956 "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100957 U2H(stats[ST_F_HRSP_1XX].u.u64),
958 U2H(stats[ST_F_HRSP_2XX].u.u64),
959 U2H(stats[ST_F_COMP_RSP].u.u64),
960 stats[ST_F_HRSP_2XX].u.u64 ?
961 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
962 U2H(stats[ST_F_HRSP_3XX].u.u64),
963 U2H(stats[ST_F_HRSP_4XX].u.u64),
964 U2H(stats[ST_F_HRSP_5XX].u.u64),
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100965 U2H(stats[ST_F_HRSP_OTHER].u.u64));
966
967 chunk_appendf(out,
968 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
969 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
970 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
971 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
972 "<tr><th>Internal errors:</th><td>%s</td></tr>"
973 "",
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200974 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100975 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
976 U2H(stats[ST_F_CACHE_HITS].u.u64),
977 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
978 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100979 U2H(stats[ST_F_WREW].u.u64),
980 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100981 }
982
983 chunk_appendf(out,
984 "</table></div></u></td>"
985 /* sessions: lbtot, lastsess */
986 "<td></td><td></td>"
987 /* bytes : in */
988 "<td>%s</td>"
989 "",
990 U2H(stats[ST_F_BIN].u.u64));
991
992 chunk_appendf(out,
993 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
994 "<td>%s%s<div class=tips><table class=det>"
995 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
996 "<tr><th>Compression in:</th><td>%s</td></tr>"
997 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
998 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
999 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1000 "</table></div>%s</td>",
1001 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1002 U2H(stats[ST_F_BOUT].u.u64),
1003 U2H(stats[ST_F_BOUT].u.u64),
1004 U2H(stats[ST_F_COMP_IN].u.u64),
1005 U2H(stats[ST_F_COMP_OUT].u.u64),
1006 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1007 U2H(stats[ST_F_COMP_BYP].u.u64),
1008 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1009 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,
1010 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1011
1012 chunk_appendf(out,
1013 /* denied: req, resp */
1014 "<td>%s</td><td>%s</td>"
1015 /* errors : request, connect, response */
1016 "<td>%s</td><td></td><td></td>"
1017 /* warnings: retries, redispatches */
1018 "<td></td><td></td>"
1019 /* server status : reflect frontend status */
1020 "<td class=ac>%s</td>"
1021 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001022 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001023 "",
1024 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1025 U2H(stats[ST_F_EREQ].u.u64),
1026 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001027
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001028 if (flags & STAT_SHMODULES) {
1029 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001030 chunk_appendf(out, "<td>");
1031
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001032 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001033 chunk_appendf(out,
1034 "<u>%s<div class=tips><table class=det>",
1035 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001036 for (j = 0; j < mod->stats_count; ++j) {
1037 chunk_appendf(out,
1038 "<tr><th>%s</th><td>%s</td></tr>",
1039 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1040 ++i;
1041 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001042 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001043 } else {
1044 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001045 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001046
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001047 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001048 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001049 }
1050
1051 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001052 }
1053 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
1054 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001055 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001056 /* Column sub-heading for Enable or Disable server */
1057 chunk_appendf(out, "<td></td>");
1058 }
1059
1060 chunk_appendf(out,
1061 /* frontend name, listener name */
1062 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1063 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1064 "",
1065 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001066 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001067 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1068
Willy Tarreau708c4162019-10-09 10:19:16 +02001069 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001070 chunk_appendf(out, "<div class=tips>");
1071
Willy Tarreau90807112020-02-25 08:16:33 +01001072 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001073 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1074 else if (*field_str(stats, ST_F_ADDR) == '[')
1075 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1076 else if (*field_str(stats, ST_F_ADDR))
1077 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1078
1079 /* id */
1080 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1081 }
1082
1083 chunk_appendf(out,
1084 /* queue */
1085 "%s</td><td colspan=3></td>"
1086 /* sessions rate: current, max, limit */
1087 "<td colspan=3>&nbsp;</td>"
1088 /* sessions: current, max, limit, total, lbtot, lastsess */
1089 "<td>%s</td><td>%s</td><td>%s</td>"
1090 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1091 /* bytes: in, out */
1092 "<td>%s</td><td>%s</td>"
1093 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001094 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001095 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1096 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1097
1098 chunk_appendf(out,
1099 /* denied: req, resp */
1100 "<td>%s</td><td>%s</td>"
1101 /* errors: request, connect, response */
1102 "<td>%s</td><td></td><td></td>"
1103 /* warnings: retries, redispatches */
1104 "<td></td><td></td>"
1105 /* server status: reflect listener status */
1106 "<td class=ac>%s</td>"
1107 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001108 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001109 "",
1110 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1111 U2H(stats[ST_F_EREQ].u.u64),
1112 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001113
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001114 if (flags & STAT_SHMODULES) {
1115 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001116 chunk_appendf(out, "<td>");
1117
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001118 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001119 chunk_appendf(out,
1120 "<u>%s<div class=tips><table class=det>",
1121 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001122 for (j = 0; j < mod->stats_count; ++j) {
1123 chunk_appendf(out,
1124 "<tr><th>%s</th><td>%s</td></tr>",
1125 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1126 ++i;
1127 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001128 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001129 } else {
1130 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001131 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001132
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001133 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001134 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001135 }
1136
1137 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001138 }
1139 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1140 const char *style;
1141
1142 /* determine the style to use depending on the server's state,
1143 * its health and weight. There isn't a 1-to-1 mapping between
1144 * state and styles for the cases where the server is (still)
1145 * up. The reason is that we don't want to report nolb and
1146 * drain with the same color.
1147 */
1148
1149 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1150 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1151 style = "down";
1152 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001153 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001154 style = "going_up";
1155 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001156 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1157 style = "draining";
1158 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001159 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001160 style = "going_down";
1161 }
1162 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1163 style = "nolb";
1164 }
1165 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1166 style = "no_check";
1167 }
1168 else if (!stats[ST_F_CHKFAIL].type ||
1169 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1170 /* no check or max health = UP */
1171 if (stats[ST_F_WEIGHT].u.u32)
1172 style = "up";
1173 else
1174 style = "draining";
1175 }
1176 else {
1177 style = "going_down";
1178 }
1179
Willy Tarreau7b524852020-08-11 10:26:36 +02001180 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001181 chunk_appendf(out, "<tr class=\"maintain\">");
1182 else
1183 chunk_appendf(out,
1184 "<tr class=\"%s_%s\">",
1185 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1186
1187
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001188 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001189 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001190 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1191 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001192 field_str(stats, ST_F_SVNAME));
1193
1194 chunk_appendf(out,
1195 "<td class=ac><a name=\"%s/%s\"></a>%s"
1196 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1197 "",
1198 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001199 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001200 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1201
Willy Tarreau708c4162019-10-09 10:19:16 +02001202 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001203 chunk_appendf(out, "<div class=tips>");
1204
Willy Tarreau90807112020-02-25 08:16:33 +01001205 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001206 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1207 else if (*field_str(stats, ST_F_ADDR) == '[')
1208 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1209 else if (*field_str(stats, ST_F_ADDR))
1210 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1211
1212 /* id */
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01001213 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 +01001214
1215 /* cookie */
1216 if (stats[ST_F_COOKIE].type) {
1217 chunk_appendf(out, ", cookie: '");
1218 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1219 chunk_htmlencode(out, &src);
1220 chunk_appendf(out, "'");
1221 }
1222
1223 chunk_appendf(out, "</div>");
1224 }
1225
1226 chunk_appendf(out,
1227 /* queue : current, max, limit */
1228 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1229 /* sessions rate : current, max, limit */
1230 "<td>%s</td><td>%s</td><td></td>"
1231 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001232 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001233 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1234 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1235
1236 chunk_appendf(out,
1237 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001238 "<td><u>%s<div class=tips>"
1239 "<table class=det>"
1240 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001241 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001242 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001243 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1244 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001245 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001246 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1247 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1248 "</table></div></u>"
1249 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001250 "<td><u>%s<div class=tips><table class=det>"
1251 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1252 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001253 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001254 U2H(stats[ST_F_SCUR].u.u32),
1255 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1256 U2H(stats[ST_F_SRV_ICUR].u.u32),
1257 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1258 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001259 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001260
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001261 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1262 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1263 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001264 U2H(stats[ST_F_STOT].u.u64),
1265 U2H(stats[ST_F_STOT].u.u64));
1266
1267 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1268 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001269 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001270 "<tr><th>New connections:</th><td>%s</td></tr>"
1271 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001272 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001273 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1274 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1275 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1276 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1277 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1278 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001279 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001280 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001281 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001282 U2H(stats[ST_F_CONNECT].u.u64),
1283 U2H(stats[ST_F_REUSE].u.u64),
1284 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1285 (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 +02001286 U2H(stats[ST_F_REQ_TOT].u.u64),
1287 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1288 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1289 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1290 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1291 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1292 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1293 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1294 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1295 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1296 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1297 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1298 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001299 U2H(stats[ST_F_WREW].u.u64),
1300 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001301 }
1302
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001303 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1304 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1305 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1306 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1307 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001308 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001309 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1310 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1311 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1312 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001313
1314 chunk_appendf(out,
1315 "</table></div></u></td>"
1316 /* sessions: lbtot, last */
1317 "<td>%s</td><td>%s</td>",
1318 U2H(stats[ST_F_LBTOT].u.u64),
1319 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1320
1321 chunk_appendf(out,
1322 /* bytes : in, out */
1323 "<td>%s</td><td>%s</td>"
1324 /* denied: req, resp */
1325 "<td></td><td>%s</td>"
1326 /* errors : request, connect */
1327 "<td></td><td>%s</td>"
1328 /* errors : response */
1329 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1330 /* warnings: retries, redispatches */
1331 "<td>%lld</td><td>%lld</td>"
1332 "",
1333 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1334 U2H(stats[ST_F_DRESP].u.u64),
1335 U2H(stats[ST_F_ECON].u.u64),
1336 U2H(stats[ST_F_ERESP].u.u64),
1337 (long long)stats[ST_F_CLI_ABRT].u.u64,
1338 (long long)stats[ST_F_SRV_ABRT].u.u64,
1339 (long long)stats[ST_F_WRETR].u.u64,
1340 (long long)stats[ST_F_WREDIS].u.u64);
1341
1342 /* status, last change */
1343 chunk_appendf(out, "<td class=ac>");
1344
1345 /* FIXME!!!!
1346 * LASTCHG should contain the last change for *this* server and must be computed
1347 * properly above, as was done below, ie: this server if maint, otherwise ref server
1348 * if tracking. Note that ref is either local or remote depending on tracking.
1349 */
1350
1351
Willy Tarreau7b524852020-08-11 10:26:36 +02001352 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001353 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1354 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001355 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001356 chunk_strcat(out, "<i>no check</i>");
1357 }
1358 else {
1359 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 +02001360 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001361 if (stats[ST_F_CHECK_HEALTH].u.u32)
1362 chunk_strcat(out, " &uarr;");
1363 }
1364 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1365 chunk_strcat(out, " &darr;");
1366 }
1367
Willy Tarreau7b524852020-08-11 10:26:36 +02001368 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001369 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1370 chunk_appendf(out,
1371 "</td><td class=ac><u> %s",
1372 field_str(stats, ST_F_AGENT_STATUS));
1373
1374 if (stats[ST_F_AGENT_CODE].type)
1375 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1376
1377 if (stats[ST_F_AGENT_DURATION].type)
1378 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1379
1380 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1381
1382 if (*field_str(stats, ST_F_LAST_AGT)) {
1383 chunk_appendf(out, ": ");
1384 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1385 chunk_htmlencode(out, &src);
1386 }
1387 chunk_appendf(out, "</div></u>");
1388 }
1389 else if (stats[ST_F_CHECK_STATUS].type) {
1390 chunk_appendf(out,
1391 "</td><td class=ac><u> %s",
1392 field_str(stats, ST_F_CHECK_STATUS));
1393
1394 if (stats[ST_F_CHECK_CODE].type)
1395 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1396
1397 if (stats[ST_F_CHECK_DURATION].type)
1398 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1399
1400 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1401
1402 if (*field_str(stats, ST_F_LAST_CHK)) {
1403 chunk_appendf(out, ": ");
1404 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1405 chunk_htmlencode(out, &src);
1406 }
1407 chunk_appendf(out, "</div></u>");
1408 }
1409 else
1410 chunk_appendf(out, "</td><td>");
1411
1412 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001413 /* weight / uweight */
1414 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001415 /* act, bck */
1416 "<td class=ac>%s</td><td class=ac>%s</td>"
1417 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001418 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001419 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1420 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1421
1422 /* check failures: unique, fatal, down time */
1423 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1424 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1425 }
1426 else if (stats[ST_F_CHKFAIL].type) {
1427 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1428
1429 if (stats[ST_F_HANAFAIL].type)
1430 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1431
1432 chunk_appendf(out,
1433 "<div class=tips>Failed Health Checks%s</div></u></td>"
1434 "<td>%lld</td><td>%s</td>"
1435 "",
1436 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1437 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1438 }
1439 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1440 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1441 chunk_appendf(out,
1442 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1443 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1444 }
1445 else
1446 chunk_appendf(out, "<td colspan=3></td>");
1447
1448 /* throttle */
1449 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001450 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001451 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001452 chunk_appendf(out, "<td class=ac>-</td>");
1453
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001454 if (flags & STAT_SHMODULES) {
1455 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001456 chunk_appendf(out, "<td>");
1457
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001458 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001459 chunk_appendf(out,
1460 "<u>%s<div class=tips><table class=det>",
1461 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001462 for (j = 0; j < mod->stats_count; ++j) {
1463 chunk_appendf(out,
1464 "<tr><th>%s</th><td>%s</td></tr>",
1465 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1466 ++i;
1467 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001468 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001469 } else {
1470 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001471 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001472
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001473 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001474 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001475 }
1476
1477 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001478 }
1479 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1480 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001481 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001482 /* Column sub-heading for Enable or Disable server */
1483 chunk_appendf(out, "<td></td>");
1484 }
1485 chunk_appendf(out,
1486 "<td class=ac>"
1487 /* name */
1488 "%s<a name=\"%s/Backend\"></a>"
1489 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1490 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001491 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001492 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1493
Willy Tarreau708c4162019-10-09 10:19:16 +02001494 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001495 /* balancing */
1496 chunk_appendf(out, "<div class=tips>balancing: %s",
1497 field_str(stats, ST_F_ALGO));
1498
1499 /* cookie */
1500 if (stats[ST_F_COOKIE].type) {
1501 chunk_appendf(out, ", cookie: '");
1502 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1503 chunk_htmlencode(out, &src);
1504 chunk_appendf(out, "'");
1505 }
1506 chunk_appendf(out, "</div>");
1507 }
1508
1509 chunk_appendf(out,
1510 "%s</td>"
1511 /* queue : current, max */
1512 "<td>%s</td><td>%s</td><td></td>"
1513 /* sessions rate : current, max, limit */
1514 "<td>%s</td><td>%s</td><td></td>"
1515 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001516 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001517 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1518 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1519
1520 chunk_appendf(out,
1521 /* sessions: current, max, limit, total */
1522 "<td>%s</td><td>%s</td><td>%s</td>"
1523 "<td><u>%s<div class=tips><table class=det>"
1524 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1525 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001526 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 +01001527 U2H(stats[ST_F_STOT].u.u64),
1528 U2H(stats[ST_F_STOT].u.u64));
1529
1530 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1531 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1532 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001533 "<tr><th>New connections:</th><td>%s</td></tr>"
1534 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001535 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1536 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1537 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1538 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1539 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1540 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1541 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1542 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001543 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1544 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001545 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001546 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001547 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001548 U2H(stats[ST_F_CONNECT].u.u64),
1549 U2H(stats[ST_F_REUSE].u.u64),
1550 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1551 (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 +01001552 U2H(stats[ST_F_REQ_TOT].u.u64),
1553 U2H(stats[ST_F_HRSP_1XX].u.u64),
1554 U2H(stats[ST_F_HRSP_2XX].u.u64),
1555 U2H(stats[ST_F_COMP_RSP].u.u64),
1556 stats[ST_F_HRSP_2XX].u.u64 ?
1557 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1558 U2H(stats[ST_F_HRSP_3XX].u.u64),
1559 U2H(stats[ST_F_HRSP_4XX].u.u64),
1560 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001561 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001562 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1563 U2H(stats[ST_F_CACHE_HITS].u.u64),
1564 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1565 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001566 U2H(stats[ST_F_WREW].u.u64),
1567 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001568 }
1569
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001570 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1571 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1572 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1573 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1574 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001575 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001576 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1577 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1578 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1579 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001580
1581 chunk_appendf(out,
1582 "</table></div></u></td>"
1583 /* sessions: lbtot, last */
1584 "<td>%s</td><td>%s</td>"
1585 /* bytes: in */
1586 "<td>%s</td>"
1587 "",
1588 U2H(stats[ST_F_LBTOT].u.u64),
1589 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1590 U2H(stats[ST_F_BIN].u.u64));
1591
1592 chunk_appendf(out,
1593 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1594 "<td>%s%s<div class=tips><table class=det>"
1595 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1596 "<tr><th>Compression in:</th><td>%s</td></tr>"
1597 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1598 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1599 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1600 "</table></div>%s</td>",
1601 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1602 U2H(stats[ST_F_BOUT].u.u64),
1603 U2H(stats[ST_F_BOUT].u.u64),
1604 U2H(stats[ST_F_COMP_IN].u.u64),
1605 U2H(stats[ST_F_COMP_OUT].u.u64),
1606 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1607 U2H(stats[ST_F_COMP_BYP].u.u64),
1608 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1609 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,
1610 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1611
1612 chunk_appendf(out,
1613 /* denied: req, resp */
1614 "<td>%s</td><td>%s</td>"
1615 /* errors : request, connect */
1616 "<td></td><td>%s</td>"
1617 /* errors : response */
1618 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1619 /* warnings: retries, redispatches */
1620 "<td>%lld</td><td>%lld</td>"
1621 /* backend status: reflect backend status (up/down): we display UP
1622 * if the backend has known working servers or if it has no server at
1623 * all (eg: for stats). Then we display the total weight, number of
1624 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001625 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001626 "<td class=ac>%d</td><td class=ac>%d</td>"
1627 "",
1628 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1629 U2H(stats[ST_F_ECON].u.u64),
1630 U2H(stats[ST_F_ERESP].u.u64),
1631 (long long)stats[ST_F_CLI_ABRT].u.u64,
1632 (long long)stats[ST_F_SRV_ABRT].u.u64,
1633 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1634 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1635 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 +02001636 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001637 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1638
1639 chunk_appendf(out,
1640 /* rest of backend: nothing, down transitions, total downtime, throttle */
1641 "<td class=ac>&nbsp;</td><td>%d</td>"
1642 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001643 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001644 stats[ST_F_CHKDOWN].u.u32,
1645 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001646
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001647 if (flags & STAT_SHMODULES) {
1648 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001649 chunk_appendf(out, "<td>");
1650
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001651 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001652 chunk_appendf(out,
1653 "<u>%s<div class=tips><table class=det>",
1654 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001655 for (j = 0; j < mod->stats_count; ++j) {
1656 chunk_appendf(out,
1657 "<tr><th>%s</th><td>%s</td></tr>",
1658 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1659 ++i;
1660 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001661 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001662 } else {
1663 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001664 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001665
1666 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001667 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001668 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001669
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001670 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001671 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001672
William Lallemand74c24fb2016-11-21 17:18:36 +01001673 return 1;
1674}
1675
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001676int stats_dump_one_line(const struct field *stats, size_t stats_count,
1677 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001678{
Willy Tarreau91cefca2022-05-03 17:08:29 +02001679 struct show_stat_ctx *ctx = appctx->svcctx;
Simon Horman05ee2132017-01-04 09:37:25 +01001680 int ret;
1681
Willy Tarreau91cefca2022-05-03 17:08:29 +02001682 if (ctx->flags & STAT_FMT_HTML)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001683 ret = stats_dump_fields_html(&trash_chunk, stats, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001684 else if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001685 ret = stats_dump_fields_typed(&trash_chunk, stats, stats_count, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001686 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001687 ret = stats_dump_fields_json(&trash_chunk, stats, stats_count, ctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001688 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01001689 ret = stats_dump_fields_csv(&trash_chunk, stats, stats_count, ctx);
Simon Horman05ee2132017-01-04 09:37:25 +01001690
1691 if (ret)
Willy Tarreau91cefca2022-05-03 17:08:29 +02001692 ctx->flags |= STAT_STARTED;
Simon Horman05ee2132017-01-04 09:37:25 +01001693
1694 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001695}
1696
William Dauchy0ef54392021-01-17 18:27:45 +01001697/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001698 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1699 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1700 * this value, or if the selected field is not implemented for frontends, the
1701 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001702 */
William Dauchy0ef54392021-01-17 18:27:45 +01001703int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1704 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001705{
William Dauchy0ef54392021-01-17 18:27:45 +01001706 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001707
William Lallemand74c24fb2016-11-21 17:18:36 +01001708 if (len < ST_F_TOTAL_FIELDS)
1709 return 0;
1710
William Dauchy0ef54392021-01-17 18:27:45 +01001711 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001712 struct field metric = { 0 };
1713
William Dauchy0ef54392021-01-17 18:27:45 +01001714 switch (current_field) {
1715 case ST_F_PXNAME:
1716 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1717 break;
1718 case ST_F_SVNAME:
1719 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1720 break;
1721 case ST_F_MODE:
1722 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1723 break;
1724 case ST_F_SCUR:
1725 metric = mkf_u32(0, px->feconn);
1726 break;
1727 case ST_F_SMAX:
1728 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1729 break;
1730 case ST_F_SLIM:
1731 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1732 break;
1733 case ST_F_STOT:
1734 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1735 break;
1736 case ST_F_BIN:
1737 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1738 break;
1739 case ST_F_BOUT:
1740 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1741 break;
1742 case ST_F_DREQ:
1743 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1744 break;
1745 case ST_F_DRESP:
1746 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1747 break;
1748 case ST_F_EREQ:
1749 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1750 break;
1751 case ST_F_DCON:
1752 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1753 break;
1754 case ST_F_DSES:
1755 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1756 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001757 case ST_F_STATUS: {
1758 const char *state;
1759
1760 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1761 state = "STOP";
1762 else if (px->flags & PR_FL_PAUSED)
1763 state = "PAUSED";
1764 else
1765 state = "OPEN";
1766 metric = mkf_str(FO_STATUS, state);
William Dauchy0ef54392021-01-17 18:27:45 +01001767 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001768 }
William Dauchy0ef54392021-01-17 18:27:45 +01001769 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001770 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001771 break;
1772 case ST_F_IID:
1773 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1774 break;
1775 case ST_F_SID:
1776 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1777 break;
1778 case ST_F_TYPE:
1779 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1780 break;
1781 case ST_F_RATE:
1782 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1783 break;
1784 case ST_F_RATE_LIM:
1785 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1786 break;
1787 case ST_F_RATE_MAX:
1788 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1789 break;
1790 case ST_F_WREW:
1791 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1792 break;
1793 case ST_F_EINT:
1794 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1795 break;
1796 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001797 if (px->mode == PR_MODE_HTTP)
1798 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001799 break;
1800 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001801 if (px->mode == PR_MODE_HTTP)
1802 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001803 break;
1804 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001805 if (px->mode == PR_MODE_HTTP)
1806 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001807 break;
1808 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001809 if (px->mode == PR_MODE_HTTP)
1810 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001811 break;
1812 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001813 if (px->mode == PR_MODE_HTTP)
1814 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001815 break;
1816 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001817 if (px->mode == PR_MODE_HTTP)
1818 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001819 break;
1820 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001821 if (px->mode == PR_MODE_HTTP)
1822 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001823 break;
1824 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001825 if (px->mode == PR_MODE_HTTP)
1826 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001827 break;
1828 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001829 if (px->mode == PR_MODE_HTTP)
1830 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001831 break;
1832 case ST_F_REQ_RATE:
1833 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1834 break;
1835 case ST_F_REQ_RATE_MAX:
1836 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1837 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001838 case ST_F_REQ_TOT: {
1839 int i;
1840 uint64_t total_req;
1841 size_t nb_reqs =
1842 sizeof(px->fe_counters.p.http.cum_req) / sizeof(*px->fe_counters.p.http.cum_req);
1843
1844 total_req = 0;
1845 for (i = 0; i < nb_reqs; i++)
1846 total_req += px->fe_counters.p.http.cum_req[i];
1847 metric = mkf_u64(FN_COUNTER, total_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001848 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001849 }
William Dauchy0ef54392021-01-17 18:27:45 +01001850 case ST_F_COMP_IN:
1851 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1852 break;
1853 case ST_F_COMP_OUT:
1854 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1855 break;
1856 case ST_F_COMP_BYP:
1857 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1858 break;
1859 case ST_F_COMP_RSP:
1860 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1861 break;
1862 case ST_F_CONN_RATE:
1863 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1864 break;
1865 case ST_F_CONN_RATE_MAX:
1866 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1867 break;
1868 case ST_F_CONN_TOT:
1869 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1870 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001871 case ST_F_SESS_OTHER: {
1872 int i;
1873 uint64_t total_sess;
1874 size_t nb_sess =
1875 sizeof(px->fe_counters.cum_sess_ver) / sizeof(*px->fe_counters.cum_sess_ver);
1876
1877 total_sess = px->fe_counters.cum_sess;
1878 for (i = 0; i < nb_sess; i++)
1879 total_sess -= px->fe_counters.cum_sess_ver[i];
1880 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);
Christopher Fauleta8b76842022-12-16 15:08:36 +01003198 if (!stats_putchk(rep, htx, &trash_chunk))
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)) {
Christopher Fauleta8b76842022-12-16 15:08:36 +01003208 if (!stats_putchk(rep, htx, &trash_chunk))
Christopher Fauletef779222018-10-31 08:47:01 +01003209 goto full;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003210 if (ctx->field)
3211 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003212 }
3213
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003214 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003215 ctx->obj2 = px->conf.listeners.n;
3216 ctx->px_st = STAT_PX_ST_LI;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003217 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003218
3219 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003220 /* obj2 points to listeners list as initialized above */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003221 for (; ctx->obj2 != &px->conf.listeners; ctx->obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003222 if (htx) {
3223 if (htx_almost_full(htx))
3224 goto full;
3225 }
3226 else {
3227 if (buffer_almost_full(&rep->buf))
3228 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003229 }
3230
Willy Tarreau91cefca2022-05-03 17:08:29 +02003231 l = LIST_ELEM(ctx->obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003232 if (!l->counters)
3233 continue;
3234
Willy Tarreau91cefca2022-05-03 17:08:29 +02003235 if (ctx->flags & STAT_BOUND) {
3236 if (!(ctx->type & (1 << STATS_TYPE_SO)))
William Lallemand74c24fb2016-11-21 17:18:36 +01003237 break;
3238
Willy Tarreau91cefca2022-05-03 17:08:29 +02003239 if (ctx->sid != -1 && l->luid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003240 continue;
3241 }
3242
3243 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003244 if (stats_dump_li_stats(sc, px, l)) {
Christopher Fauleta8b76842022-12-16 15:08:36 +01003245 if (!stats_putchk(rep, htx, &trash_chunk))
Christopher Fauletef779222018-10-31 08:47:01 +01003246 goto full;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003247 if (ctx->field)
3248 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003249 }
3250 }
3251
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003252 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003253 ctx->obj2 = px->srv; /* may be NULL */
3254 ctx->px_st = STAT_PX_ST_SV;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003255 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003256
3257 case STAT_PX_ST_SV:
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003258 /* obj2 points to servers list as initialized above.
3259 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003260 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003261 * Temporarily increment its refcount to prevent its
3262 * anticipated cleaning. Call free_server to release it.
3263 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003264 for (; ctx->obj2 != NULL;
3265 ctx->obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003266
Willy Tarreau91cefca2022-05-03 17:08:29 +02003267 sv = ctx->obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003268 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003269
Christopher Fauletef779222018-10-31 08:47:01 +01003270 if (htx) {
3271 if (htx_almost_full(htx))
3272 goto full;
3273 }
3274 else {
3275 if (buffer_almost_full(&rep->buf))
3276 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003277 }
3278
Willy Tarreau91cefca2022-05-03 17:08:29 +02003279 if (ctx->flags & STAT_BOUND) {
3280 if (!(ctx->type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003281 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003282 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003283 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003284
Willy Tarreau91cefca2022-05-03 17:08:29 +02003285 if (ctx->sid != -1 && sv->puid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003286 continue;
3287 }
3288
Willy Tarreau3e320362020-10-23 17:28:57 +02003289 /* do not report disabled servers */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003290 if (ctx->flags & STAT_HIDE_MAINT &&
Willy Tarreau3e320362020-10-23 17:28:57 +02003291 sv->cur_admin & SRV_ADMF_MAINT) {
3292 continue;
3293 }
3294
William Lallemand74c24fb2016-11-21 17:18:36 +01003295 svs = sv;
3296 while (svs->track)
3297 svs = svs->track;
3298
3299 /* do not report servers which are DOWN and not changing state */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003300 if ((ctx->flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003301 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3302 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003303 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3304 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3305 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3306 continue;
3307 }
3308
Willy Tarreaucaff6312022-05-27 10:17:46 +02003309 if (stats_dump_sv_stats(sc, px, sv)) {
Christopher Fauleta8b76842022-12-16 15:08:36 +01003310 if (!stats_putchk(rep, htx, &trash_chunk))
Christopher Fauletef779222018-10-31 08:47:01 +01003311 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003312 }
3313 } /* for sv */
3314
Willy Tarreau91cefca2022-05-03 17:08:29 +02003315 ctx->px_st = STAT_PX_ST_BE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003316 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003317
3318 case STAT_PX_ST_BE:
3319 /* print the backend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003320 if (stats_dump_be_stats(sc, px)) {
Christopher Fauleta8b76842022-12-16 15:08:36 +01003321 if (!stats_putchk(rep, htx, &trash_chunk))
Christopher Fauletef779222018-10-31 08:47:01 +01003322 goto full;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003323 if (ctx->field)
3324 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003325 }
3326
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003327 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003328 ctx->px_st = STAT_PX_ST_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003329 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003330
3331 case STAT_PX_ST_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003332 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003333 stats_dump_html_px_end(sc, px);
Christopher Fauleta8b76842022-12-16 15:08:36 +01003334 if (!stats_putchk(rep, htx, &trash_chunk))
Christopher Fauletef779222018-10-31 08:47:01 +01003335 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003336 }
3337
Willy Tarreau91cefca2022-05-03 17:08:29 +02003338 ctx->px_st = STAT_PX_ST_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003339 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003340
3341 case STAT_PX_ST_FIN:
3342 return 1;
3343
3344 default:
3345 /* unknown state, we should put an abort() here ! */
3346 return 1;
3347 }
Christopher Fauletef779222018-10-31 08:47:01 +01003348
3349 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003350 sc_need_room(sc);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003351 /* restore previous field */
3352 ctx->field = current_field;
Christopher Fauletef779222018-10-31 08:47:01 +01003353 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003354}
3355
Christopher Fauleta8b76842022-12-16 15:08:36 +01003356/* Dumps the HTTP stats head block to the local trash buffer for and uses the
3357 * per-uri parameters <uri>. The caller is responsible for clearing the local
3358 * trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003359 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003360static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003361{
Willy Tarreau91cefca2022-05-03 17:08:29 +02003362 struct show_stat_ctx *ctx = appctx->svcctx;
3363
William Lallemand74c24fb2016-11-21 17:18:36 +01003364 /* WARNING! This must fit in the first buffer !!! */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003365 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003366 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3367 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3368 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003369 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003370 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3371 "<style type=\"text/css\"><!--\n"
3372 "body {"
3373 " font-family: arial, helvetica, sans-serif;"
3374 " font-size: 12px;"
3375 " font-weight: normal;"
3376 " color: black;"
3377 " background: white;"
3378 "}\n"
3379 "th,td {"
3380 " font-size: 10px;"
3381 "}\n"
3382 "h1 {"
3383 " font-size: x-large;"
3384 " margin-bottom: 0.5em;"
3385 "}\n"
3386 "h2 {"
3387 " font-family: helvetica, arial;"
3388 " font-size: x-large;"
3389 " font-weight: bold;"
3390 " font-style: italic;"
3391 " color: #6020a0;"
3392 " margin-top: 0em;"
3393 " margin-bottom: 0em;"
3394 "}\n"
3395 "h3 {"
3396 " font-family: helvetica, arial;"
3397 " font-size: 16px;"
3398 " font-weight: bold;"
3399 " color: #b00040;"
3400 " background: #e8e8d0;"
3401 " margin-top: 0em;"
3402 " margin-bottom: 0em;"
3403 "}\n"
3404 "li {"
3405 " margin-top: 0.25em;"
3406 " margin-right: 2em;"
3407 "}\n"
3408 ".hr {margin-top: 0.25em;"
3409 " border-color: black;"
3410 " border-bottom-style: solid;"
3411 "}\n"
3412 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3413 ".total {background: #20D0D0;color: #ffff80;}\n"
3414 ".frontend {background: #e8e8d0;}\n"
3415 ".socket {background: #d0d0d0;}\n"
3416 ".backend {background: #e8e8d0;}\n"
3417 ".active_down {background: #ff9090;}\n"
3418 ".active_going_up {background: #ffd020;}\n"
3419 ".active_going_down {background: #ffffa0;}\n"
3420 ".active_up {background: #c0ffc0;}\n"
3421 ".active_nolb {background: #20a0ff;}\n"
3422 ".active_draining {background: #20a0FF;}\n"
3423 ".active_no_check {background: #e0e0e0;}\n"
3424 ".backup_down {background: #ff9090;}\n"
3425 ".backup_going_up {background: #ff80ff;}\n"
3426 ".backup_going_down {background: #c060ff;}\n"
3427 ".backup_up {background: #b0d0ff;}\n"
3428 ".backup_nolb {background: #90b0e0;}\n"
3429 ".backup_draining {background: #cc9900;}\n"
3430 ".backup_no_check {background: #e0e0e0;}\n"
3431 ".maintain {background: #c07820;}\n"
3432 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3433 "\n"
3434 "a.px:link {color: #ffff40; text-decoration: none;}"
3435 "a.px:visited {color: #ffff40; text-decoration: none;}"
3436 "a.px:hover {color: #ffffff; text-decoration: none;}"
3437 "a.lfsb:link {color: #000000; text-decoration: none;}"
3438 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3439 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3440 "\n"
3441 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3442 "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"
3443 "table.tbl td.ac { text-align: center;}\n"
3444 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3445 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3446 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3447 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3448 "\n"
3449 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3450 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3451 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3452 "table.det { border-collapse: collapse; border-style: none; }\n"
3453 "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"
3454 "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"
3455 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003456 "div.tips {\n"
3457 " display:block;\n"
3458 " visibility:hidden;\n"
3459 " z-index:2147483647;\n"
3460 " position:absolute;\n"
3461 " padding:2px 4px 3px;\n"
3462 " background:#f0f060; color:#000000;\n"
3463 " border:1px solid #7040c0;\n"
3464 " white-space:nowrap;\n"
3465 " font-style:normal;font-size:11px;font-weight:normal;\n"
3466 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3467 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3468 "}\n"
3469 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003470 "@media (prefers-color-scheme: dark) {\n"
3471 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3472 " h1 { color: #a265e0!important; }\n"
3473 " h2 { color: #a265e0; }\n"
3474 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3475 " a { color: #3391ff; }\n"
3476 " input { background-color: #2f3437; }\n"
3477 " .hr { border-color: #8c8273; }\n"
3478 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3479 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003480 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003481 " .backend {background: #2f3437;}\n"
3482 " .active_down {background: #760000;}\n"
3483 " .active_going_up {background: #b99200;}\n"
3484 " .active_going_down {background: #6c6c00;}\n"
3485 " .active_up {background: #165900;}\n"
3486 " .active_nolb {background: #006ab9;}\n"
3487 " .active_draining {background: #006ab9;}\n"
3488 " .active_no_check {background: #2a2d2f;}\n"
3489 " .backup_down {background: #760000;}\n"
3490 " .backup_going_up {background: #7f007f;}\n"
3491 " .backup_going_down {background: #580092;}\n"
3492 " .backup_up {background: #2e3234;}\n"
3493 " .backup_nolb {background: #1e3c6a;}\n"
3494 " .backup_draining {background: #a37a00;}\n"
3495 " .backup_no_check {background: #2a2d2f;}\n"
3496 " .maintain {background: #9a601a;}\n"
3497 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3498 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3499 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3500 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3501 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3502 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3503 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003504 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003505 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3506 " table.tbl th { border-color: #808080; }\n"
3507 " table.tbl td { border-color: #808080; }\n"
3508 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3509 " div.tips {\n"
3510 " background:#8e8e0d;\n"
3511 " color:#e8e6e3;\n"
3512 " border-color: #4e2c86;\n"
3513 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3514 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3515 " box-shadow: #60686c 2px 2px 3px;\n"
3516 " }\n"
3517 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003518 "-->\n"
3519 "</style></head>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003520 (ctx->flags & STAT_SHNODE) ? " on " : "",
3521 (ctx->flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003522 );
3523}
3524
Christopher Fauleta8b76842022-12-16 15:08:36 +01003525/* Dumps the HTML stats information block to the local trash buffer for and uses
3526 * the state from stream connector <sc> and per-uri parameters <uri>. The caller
3527 * is responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003528 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003529static void stats_dump_html_info(struct stconn *sc, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003530{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003531 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003532 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003533 unsigned int up = (now.tv_sec - start_date.tv_sec);
3534 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02003535 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Willy Tarreau6be8d092023-01-12 16:08:41 +01003536 unsigned long long bps;
3537 int thr;
3538
3539 for (bps = thr = 0; thr < global.nbthread; thr++)
3540 bps += 32ULL * read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
Willy Tarreau1713c032019-05-23 12:23:55 +02003541
3542 /* Turn the bytes per second to bits per second and take care of the
3543 * usual ethernet overhead in order to help figure how far we are from
3544 * interface saturation since it's the only case which usually matters.
3545 * For this we count the total size of an Ethernet frame on the wire
3546 * including preamble and IFG (1538) for the largest TCP segment it
3547 * transports (1448 with TCP timestamps). This is not valid for smaller
3548 * packets (under-estimated), but it gives a reasonably accurate
3549 * estimation of how far we are from uplink saturation.
3550 */
3551 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003552
3553 /* WARNING! this has to fit the first packet too.
3554 * We are around 3.5 kB, add adding entries will
3555 * become tricky if we want to support 4kB buffers !
3556 */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003557 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003558 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3559 PRODUCT_NAME "%s</a></h1>\n"
3560 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3561 "<hr width=\"100%%\" class=\"hr\">\n"
3562 "<h3>&gt; General process information</h3>\n"
3563 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003564 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003565 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3566 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3567 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003568 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003569 "Running tasks: %d/%d; idle = %d %%<br>\n"
3570 "</td><td align=\"center\" nowrap>\n"
3571 "<table class=\"lgd\"><tr>\n"
3572 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3573 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3574 "</tr><tr>\n"
3575 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3576 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3577 "</tr><tr>\n"
3578 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3579 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3580 "</tr><tr>\n"
3581 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3582 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3583 "</tr><tr>\n"
3584 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3585 "</tr><tr>\n"
3586 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3587 "</tr></table>\n"
3588 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3589 "</td>"
3590 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3591 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3592 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003593 (ctx->flags & STAT_HIDEVER) ? "" : (stats_version_string),
3594 pid, (ctx->flags & STAT_SHNODE) ? " on " : "",
3595 (ctx->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3596 (ctx->flags & STAT_SHDESC) ? ": " : "",
3597 (ctx->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003598 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003599 up / 86400, (up % 86400) / 3600,
3600 (up % 3600) / 60, (up % 60),
3601 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3602 global.rlimit_memmax ? " MB" : "",
3603 global.rlimit_nofile,
3604 global.maxsock, global.maxconn, global.maxpipes,
3605 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003606 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3607 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003608 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003609 );
3610
Willy Tarreau91cefca2022-05-03 17:08:29 +02003611 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3612 memcpy(scope_txt, scope_ptr, ctx->scope_len);
3613 scope_txt[ctx->scope_len] = '\0';
William Lallemand74c24fb2016-11-21 17:18:36 +01003614
Christopher Fauleta8b76842022-12-16 15:08:36 +01003615 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003616 "<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 +02003617 (ctx->scope_len > 0) ? scope_txt : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003618 STAT_SCOPE_TXT_MAXLEN);
3619
Willy Tarreau91cefca2022-05-03 17:08:29 +02003620 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003621 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003622 if (ctx->scope_len) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003623 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003624 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3625 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003626 }
3627
Willy Tarreau91cefca2022-05-03 17:08:29 +02003628 if (ctx->flags & STAT_HIDE_DOWN)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003629 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003630 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3631 uri->uri_prefix,
3632 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003633 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003634 scope_txt);
3635 else
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\">Hide 'DOWN' servers</a><br>\n",
3638 uri->uri_prefix,
3639 ";up",
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
3643 if (uri->refresh > 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003644 if (ctx->flags & STAT_NO_REFRESH)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003645 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003646 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3647 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003648 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003649 "",
3650 scope_txt);
3651 else
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\">Disable 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 ";norefresh",
3657 scope_txt);
3658 }
3659
Christopher Fauleta8b76842022-12-16 15:08:36 +01003660 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003661 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3662 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003663 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3664 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003665 scope_txt);
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;csv%s%s\">CSV export</a><br>\n",
3669 uri->uri_prefix,
3670 (uri->refresh > 0) ? ";norefresh" : "",
3671 scope_txt);
3672
Christopher Fauleta8b76842022-12-16 15:08:36 +01003673 chunk_appendf(&trash_chunk,
Christopher Faulet6338a082019-09-09 15:50:54 +02003674 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3675 uri->uri_prefix,
3676 (uri->refresh > 0) ? ";norefresh" : "",
3677 scope_txt, uri->uri_prefix);
3678
Christopher Fauleta8b76842022-12-16 15:08:36 +01003679 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003680 "</ul></td>"
3681 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3682 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3683 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3684 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3685 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3686 "</ul>"
3687 "</td>"
3688 "</tr></table>\n"
3689 ""
3690 );
3691
Willy Tarreau91cefca2022-05-03 17:08:29 +02003692 if (ctx->st_code) {
3693 switch (ctx->st_code) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003694 case STAT_STATUS_DONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003695 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003696 "<p><div class=active_up>"
3697 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3698 "Action processed successfully."
3699 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003700 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3701 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003702 scope_txt);
3703 break;
3704 case STAT_STATUS_NONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003705 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003706 "<p><div class=active_going_down>"
3707 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3708 "Nothing has changed."
3709 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003710 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3711 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003712 scope_txt);
3713 break;
3714 case STAT_STATUS_PART:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003715 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003716 "<p><div class=active_going_down>"
3717 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3718 "Action partially processed.<br>"
3719 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3720 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003721 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3722 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003723 scope_txt);
3724 break;
3725 case STAT_STATUS_ERRP:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003726 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003727 "<p><div class=active_down>"
3728 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3729 "Action not processed because of invalid parameters."
3730 "<ul>"
3731 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003732 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003733 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3734 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3735 "</ul>"
3736 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003737 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3738 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003739 scope_txt);
3740 break;
3741 case STAT_STATUS_EXCD:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003742 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003743 "<p><div class=active_down>"
3744 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3745 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3746 "You should retry with less servers at a time.</b>"
3747 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003748 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3749 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003750 scope_txt);
3751 break;
3752 case STAT_STATUS_DENY:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003753 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003754 "<p><div class=active_down>"
3755 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3756 "<b>Action denied.</b>"
3757 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003758 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3759 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003760 scope_txt);
3761 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003762 case STAT_STATUS_IVAL:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003763 chunk_appendf(&trash_chunk,
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003764 "<p><div class=active_down>"
3765 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3766 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3767 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003768 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3769 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003770 scope_txt);
3771 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003772 default:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003773 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003774 "<p><div class=active_no_check>"
3775 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3776 "Unexpected result."
3777 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003778 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3779 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003780 scope_txt);
3781 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003782 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003783 }
3784}
3785
Christopher Fauleta8b76842022-12-16 15:08:36 +01003786/* Dumps the HTML stats trailer block to the local trash buffer. The caller is
3787 * responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003788 */
3789static void stats_dump_html_end()
3790{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003791 chunk_appendf(&trash_chunk, "</body></html>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003792}
3793
Christopher Fauleta8b76842022-12-16 15:08:36 +01003794/* Dumps the stats JSON header to the local trash buffer buffer which. The
3795 * caller is responsible for clearing it if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003796 */
3797static void stats_dump_json_header()
3798{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003799 chunk_strcat(&trash_chunk, "[");
Simon Horman05ee2132017-01-04 09:37:25 +01003800}
3801
3802
Christopher Fauleta8b76842022-12-16 15:08:36 +01003803/* Dumps the JSON stats trailer block to the local trash buffer. The caller is
3804 * responsible for clearing the local trash buffer if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003805 */
3806static void stats_dump_json_end()
3807{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003808 chunk_strcat(&trash_chunk, "]\n");
Simon Horman05ee2132017-01-04 09:37:25 +01003809}
3810
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003811/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3812 * a pointer to the current server/listener.
3813 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003814static int stats_dump_proxies(struct stconn *sc,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003815 struct htx *htx,
3816 struct uri_auth *uri)
3817{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003818 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003819 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003820 struct channel *rep = sc_ic(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003821 struct proxy *px;
3822
3823 /* dump proxies */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003824 while (ctx->obj1) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003825 if (htx) {
3826 if (htx_almost_full(htx))
3827 goto full;
3828 }
3829 else {
3830 if (buffer_almost_full(&rep->buf))
3831 goto full;
3832 }
3833
Willy Tarreau91cefca2022-05-03 17:08:29 +02003834 px = ctx->obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003835 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003836 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003837 * This change allows retrieving stats from "old" proxies after a reload.
3838 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003839 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003840 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003841 if (stats_dump_proxy_to_buffer(sc, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003842 return 0;
3843 }
3844
Willy Tarreau91cefca2022-05-03 17:08:29 +02003845 ctx->obj1 = px->next;
3846 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003847 ctx->field = 0;
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003848 }
3849
3850 return 1;
3851
3852 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003853 sc_need_room(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003854 return 0;
3855}
3856
Willy Tarreau4596fe22022-05-17 19:07:51 +02003857/* This function dumps statistics onto the stream connector's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003858 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3859 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3860 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3861 * and the stream must be closed, or -1 in case of any error. This function is
3862 * used by both the CLI and the HTTP handlers.
3863 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003864static int stats_dump_stat_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003865 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003866{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003867 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003868 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003869 struct channel *rep = sc_ic(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003870 enum stats_domain domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003871
Christopher Fauleta8b76842022-12-16 15:08:36 +01003872 chunk_reset(&trash_chunk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003873
Willy Tarreau41f88522022-05-03 18:39:27 +02003874 switch (ctx->state) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003875 case STAT_STATE_INIT:
3876 ctx->state = STAT_STATE_HEAD; /* let's start producing data */
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003877 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003878
Willy Tarreau6ef16482022-05-06 18:07:53 +02003879 case STAT_STATE_HEAD:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003880 if (ctx->flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003881 stats_dump_html_head(appctx, uri);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003882 else if (ctx->flags & STAT_JSON_SCHM)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003883 stats_dump_json_schema(&trash_chunk);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003884 else if (ctx->flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003885 stats_dump_json_header();
Willy Tarreau91cefca2022-05-03 17:08:29 +02003886 else if (!(ctx->flags & STAT_FMT_TYPED))
3887 stats_dump_csv_header(ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003888
Christopher Fauleta8b76842022-12-16 15:08:36 +01003889 if (!stats_putchk(rep, htx, &trash_chunk))
Christopher Fauletef779222018-10-31 08:47:01 +01003890 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003891
Willy Tarreau91cefca2022-05-03 17:08:29 +02003892 if (ctx->flags & STAT_JSON_SCHM) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003893 ctx->state = STAT_STATE_FIN;
Christopher Faulet6338a082019-09-09 15:50:54 +02003894 return 1;
3895 }
Willy Tarreau6ef16482022-05-06 18:07:53 +02003896 ctx->state = STAT_STATE_INFO;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003897 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003898
Willy Tarreau6ef16482022-05-06 18:07:53 +02003899 case STAT_STATE_INFO:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003900 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003901 stats_dump_html_info(sc, uri);
Christopher Fauleta8b76842022-12-16 15:08:36 +01003902 if (!stats_putchk(rep, htx, &trash_chunk))
Christopher Fauletef779222018-10-31 08:47:01 +01003903 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003904 }
3905
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003906 if (domain == STATS_DOMAIN_PROXY)
Willy Tarreau91cefca2022-05-03 17:08:29 +02003907 ctx->obj1 = proxies_list;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003908
Willy Tarreau91cefca2022-05-03 17:08:29 +02003909 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003910 ctx->field = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003911 ctx->state = STAT_STATE_LIST;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003912 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003913
Willy Tarreau6ef16482022-05-06 18:07:53 +02003914 case STAT_STATE_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003915 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003916 case STATS_DOMAIN_RESOLVERS:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003917 if (!stats_dump_resolvers(sc, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003918 stat_count[domain],
3919 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003920 return 0;
3921 }
3922 break;
3923
3924 case STATS_DOMAIN_PROXY:
3925 default:
3926 /* dump proxies */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003927 if (!stats_dump_proxies(sc, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003928 return 0;
3929 break;
3930 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003931
Willy Tarreau6ef16482022-05-06 18:07:53 +02003932 ctx->state = STAT_STATE_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003933 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003934
Willy Tarreau6ef16482022-05-06 18:07:53 +02003935 case STAT_STATE_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003936 if (ctx->flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3937 if (ctx->flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01003938 stats_dump_html_end();
3939 else
3940 stats_dump_json_end();
Christopher Fauleta8b76842022-12-16 15:08:36 +01003941 if (!stats_putchk(rep, htx, &trash_chunk))
Christopher Fauletef779222018-10-31 08:47:01 +01003942 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003943 }
3944
Willy Tarreau6ef16482022-05-06 18:07:53 +02003945 ctx->state = STAT_STATE_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003946 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003947
Willy Tarreau6ef16482022-05-06 18:07:53 +02003948 case STAT_STATE_FIN:
William Lallemand74c24fb2016-11-21 17:18:36 +01003949 return 1;
3950
3951 default:
3952 /* unknown state ! */
Willy Tarreau6ef16482022-05-06 18:07:53 +02003953 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01003954 return -1;
3955 }
Christopher Fauletef779222018-10-31 08:47:01 +01003956
3957 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003958 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01003959 return 0;
3960
William Lallemand74c24fb2016-11-21 17:18:36 +01003961}
3962
3963/* We reached the stats page through a POST request. The appctx is
3964 * expected to have already been allocated by the caller.
3965 * Parse the posted data and enable/disable servers if necessary.
3966 * Returns 1 if request was parsed or zero if it needs more data.
3967 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003968static int stats_process_http_post(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01003969{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003970 struct stream *s = __sc_strm(sc);
3971 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003972 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003973
3974 struct proxy *px = NULL;
3975 struct server *sv = NULL;
3976
3977 char key[LINESIZE];
3978 int action = ST_ADM_ACTION_NONE;
3979 int reprocess = 0;
3980
3981 int total_servers = 0;
3982 int altered_servers = 0;
3983
3984 char *first_param, *cur_param, *next_param, *end_params;
3985 char *st_cur_param = NULL;
3986 char *st_next_param = NULL;
3987
Christopher Fauleta3618372018-12-13 21:59:56 +01003988 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01003989
Christopher Fauletb7f88902019-07-15 21:56:43 +02003990 struct htx *htx = htxbuf(&s->req.buf);
3991 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01003992
Christopher Fauletb7f88902019-07-15 21:56:43 +02003993 /* we need more data */
3994 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
3995 /* check if we can receive more */
3996 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003997 ctx->st_code = STAT_STATUS_EXCD;
Christopher Fauletb7f88902019-07-15 21:56:43 +02003998 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01003999 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004000 goto wait;
4001 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004002
Christopher Fauletb7f88902019-07-15 21:56:43 +02004003 /* The request was fully received. Copy data */
4004 blk = htx_get_head_blk(htx);
4005 while (blk) {
4006 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004007
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004008 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02004009 break;
4010 if (type == HTX_BLK_DATA) {
4011 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004012
Christopher Fauletb7f88902019-07-15 21:56:43 +02004013 if (!chunk_memcat(temp, v.ptr, v.len)) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004014 ctx->st_code = STAT_STATUS_EXCD;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004015 goto out;
4016 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004017 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004018 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01004019 }
4020
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004021 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01004022 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01004023 cur_param = next_param = end_params;
4024 *end_params = '\0';
4025
Willy Tarreau91cefca2022-05-03 17:08:29 +02004026 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004027
4028 /*
4029 * Parse the parameters in reverse order to only store the last value.
4030 * From the html form, the backend and the action are at the end.
4031 */
4032 while (cur_param > first_param) {
4033 char *value;
4034 int poffset, plen;
4035
4036 cur_param--;
4037
4038 if ((*cur_param == '&') || (cur_param == first_param)) {
4039 reprocess_servers:
4040 /* Parse the key */
4041 poffset = (cur_param != first_param ? 1 : 0);
4042 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4043 if ((plen > 0) && (plen <= sizeof(key))) {
4044 strncpy(key, cur_param + poffset, plen);
4045 key[plen - 1] = '\0';
4046 } else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004047 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004048 goto out;
4049 }
4050
4051 /* Parse the value */
4052 value = key;
4053 while (*value != '\0' && *value != '=') {
4054 value++;
4055 }
4056 if (*value == '=') {
4057 /* Ok, a value is found, we can mark the end of the key */
4058 *value++ = '\0';
4059 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02004060 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01004061 break;
4062
4063 /* Now we can check the key to see what to do */
4064 if (!px && (strcmp(key, "b") == 0)) {
4065 if ((px = proxy_be_by_name(value)) == NULL) {
4066 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004067 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004068 goto out;
4069 }
4070 }
4071 else if (!action && (strcmp(key, "action") == 0)) {
4072 if (strcmp(value, "ready") == 0) {
4073 action = ST_ADM_ACTION_READY;
4074 }
4075 else if (strcmp(value, "drain") == 0) {
4076 action = ST_ADM_ACTION_DRAIN;
4077 }
4078 else if (strcmp(value, "maint") == 0) {
4079 action = ST_ADM_ACTION_MAINT;
4080 }
4081 else if (strcmp(value, "shutdown") == 0) {
4082 action = ST_ADM_ACTION_SHUTDOWN;
4083 }
4084 else if (strcmp(value, "dhlth") == 0) {
4085 action = ST_ADM_ACTION_DHLTH;
4086 }
4087 else if (strcmp(value, "ehlth") == 0) {
4088 action = ST_ADM_ACTION_EHLTH;
4089 }
4090 else if (strcmp(value, "hrunn") == 0) {
4091 action = ST_ADM_ACTION_HRUNN;
4092 }
4093 else if (strcmp(value, "hnolb") == 0) {
4094 action = ST_ADM_ACTION_HNOLB;
4095 }
4096 else if (strcmp(value, "hdown") == 0) {
4097 action = ST_ADM_ACTION_HDOWN;
4098 }
4099 else if (strcmp(value, "dagent") == 0) {
4100 action = ST_ADM_ACTION_DAGENT;
4101 }
4102 else if (strcmp(value, "eagent") == 0) {
4103 action = ST_ADM_ACTION_EAGENT;
4104 }
4105 else if (strcmp(value, "arunn") == 0) {
4106 action = ST_ADM_ACTION_ARUNN;
4107 }
4108 else if (strcmp(value, "adown") == 0) {
4109 action = ST_ADM_ACTION_ADOWN;
4110 }
4111 /* else these are the old supported methods */
4112 else if (strcmp(value, "disable") == 0) {
4113 action = ST_ADM_ACTION_DISABLE;
4114 }
4115 else if (strcmp(value, "enable") == 0) {
4116 action = ST_ADM_ACTION_ENABLE;
4117 }
4118 else if (strcmp(value, "stop") == 0) {
4119 action = ST_ADM_ACTION_STOP;
4120 }
4121 else if (strcmp(value, "start") == 0) {
4122 action = ST_ADM_ACTION_START;
4123 }
4124 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004125 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004126 goto out;
4127 }
4128 }
4129 else if (strcmp(key, "s") == 0) {
4130 if (!(px && action)) {
4131 /*
4132 * Indicates that we'll need to reprocess the parameters
4133 * as soon as backend and action are known
4134 */
4135 if (!reprocess) {
4136 st_cur_param = cur_param;
4137 st_next_param = next_param;
4138 }
4139 reprocess = 1;
4140 }
4141 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004142 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004143 switch (action) {
4144 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004145 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004146 altered_servers++;
4147 total_servers++;
4148 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
4149 }
4150 break;
4151 case ST_ADM_ACTION_ENABLE:
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_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4156 }
4157 break;
4158 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004159 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004160 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
4161 altered_servers++;
4162 total_servers++;
4163 }
4164 break;
4165 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004166 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004167 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4168 altered_servers++;
4169 total_servers++;
4170 }
4171 break;
4172 case ST_ADM_ACTION_DHLTH:
4173 if (sv->check.state & CHK_ST_CONFIGURED) {
4174 sv->check.state &= ~CHK_ST_ENABLED;
4175 altered_servers++;
4176 total_servers++;
4177 }
4178 break;
4179 case ST_ADM_ACTION_EHLTH:
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_HRUNN:
4187 if (!(sv->track)) {
4188 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004189 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004190 altered_servers++;
4191 total_servers++;
4192 }
4193 break;
4194 case ST_ADM_ACTION_HNOLB:
4195 if (!(sv->track)) {
4196 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004197 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004198 altered_servers++;
4199 total_servers++;
4200 }
4201 break;
4202 case ST_ADM_ACTION_HDOWN:
4203 if (!(sv->track)) {
4204 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004205 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004206 altered_servers++;
4207 total_servers++;
4208 }
4209 break;
4210 case ST_ADM_ACTION_DAGENT:
4211 if (sv->agent.state & CHK_ST_CONFIGURED) {
4212 sv->agent.state &= ~CHK_ST_ENABLED;
4213 altered_servers++;
4214 total_servers++;
4215 }
4216 break;
4217 case ST_ADM_ACTION_EAGENT:
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_ARUNN:
4225 if (sv->agent.state & CHK_ST_ENABLED) {
4226 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004227 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004228 altered_servers++;
4229 total_servers++;
4230 }
4231 break;
4232 case ST_ADM_ACTION_ADOWN:
4233 if (sv->agent.state & CHK_ST_ENABLED) {
4234 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004235 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004236 altered_servers++;
4237 total_servers++;
4238 }
4239 break;
4240 case ST_ADM_ACTION_READY:
4241 srv_adm_set_ready(sv);
4242 altered_servers++;
4243 total_servers++;
4244 break;
4245 case ST_ADM_ACTION_DRAIN:
4246 srv_adm_set_drain(sv);
4247 altered_servers++;
4248 total_servers++;
4249 break;
4250 case ST_ADM_ACTION_MAINT:
4251 srv_adm_set_maint(sv);
4252 altered_servers++;
4253 total_servers++;
4254 break;
4255 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004256 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004257 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004258 altered_servers++;
4259 total_servers++;
4260 }
4261 break;
4262 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004263 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004264 } else {
4265 /* the server name is unknown or ambiguous (duplicate names) */
4266 total_servers++;
4267 }
4268 }
4269 if (reprocess && px && action) {
4270 /* Now, we know the backend and the action chosen by the user.
4271 * We can safely restart from the first server parameter
4272 * to reprocess them
4273 */
4274 cur_param = st_cur_param;
4275 next_param = st_next_param;
4276 reprocess = 0;
4277 goto reprocess_servers;
4278 }
4279
4280 next_param = cur_param;
4281 }
4282 }
4283
4284 if (total_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004285 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004286 }
4287 else if (altered_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004288 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004289 }
4290 else if (altered_servers == total_servers) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004291 ctx->st_code = STAT_STATUS_DONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004292 }
4293 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004294 ctx->st_code = STAT_STATUS_PART;
William Lallemand74c24fb2016-11-21 17:18:36 +01004295 }
4296 out:
4297 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004298 wait:
Willy Tarreau91cefca2022-05-03 17:08:29 +02004299 ctx->st_code = STAT_STATUS_NONE;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004300 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004301}
4302
4303
Willy Tarreaucaff6312022-05-27 10:17:46 +02004304static int stats_send_http_headers(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004305{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004306 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004307 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004308 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004309 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004310 struct htx_sl *sl;
4311 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004312
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004313 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);
4314 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4315 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004316 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004317 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004318
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004319 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004320 goto full;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004321 if (ctx->flags & STAT_FMT_HTML) {
Christopher Fauletef779222018-10-31 08:47:01 +01004322 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4323 goto full;
4324 }
Willy Tarreau91cefca2022-05-03 17:08:29 +02004325 else if (ctx->flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004326 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4327 goto full;
4328 }
Christopher Fauletef779222018-10-31 08:47:01 +01004329 else {
4330 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4331 goto full;
4332 }
4333
Willy Tarreau91cefca2022-05-03 17:08:29 +02004334 if (uri->refresh > 0 && !(ctx->flags & STAT_NO_REFRESH)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004335 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004336 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004337 goto full;
4338 }
4339
Willy Tarreau91cefca2022-05-03 17:08:29 +02004340 if (ctx->flags & STAT_CHUNKED) {
Christopher Fauletef779222018-10-31 08:47:01 +01004341 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4342 goto full;
4343 }
4344
4345 if (!htx_add_endof(htx, HTX_BLK_EOH))
4346 goto full;
4347
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004348 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004349 return 1;
4350
4351 full:
4352 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004353 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004354 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004355}
4356
Christopher Fauletef779222018-10-31 08:47:01 +01004357
Willy Tarreaucaff6312022-05-27 10:17:46 +02004358static int stats_send_http_redirect(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004359{
4360 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02004361 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004362 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004363 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004364 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004365 struct htx_sl *sl;
4366 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004367
Willy Tarreau91cefca2022-05-03 17:08:29 +02004368 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauletef779222018-10-31 08:47:01 +01004369 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004370 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004371 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004372
Christopher Fauletef779222018-10-31 08:47:01 +01004373 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004374 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
4375 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004376 }
4377
4378 /* We don't want to land on the posted stats page because a refresh will
4379 * repost the data. We don't want this to happen on accident so we redirect
4380 * the browse to the stats page with a GET.
4381 */
4382 chunk_printf(&trash, "%s;st=%s%s%s%s",
4383 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02004384 ((ctx->st_code > STAT_STATUS_INIT) &&
4385 (ctx->st_code < STAT_STATUS_SIZE) &&
4386 stat_status_codes[ctx->st_code]) ?
4387 stat_status_codes[ctx->st_code] :
Christopher Fauletef779222018-10-31 08:47:01 +01004388 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau91cefca2022-05-03 17:08:29 +02004389 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
4390 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Fauletef779222018-10-31 08:47:01 +01004391 scope_txt);
4392
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004393 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4394 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4395 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004396 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004397 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004398
4399 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004400 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4401 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4402 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4403 goto full;
4404
4405 if (!htx_add_endof(htx, HTX_BLK_EOH))
4406 goto full;
4407
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004408 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004409 return 1;
4410
4411full:
4412 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004413 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004414 return 0;
4415}
William Lallemand74c24fb2016-11-21 17:18:36 +01004416
Simon Horman05ee2132017-01-04 09:37:25 +01004417
Willy Tarreau4596fe22022-05-17 19:07:51 +02004418/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004419 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4420 * appctx->st0 contains the operation in progress (dump, done). The handler
4421 * automatically unregisters itself once transfer is complete.
4422 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004423static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004424{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004425 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02004426 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004427 struct stream *s = __sc_strm(sc);
4428 struct channel *req = sc_oc(sc);
4429 struct channel *res = sc_ic(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004430 struct htx *req_htx, *res_htx;
4431
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004432 /* only proxy stats are available via http */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004433 ctx->domain = STATS_DOMAIN_PROXY;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004434
Christopher Fauletef779222018-10-31 08:47:01 +01004435 res_htx = htx_from_buf(&res->buf);
4436
Willy Tarreaucaff6312022-05-27 10:17:46 +02004437 if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO))
Christopher Fauletef779222018-10-31 08:47:01 +01004438 goto out;
4439
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004440 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004441 if (!b_size(&res->buf)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004442 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004443 goto out;
4444 }
4445
4446 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004447 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
4448 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004449
4450 /* all states are processed in sequence */
4451 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004452 if (stats_send_http_headers(sc, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004453 if (s->txn->meth == HTTP_METH_HEAD)
4454 appctx->st0 = STAT_HTTP_DONE;
4455 else
4456 appctx->st0 = STAT_HTTP_DUMP;
4457 }
4458 }
4459
4460 if (appctx->st0 == STAT_HTTP_DUMP) {
Aurelien DARRAGON5e7ecbe2023-02-02 15:03:12 +01004461 trash_chunk = b_make(trash.area, trash.size, 0, 0);
4462 /* adjust buffer size to take htx overhead into account,
4463 * make sure to perform this call on an empty buffer
4464 */
4465 trash_chunk.size = buf_room_for_htx_data(&trash_chunk);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004466 if (stats_dump_stat_to_buffer(sc, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004467 appctx->st0 = STAT_HTTP_DONE;
4468 }
4469
4470 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004471 if (stats_process_http_post(sc))
Christopher Fauletef779222018-10-31 08:47:01 +01004472 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004473 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004474 appctx->st0 = STAT_HTTP_DONE;
4475 }
4476
4477 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004478 if (stats_send_http_redirect(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004479 appctx->st0 = STAT_HTTP_DONE;
4480 }
4481
4482 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004483 /* no more data are expected. If the response buffer is empty,
4484 * be sure to add something (EOT block in this case) to have
4485 * something to send. It is important to be sure the EOM flags
4486 * will be handled by the endpoint.
4487 */
4488 if (htx_is_empty(res_htx)) {
4489 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004490 sc_need_room(sc);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004491 goto out;
4492 }
4493 channel_add_input(res, 1);
4494 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004495 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet3fa5d192022-03-07 15:52:42 +01004496 res->flags |= CF_EOI;
Willy Tarreaud869e132022-05-17 18:05:31 +02004497 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004498 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004499 }
4500
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004501 if (appctx->st0 == STAT_HTTP_END) {
4502 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet6e1bbc42022-12-12 08:08:15 +01004503 res->flags |= CF_READ_EVENT;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004504 sc_shutr(sc);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004505 }
4506
4507 /* eat the whole request */
4508 if (co_data(req)) {
4509 req_htx = htx_from_buf(&req->buf);
4510 co_htx_skip(req, req_htx, co_data(req));
4511 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01004512 }
4513 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004514
Christopher Fauletef779222018-10-31 08:47:01 +01004515 out:
4516 /* we have left the request in the buffer for the case where we
4517 * process a POST, and this automatically re-enables activity on
4518 * read. It's better to indicate that we want to stop reading when
4519 * we're sending, so that we know there's at most one direction
4520 * deciding to wake the applet up. It saves it from looping when
4521 * emitting large blocks into small TCP windows.
4522 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004523 htx_to_buf(res_htx, &res->buf);
4524 if (!channel_is_empty(res))
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02004525 applet_wont_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004526}
4527
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004528/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004529static int stats_dump_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004530 const struct field *info,
4531 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004532{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004533 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004534 int field;
4535
4536 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4537 if (!field_format(info, field))
4538 continue;
4539
Willy Tarreaueaa55372019-10-09 07:39:11 +02004540 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004541 return 0;
4542 if (!stats_emit_raw_data_field(out, &info[field]))
4543 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004544 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4545 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004546 if (!chunk_strcat(out, "\n"))
4547 return 0;
4548 }
4549 return 1;
4550}
4551
4552/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004553static int stats_dump_typed_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004554 const struct field *info,
4555 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004556{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004557 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004558 int field;
4559
4560 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4561 if (!field_format(info, field))
4562 continue;
4563
Willy Tarreaueaa55372019-10-09 07:39:11 +02004564 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 +01004565 return 0;
4566 if (!stats_emit_field_tags(out, &info[field], ':'))
4567 return 0;
4568 if (!stats_emit_typed_data_field(out, &info[field]))
4569 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004570 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4571 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004572 if (!chunk_strcat(out, "\n"))
4573 return 0;
4574 }
4575 return 1;
4576}
4577
Willy Tarreau0b26b382021-05-08 07:43:53 +02004578/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4579 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4580 * less then this value, the function returns 0, otherwise, it returns 1. Some
4581 * fields' presence or precision may depend on some of the STAT_* flags present
4582 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004583 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004584int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004585{
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004586 struct timeval up;
Willy Tarreau83061a82018-07-13 11:56:34 +02004587 struct buffer *out = get_trash_chunk();
Willy Tarreau6be8d092023-01-12 16:08:41 +01004588 uint64_t glob_out_bytes, glob_spl_bytes, glob_out_b32;
4589 int thr;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004590
4591#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004592 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4593 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4594 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004595
Willy Tarreauc5977722021-05-08 08:14:04 +02004596 if (ssl_key_rate < ssl_sess_rate)
4597 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004598#endif
4599
Willy Tarreau6be8d092023-01-12 16:08:41 +01004600 /* sum certain per-thread totals (mostly byte counts) */
4601 glob_out_bytes = glob_spl_bytes = glob_out_b32 = 0;
4602 for (thr = 0; thr < global.nbthread; thr++) {
4603 glob_out_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].out_bytes);
4604 glob_spl_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].spliced_out_bytes);
4605 glob_out_b32 += read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
4606 }
4607 glob_out_b32 *= 32; // values are 32-byte units
4608
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004609 tv_remain(&start_date, &now, &up);
4610
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004611 if (len < INF_TOTAL_FIELDS)
4612 return 0;
4613
4614 chunk_reset(out);
4615 memset(info, 0, sizeof(*info) * len);
4616
4617 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004618 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004619 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004620 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004621
Yves Lafon95317282018-02-26 11:10:37 +01004622 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004623 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004624 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004625 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4626
4627 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004628 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 +01004629
Willy Tarreaue8abc322021-05-08 07:56:56 +02004630 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);
4631 info[INF_START_TIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, start_date.tv_sec + start_date.tv_usec / 1000000.0) : mkf_u32(FN_DURATION, start_date.tv_sec);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004632 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004633 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004634 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004635 info[INF_POOL_ALLOC_BYTES] = mkf_u64(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004636 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004637 info[INF_POOL_USED_BYTES] = mkf_u64(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004638 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4639 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4640 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4641 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4642 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4643 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4644 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4645 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4646#ifdef USE_OPENSSL
4647 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004648 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4649 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004650#endif
4651 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4652 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4653 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004654 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 +01004655 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4656 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004657 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 +01004658 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4659 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4660
4661#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004662 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 +01004663 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4664 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004665 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 +01004666 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004667 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4668 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 +01004669 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4670 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4671 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4672#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004673 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));
4674 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 +01004675 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4676#ifdef USE_ZLIB
4677 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4678 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4679#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004680 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004681 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004682 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004683 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4684 if (global.desc)
4685 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004686 info[INF_STOPPING] = mkf_u32(0, stopping);
4687 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004688 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004689 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004690 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004691 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004692 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004693 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004694 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau6be8d092023-01-12 16:08:41 +01004695 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, glob_out_bytes);
4696 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, glob_spl_bytes);
4697 info[INF_BYTES_OUT_RATE] = mkf_u64(FN_RATE, glob_out_b32);
Willy Tarreau9b013702019-10-24 18:18:02 +02004698 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004699 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004700
4701 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4702 chunk_appendf(out, "%#x", get_tainted());
4703
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004704 return 1;
4705}
4706
Willy Tarreau4596fe22022-05-17 19:07:51 +02004707/* This function dumps information onto the stream connector's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004708 * It returns 0 as long as it does not complete, non-zero upon completion.
4709 * No state is used.
4710 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004711static int stats_dump_info_to_buffer(struct stconn *sc)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004712{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004713 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004714 struct show_stat_ctx *ctx = appctx->svcctx;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004715 int ret;
4716 int current_field;
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004717
Willy Tarreau91cefca2022-05-03 17:08:29 +02004718 if (!stats_fill_info(info, INF_TOTAL_FIELDS, ctx->flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004719 return 0;
4720
Christopher Fauleta8b76842022-12-16 15:08:36 +01004721 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004722more:
4723 current_field = ctx->field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004724
Willy Tarreau91cefca2022-05-03 17:08:29 +02004725 if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004726 ret = stats_dump_typed_info_fields(&trash_chunk, info, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004727 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004728 ret = stats_dump_json_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004729 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01004730 ret = stats_dump_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004731
Christopher Fauleta8b76842022-12-16 15:08:36 +01004732 if (applet_putchk(appctx, &trash_chunk) == -1) {
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004733 /* restore previous field */
4734 ctx->field = current_field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004735 return 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004736 }
4737 if (ret && ctx->field) {
4738 /* partial dump */
4739 goto more;
4740 }
4741 ctx->field = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004742 return 1;
4743}
4744
Willy Tarreau4596fe22022-05-17 19:07:51 +02004745/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004746 * It returns 0 as long as it does not complete, non-zero upon completion.
4747 * No state is used.
4748 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004749 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004750 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4751 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004752static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004753{
4754
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004755 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004756
4757 chunk_strcat(out,
4758 "{"
4759 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4760 "\"oneOf\":["
4761 "{"
4762 "\"title\":\"Info\","
4763 "\"type\":\"array\","
4764 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004765 "\"title\":\"InfoItem\","
4766 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004767 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004768 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4769 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4770 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4771 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4772 "},"
4773 "\"required\":[\"field\",\"processNum\",\"tags\","
4774 "\"value\"]"
4775 "}"
4776 "},"
4777 "{"
4778 "\"title\":\"Stat\","
4779 "\"type\":\"array\","
4780 "\"items\":{"
4781 "\"title\":\"InfoItem\","
4782 "\"type\":\"object\","
4783 "\"properties\":{"
4784 "\"objType\":{"
4785 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4786 "\"Server\",\"Unknown\"]"
4787 "},"
4788 "\"proxyId\":{"
4789 "\"type\":\"integer\","
4790 "\"minimum\":0"
4791 "},"
4792 "\"id\":{"
4793 "\"type\":\"integer\","
4794 "\"minimum\":0"
4795 "},"
4796 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4797 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4798 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4799 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4800 "},"
4801 "\"required\":[\"objType\",\"proxyId\",\"id\","
4802 "\"field\",\"processNum\",\"tags\","
4803 "\"value\"]"
4804 "}"
4805 "},"
4806 "{"
4807 "\"title\":\"Error\","
4808 "\"type\":\"object\","
4809 "\"properties\":{"
4810 "\"errorStr\":{"
4811 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004812 "}"
4813 "},"
4814 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004815 "}"
4816 "],"
4817 "\"definitions\":{"
4818 "\"field\":{"
4819 "\"type\":\"object\","
4820 "\"pos\":{"
4821 "\"type\":\"integer\","
4822 "\"minimum\":0"
4823 "},"
4824 "\"name\":{"
4825 "\"type\":\"string\""
4826 "},"
4827 "\"required\":[\"pos\",\"name\"]"
4828 "},"
4829 "\"processNum\":{"
4830 "\"type\":\"integer\","
4831 "\"minimum\":1"
4832 "},"
4833 "\"tags\":{"
4834 "\"type\":\"object\","
4835 "\"origin\":{"
4836 "\"type\":\"string\","
4837 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4838 "\"Config\",\"Product\",\"Unknown\"]"
4839 "},"
4840 "\"nature\":{"
4841 "\"type\":\"string\","
4842 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4843 "\"Rate\",\"Counter\",\"Duration\","
4844 "\"Age\",\"Time\",\"Name\",\"Output\","
4845 "\"Avg\", \"Unknown\"]"
4846 "},"
4847 "\"scope\":{"
4848 "\"type\":\"string\","
4849 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4850 "\"System\",\"Unknown\"]"
4851 "},"
4852 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4853 "},"
4854 "\"typedValue\":{"
4855 "\"type\":\"object\","
4856 "\"oneOf\":["
4857 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4858 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4859 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4860 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4861 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4862 "],"
4863 "\"definitions\":{"
4864 "\"s32Value\":{"
4865 "\"properties\":{"
4866 "\"type\":{"
4867 "\"type\":\"string\","
4868 "\"enum\":[\"s32\"]"
4869 "},"
4870 "\"value\":{"
4871 "\"type\":\"integer\","
4872 "\"minimum\":-2147483648,"
4873 "\"maximum\":2147483647"
4874 "}"
4875 "},"
4876 "\"required\":[\"type\",\"value\"]"
4877 "},"
4878 "\"s64Value\":{"
4879 "\"properties\":{"
4880 "\"type\":{"
4881 "\"type\":\"string\","
4882 "\"enum\":[\"s64\"]"
4883 "},"
4884 "\"value\":{"
4885 "\"type\":\"integer\","
4886 "\"minimum\":-9007199254740991,"
4887 "\"maximum\":9007199254740991"
4888 "}"
4889 "},"
4890 "\"required\":[\"type\",\"value\"]"
4891 "},"
4892 "\"u32Value\":{"
4893 "\"properties\":{"
4894 "\"type\":{"
4895 "\"type\":\"string\","
4896 "\"enum\":[\"u32\"]"
4897 "},"
4898 "\"value\":{"
4899 "\"type\":\"integer\","
4900 "\"minimum\":0,"
4901 "\"maximum\":4294967295"
4902 "}"
4903 "},"
4904 "\"required\":[\"type\",\"value\"]"
4905 "},"
4906 "\"u64Value\":{"
4907 "\"properties\":{"
4908 "\"type\":{"
4909 "\"type\":\"string\","
4910 "\"enum\":[\"u64\"]"
4911 "},"
4912 "\"value\":{"
4913 "\"type\":\"integer\","
4914 "\"minimum\":0,"
4915 "\"maximum\":9007199254740991"
4916 "}"
4917 "},"
4918 "\"required\":[\"type\",\"value\"]"
4919 "},"
4920 "\"strValue\":{"
4921 "\"properties\":{"
4922 "\"type\":{"
4923 "\"type\":\"string\","
4924 "\"enum\":[\"str\"]"
4925 "},"
4926 "\"value\":{\"type\":\"string\"}"
4927 "},"
4928 "\"required\":[\"type\",\"value\"]"
4929 "},"
4930 "\"unknownValue\":{"
4931 "\"properties\":{"
4932 "\"type\":{"
4933 "\"type\":\"integer\","
4934 "\"minimum\":0"
4935 "},"
4936 "\"value\":{"
4937 "\"type\":\"string\","
4938 "\"enum\":[\"unknown\"]"
4939 "}"
4940 "},"
4941 "\"required\":[\"type\",\"value\"]"
4942 "}"
4943 "}"
4944 "}"
4945 "}"
4946 "}");
4947
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004948 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004949 chunk_reset(out);
4950 chunk_appendf(out,
4951 "{\"errorStr\":\"output buffer too short\"}");
4952 }
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02004953 chunk_appendf(out, "\n");
Simon Horman6f6bb382017-01-04 09:37:26 +01004954}
4955
Willy Tarreau4596fe22022-05-17 19:07:51 +02004956/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004957 * It returns 0 as long as it does not complete, non-zero upon completion.
4958 * No state is used.
4959 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004960static int stats_dump_json_schema_to_buffer(struct appctx *appctx)
Simon Horman6f6bb382017-01-04 09:37:26 +01004961{
Simon Horman6f6bb382017-01-04 09:37:26 +01004962
Christopher Fauleta8b76842022-12-16 15:08:36 +01004963 chunk_reset(&trash_chunk);
Simon Horman6f6bb382017-01-04 09:37:26 +01004964
Christopher Fauleta8b76842022-12-16 15:08:36 +01004965 stats_dump_json_schema(&trash_chunk);
4966
4967 if (applet_putchk(appctx, &trash_chunk) == -1)
Simon Horman6f6bb382017-01-04 09:37:26 +01004968 return 0;
Simon Horman6f6bb382017-01-04 09:37:26 +01004969
4970 return 1;
4971}
4972
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004973static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004974{
4975 struct proxy *px;
4976 struct server *sv;
4977 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004978 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004979 int clrall = 0;
4980
4981 if (strcmp(args[2], "all") == 0)
4982 clrall = 1;
4983
4984 /* check permissions */
4985 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4986 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4987 return 1;
4988
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004989 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004990 if (clrall) {
4991 memset(&px->be_counters, 0, sizeof(px->be_counters));
4992 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4993 }
4994 else {
4995 px->be_counters.conn_max = 0;
4996 px->be_counters.p.http.rps_max = 0;
4997 px->be_counters.sps_max = 0;
4998 px->be_counters.cps_max = 0;
4999 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005000 px->be_counters.qtime_max = 0;
5001 px->be_counters.ctime_max = 0;
5002 px->be_counters.dtime_max = 0;
5003 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005004
5005 px->fe_counters.conn_max = 0;
5006 px->fe_counters.p.http.rps_max = 0;
5007 px->fe_counters.sps_max = 0;
5008 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005009 }
5010
5011 for (sv = px->srv; sv; sv = sv->next)
5012 if (clrall)
5013 memset(&sv->counters, 0, sizeof(sv->counters));
5014 else {
5015 sv->counters.cur_sess_max = 0;
5016 sv->counters.nbpend_max = 0;
5017 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005018 sv->counters.qtime_max = 0;
5019 sv->counters.ctime_max = 0;
5020 sv->counters.dtime_max = 0;
5021 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005022 }
5023
5024 list_for_each_entry(li, &px->conf.listeners, by_fe)
5025 if (li->counters) {
5026 if (clrall)
5027 memset(li->counters, 0, sizeof(*li->counters));
5028 else
5029 li->counters->conn_max = 0;
5030 }
5031 }
5032
5033 global.cps_max = 0;
5034 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02005035 global.ssl_max = 0;
5036 global.ssl_fe_keys_max = 0;
5037 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005038
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005039 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5040 if (!mod->clearable && !clrall)
5041 continue;
5042
5043 for (px = proxies_list; px; px = px->next) {
5044 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
5045
5046 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
5047 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
5048 mod,
5049 mod->counters,
5050 mod->counters_size);
5051 }
5052
5053 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
5054 EXTRA_COUNTERS_INIT(px->extra_counters_be,
5055 mod,
5056 mod->counters,
5057 mod->counters_size);
5058 }
5059
5060 if (mod_cap & STATS_PX_CAP_SRV) {
5061 for (sv = px->srv; sv; sv = sv->next) {
5062 EXTRA_COUNTERS_INIT(sv->extra_counters,
5063 mod,
5064 mod->counters,
5065 mod->counters_size);
5066 }
5067 }
5068
5069 if (mod_cap & STATS_PX_CAP_LI) {
5070 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5071 EXTRA_COUNTERS_INIT(li->extra_counters,
5072 mod,
5073 mod->counters,
5074 mod->counters_size);
5075 }
5076 }
5077 }
5078 }
5079
Emeric Brunf8642ee2021-10-29 17:59:18 +02005080 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005081
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005082 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01005083 return 1;
5084}
5085
5086
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005087static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005088{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005089 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005090 int arg = 2;
5091
Willy Tarreau91cefca2022-05-03 17:08:29 +02005092 ctx->scope_str = 0;
5093 ctx->scope_len = 0;
5094 ctx->flags = 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01005095 ctx->field = 0; /* explicit default value */
Willy Tarreaud25fc792016-12-16 12:33:47 +01005096
Willy Tarreau2f397382019-10-09 11:43:59 +02005097 while (*args[arg]) {
5098 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005099 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005100 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005101 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005102 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005103 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02005104 else if (strcmp(args[arg], "float") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005105 ctx->flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02005106 arg++;
5107 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005108 return 0;
5109}
5110
5111
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005112static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01005113{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005114 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005115 int arg = 2;
5116
Willy Tarreau91cefca2022-05-03 17:08:29 +02005117 ctx->scope_str = 0;
5118 ctx->scope_len = 0;
5119 ctx->flags = STAT_SHNODE | STAT_SHDESC;
Willy Tarreau578d6e42019-10-09 11:00:22 +02005120
Willy Tarreau0698c802022-05-11 14:09:57 +02005121 if ((strm_li(appctx_strm(appctx))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005122 ctx->flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01005123
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005124 /* proxy is the default domain */
Willy Tarreau91cefca2022-05-03 17:08:29 +02005125 ctx->domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005126 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005127 ++args;
5128
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005129 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005130 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005131 } else if (strcmp(args[arg], "resolvers") == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02005132 ctx->domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005133 ++args;
5134 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005135 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005136 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005137 }
5138
Willy Tarreau91cefca2022-05-03 17:08:29 +02005139 if (ctx->domain == STATS_DOMAIN_PROXY
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005140 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005141 struct proxy *px;
5142
Willy Tarreau2f397382019-10-09 11:43:59 +02005143 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005144 if (px)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005145 ctx->iid = px->uuid;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005146 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02005147 ctx->iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005148
Willy Tarreau91cefca2022-05-03 17:08:29 +02005149 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02005150 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005151
Willy Tarreau91cefca2022-05-03 17:08:29 +02005152 ctx->flags |= STAT_BOUND;
5153 ctx->type = atoi(args[arg+1]);
5154 ctx->sid = atoi(args[arg+2]);
Willy Tarreau2f397382019-10-09 11:43:59 +02005155 arg += 3;
5156 }
5157
5158 while (*args[arg]) {
5159 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005160 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005161 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005162 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005163 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005164 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02005165 else if (strcmp(args[arg], "no-maint") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005166 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02005167 else if (strcmp(args[arg], "up") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005168 ctx->flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02005169 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01005170 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01005171
Willy Tarreau2b812e22016-11-22 16:18:05 +01005172 return 0;
5173}
5174
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005175static int cli_io_handler_dump_info(struct appctx *appctx)
5176{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005177 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005178 return stats_dump_info_to_buffer(appctx_sc(appctx));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005179}
5180
Willy Tarreau4596fe22022-05-17 19:07:51 +02005181/* This I/O handler runs as an applet embedded in a stream connector. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01005182 * used to send raw stats over a socket.
5183 */
5184static int cli_io_handler_dump_stat(struct appctx *appctx)
5185{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005186 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005187 return stats_dump_stat_to_buffer(appctx_sc(appctx), NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01005188}
5189
Simon Horman6f6bb382017-01-04 09:37:26 +01005190static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5191{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005192 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005193 return stats_dump_json_schema_to_buffer(appctx);
Simon Horman6f6bb382017-01-04 09:37:26 +01005194}
5195
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005196int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5197 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005198{
5199 struct stats_module *mod;
5200
5201 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5202
5203 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5204 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5205 continue;
5206
5207 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5208 }
5209
5210 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5211
5212 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5213 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5214 continue;
5215
5216 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5217 }
5218
5219 return 1;
5220
5221 alloc_failed:
5222 return 0;
5223}
5224
5225/* Initialize and allocate all extra counters for a proxy and its attached
5226 * servers/listeners with all already registered stats module
5227 */
5228int stats_allocate_proxy_counters(struct proxy *px)
5229{
5230 struct server *sv;
5231 struct listener *li;
5232
5233 if (px->cap & PR_CAP_FE) {
5234 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5235 COUNTERS_FE,
5236 STATS_PX_CAP_FE)) {
5237 return 0;
5238 }
5239 }
5240
5241 if (px->cap & PR_CAP_BE) {
5242 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5243 COUNTERS_BE,
5244 STATS_PX_CAP_BE)) {
5245 return 0;
5246 }
5247 }
5248
5249 for (sv = px->srv; sv; sv = sv->next) {
5250 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5251 COUNTERS_SV,
5252 STATS_PX_CAP_SRV)) {
5253 return 0;
5254 }
5255 }
5256
5257 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5258 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5259 COUNTERS_LI,
5260 STATS_PX_CAP_LI)) {
5261 return 0;
5262 }
5263 }
5264
5265 return 1;
5266}
5267
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005268void stats_register_module(struct stats_module *m)
5269{
5270 const uint8_t domain = stats_get_domain(m->domain_flags);
5271
Willy Tarreau2b718102021-04-21 07:32:39 +02005272 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005273 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005274}
5275
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005276static int allocate_stats_px_postcheck(void)
5277{
5278 struct stats_module *mod;
5279 size_t i = ST_F_TOTAL_FIELDS;
5280 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005281 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005282
5283 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5284
5285 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5286 if (!stat_f[STATS_DOMAIN_PROXY]) {
5287 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5288 err_code |= ERR_ALERT | ERR_FATAL;
5289 return err_code;
5290 }
5291
5292 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5293 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5294
5295 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5296 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5297 mod->stats,
5298 mod->stats_count * sizeof(struct name_desc));
5299 i += mod->stats_count;
5300 }
5301
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005302 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005303 if (!stats_allocate_proxy_counters(px)) {
5304 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5305 err_code |= ERR_ALERT | ERR_FATAL;
5306 return err_code;
5307 }
5308 }
5309
Christopher Fauletde79cd22021-01-06 07:41:56 +01005310 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005311
5312 return err_code;
5313}
5314
5315REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5316
Emeric Brunf8642ee2021-10-29 17:59:18 +02005317static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005318{
5319 struct stats_module *mod;
5320 size_t i = 0;
5321 int err_code = 0;
5322
Emeric Brunf8642ee2021-10-29 17:59:18 +02005323 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5324 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5325 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005326 err_code |= ERR_ALERT | ERR_FATAL;
5327 return err_code;
5328 }
5329
Emeric Brunf8642ee2021-10-29 17:59:18 +02005330 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5331 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005332 mod->stats,
5333 mod->stats_count * sizeof(struct name_desc));
5334 i += mod->stats_count;
5335 }
5336
Emeric Brunf8642ee2021-10-29 17:59:18 +02005337 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5338 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005339 err_code |= ERR_ALERT | ERR_FATAL;
5340 return err_code;
5341 }
5342
Christopher Fauletde79cd22021-01-06 07:41:56 +01005343 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005344
5345 return err_code;
5346}
5347
Emeric Brunf8642ee2021-10-29 17:59:18 +02005348REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005349
Christopher Fauletde79cd22021-01-06 07:41:56 +01005350static int allocate_stat_lines_per_thread(void)
5351{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005352 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005353
5354 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5355 const int domain = domains[i];
5356
5357 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5358 if (!stat_l[domain])
5359 return 0;
5360 }
5361 return 1;
5362}
5363
5364REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5365
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005366static int allocate_trash_counters(void)
5367{
5368 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005369 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005370 size_t max_counters_size = 0;
5371
5372 /* calculate the greatest counters used by any stats modules */
5373 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5374 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5375 max_counters_size = mod->counters_size > max_counters_size ?
5376 mod->counters_size : max_counters_size;
5377 }
5378 }
5379
5380 /* allocate the trash with the size of the greatest counters */
5381 if (max_counters_size) {
5382 trash_counters = malloc(max_counters_size);
5383 if (!trash_counters) {
5384 ha_alert("stats: cannot allocate trash counters for statistics\n");
5385 return 0;
5386 }
5387 }
5388
5389 return 1;
5390}
5391
5392REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5393
Christopher Fauletde79cd22021-01-06 07:41:56 +01005394static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005395{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005396 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005397
5398 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5399 const int domain = domains[i];
5400
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005401 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005402 }
5403}
5404
5405
5406REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5407
5408static void deinit_stats(void)
5409{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005410 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005411
5412 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5413 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005414
5415 if (stat_f[domain])
5416 free(stat_f[domain]);
5417 }
5418}
5419
5420REGISTER_POST_DEINIT(deinit_stats);
5421
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005422static void free_trash_counters(void)
5423{
5424 if (trash_counters)
5425 free(trash_counters);
5426}
5427
5428REGISTER_PER_THREAD_FREE(free_trash_counters);
5429
Willy Tarreau2b812e22016-11-22 16:18:05 +01005430/* register cli keywords */
5431static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005432 { { "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 +02005433 { { "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 +02005434 { { "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 },
5435 { { "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 +01005436 {{},}
5437}};
5438
Willy Tarreau0108d902018-11-25 19:14:37 +01005439INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5440
William Lallemand74c24fb2016-11-21 17:18:36 +01005441struct applet http_stats_applet = {
5442 .obj_type = OBJ_TYPE_APPLET,
5443 .name = "<STATS>", /* used for logging */
5444 .fct = http_stats_io_handler,
5445 .release = NULL,
5446};
5447
William Lallemand74c24fb2016-11-21 17:18:36 +01005448/*
5449 * Local variables:
5450 * c-indent-level: 8
5451 * c-basic-offset: 8
5452 * End:
5453 */