blob: 0fa2df3620e2a9ddc36539b20749092f0a6340ab [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <pwd.h>
20#include <grp.h>
21
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020027#include <haproxy/activity.h>
Willy Tarreau91cefca2022-05-03 17:08:29 +020028#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020029#include <haproxy/backend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020031#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020032#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020033#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau55542642021-10-08 09:33:24 +020035#include <haproxy/clock.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020036#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020037#include <haproxy/debug.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020038#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/fd.h>
40#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020041#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020042#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020043#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020044#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020045#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020046#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020047#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020048#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020049#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020050#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020051#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020052#include <haproxy/pipe.h>
53#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020054#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010055#include <haproxy/resolvers.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020056#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020057#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020058#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020059#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020060#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020061#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020062#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020063#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020064#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020065#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020066#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020067#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010068
William Lallemand74c24fb2016-11-21 17:18:36 +010069
Christopher Faulet2b9b6782019-02-27 16:42:58 +010070/* status codes available for the stats admin page (strictly 4 chars length) */
71const char *stat_status_codes[STAT_STATUS_SIZE] = {
72 [STAT_STATUS_DENY] = "DENY",
73 [STAT_STATUS_DONE] = "DONE",
74 [STAT_STATUS_ERRP] = "ERRP",
75 [STAT_STATUS_EXCD] = "EXCD",
76 [STAT_STATUS_NONE] = "NONE",
77 [STAT_STATUS_PART] = "PART",
78 [STAT_STATUS_UNKN] = "UNKN",
79 [STAT_STATUS_IVAL] = "IVAL",
80};
81
Willy Tarreau0baac8c2016-11-22 16:36:53 +010082/* These are the field names for each INF_* field position. Please pay attention
83 * to always use the exact same name except that the strings for new names must
84 * be lower case or CamelCase while the enum entries must be upper case.
85 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020086const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020087 [INF_NAME] = { .name = "Name", .desc = "Product name" },
88 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
89 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
90 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
Willy Tarreau91358592021-06-15 08:08:04 +020091 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (historical, always 1)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +020092 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020093 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
94 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
95 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
William Dauchydefd1562021-01-15 22:41:38 +010096 [INF_START_TIME_SEC] = { .name = "Start_time_sec", .desc = "Start time in seconds" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020097 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
William Dauchya8766cf2021-01-15 22:41:37 +010098 [INF_MEMMAX_BYTES] = { .name = "Memmax_bytes", .desc = "Worker process's hard limit on memory usage in byes (-m on command line)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020099 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100100 [INF_POOL_ALLOC_BYTES] = { .name = "PoolAlloc_bytes", .desc = "Amount of memory allocated in pools (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200101 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100102 [INF_POOL_USED_BYTES] = { .name = "PoolUsed_bytes", .desc = "Amount of pool memory currently used (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200103 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
104 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
105 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
106 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
107 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
108 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
109 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
110 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
111 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
112 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
113 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
114 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
115 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
116 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
117 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
118 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
119 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
120 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
121 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
122 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
123 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
124 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
125 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
126 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
127 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
128 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
129 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
130 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
131 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
132 [INF_SSL_CACHE_MISSES] = { .name = "SslCacheMisses", .desc = "Total number of SSL session ID lookups that didn't find a session in the SSL session cache on this worker since started" },
133 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
134 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
135 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
136 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
137 [INF_MAX_ZLIB_MEM_USAGE] = { .name = "MaxZlibMemUsage", .desc = "Limit on the amount of memory used by HTTP compression above which it is automatically disabled (in bytes, see global.maxzlibmem)" },
138 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
139 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
140 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
141 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
142 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
143 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
144 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
145 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
146 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
147 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
148 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
149 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
150 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
151 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
152 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
Christopher Fauletaaa70852020-07-10 13:56:30 +0200153 [INF_TOTAL_SPLICED_BYTES_OUT] = { .name = "TotalSplicdedBytesOut", .desc = "Total number of bytes emitted by current worker process through a kernel pipe since started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200154 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200155 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Emeric Brun45c457a2020-07-09 23:23:34 +0200156 [INF_CUM_LOG_MSGS] = { .name = "CumRecvLogs", .desc = "Total number of log messages received by log-forwarding listeners on this worker process since started" },
Adis Nezirovicb62b78b2021-01-15 13:12:33 +0100157 [INF_BUILD_INFO] = { .name = "Build info", .desc = "Build info" },
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +0200158 [INF_TAINTED] = { .name = "Tainted", .desc = "Experimental features used" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100159};
160
Willy Tarreaueaa55372019-10-09 07:39:11 +0200161const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200162 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
163 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100164 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
165 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100166 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
167 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200168 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
169 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
170 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
171 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
172 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
173 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
174 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
175 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
176 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
177 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
178 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
179 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
Willy Tarreaubd715102020-10-23 22:44:30 +0200180 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server's effective weight, or sum of active servers' effective weights for a backend" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200181 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
182 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
183 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
184 [ST_F_CHKDOWN] = { .name = "chkdown", .desc = "Total number of failed checks causing UP to DOWN server transitions, per server/backend, since the worker process started" },
185 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
186 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
187 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +0200188 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200189 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
190 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
191 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
192 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
193 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
194 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
195 [ST_F_RATE] = { .name = "rate", .desc = "Total number of sessions processed by this object over the last second (sessions for listeners/frontends, requests for backends/servers)" },
196 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100197 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of sessions per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200198 [ST_F_CHECK_STATUS] = { .name = "check_status", .desc = "Status report of the server's latest health check, prefixed with '*' if a check is currently in progress" },
199 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
200 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
201 [ST_F_HRSP_1XX] = { .name = "hrsp_1xx", .desc = "Total number of HTTP responses with status 100-199 returned by this object since the worker process started" },
202 [ST_F_HRSP_2XX] = { .name = "hrsp_2xx", .desc = "Total number of HTTP responses with status 200-299 returned by this object since the worker process started" },
203 [ST_F_HRSP_3XX] = { .name = "hrsp_3xx", .desc = "Total number of HTTP responses with status 300-399 returned by this object since the worker process started" },
204 [ST_F_HRSP_4XX] = { .name = "hrsp_4xx", .desc = "Total number of HTTP responses with status 400-499 returned by this object since the worker process started" },
205 [ST_F_HRSP_5XX] = { .name = "hrsp_5xx", .desc = "Total number of HTTP responses with status 500-599 returned by this object since the worker process started" },
206 [ST_F_HRSP_OTHER] = { .name = "hrsp_other", .desc = "Total number of HTTP responses with status <100, >599 returned by this object since the worker process started (error -1 included)" },
207 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
208 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100209 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of http requests observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200210 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
211 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
212 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
213 [ST_F_COMP_IN] = { .name = "comp_in", .desc = "Total number of bytes submitted to the HTTP compressor for this object since the worker process started" },
214 [ST_F_COMP_OUT] = { .name = "comp_out", .desc = "Total number of bytes emitted by the HTTP compressor for this object since the worker process started" },
215 [ST_F_COMP_BYP] = { .name = "comp_byp", .desc = "Total number of bytes that bypassed HTTP compression for this object since the worker process started (CPU/memory/bandwidth limitation)" },
216 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
217 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
218 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
219 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
220 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
221 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
222 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
223 [ST_F_TTIME] = { .name = "ttime", .desc = "Total request+response time (request+queue+connect+response+processing), in milliseconds, averaged over the 1024 last requests (backend/server)" },
224 [ST_F_AGENT_STATUS] = { .name = "agent_status", .desc = "Status report of the server's latest agent check, prefixed with '*' if a check is currently in progress" },
225 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
226 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
227 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
228 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
229 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
230 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
231 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
232 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
233 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
234 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
235 [ST_F_ADDR] = { .name = "addr", .desc = "Server's address:port, shown only if show-legends is set, or at levels oper/admin for the CLI" },
236 [ST_F_COOKIE] = { .name = "cookie", .desc = "Backend's cookie name or Server's cookie value, shown only if show-legends is set, or at levels oper/admin for the CLI" },
237 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
238 [ST_F_ALGO] = { .name = "algo", .desc = "Backend's load balancing algorithm, shown only if show-legends is set, or at levels oper/admin for the CLI" },
239 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100240 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of connections per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200241 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
242 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
243 [ST_F_DCON] = { .name = "dcon", .desc = "Total number of incoming connections blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
244 [ST_F_DSES] = { .name = "dses", .desc = "Total number of incoming sessions blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
245 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
246 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
247 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
248 [ST_F_CACHE_LOOKUPS] = { .name = "cache_lookups", .desc = "Total number of HTTP requests looked up in the cache on this frontend/backend since the worker process started" },
249 [ST_F_CACHE_HITS] = { .name = "cache_hits", .desc = "Total number of HTTP requests not found in the cache on this frontend/backend since the worker process started" },
250 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
251 [ST_F_SRV_ILIM] = { .name = "src_ilim", .desc = "Limit on the number of available idle connections on this server (server 'pool_max_conn' directive)" },
Christopher Faulet0d1c2a62019-11-08 14:59:51 +0100252 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
253 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
254 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
255 [ST_F_TT_MAX] = { .name = "ttime_max", .desc = "Maximum observed total request+response time (request+queue+connect+response+processing), in milliseconds (backend/server)" },
Christopher Faulet0159ee42019-12-16 14:40:39 +0100256 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200257 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
258 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
259 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200260 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200261 [ST_F_UWEIGHT] = { .name = "uweight", .desc = "Server's user weight, or sum of active servers' user weights for a backend" },
Cedric Paillet7d6644e2022-12-08 09:17:00 +0000262 [ST_F_AGG_SRV_CHECK_STATUS] = { .name = "agg_server_check_status", .desc = "[DEPRECATED] Backend's aggregated gauge of servers' status" },
263 [ST_F_AGG_SRV_STATUS ] = { .name = "agg_server_status", .desc = "Backend's aggregated gauge of servers' status" },
Cedric Paillete06e31e2022-12-08 09:17:01 +0000264 [ST_F_AGG_CHECK_STATUS] = { .name = "agg_check_status", .desc = "Backend's aggregated gauge of servers' state check status" },
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +0100265 [ST_F_SRID] = { .name = "srid", .desc = "Server id revision, to prevent server id reuse mixups" },
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100266 [ST_F_SESS_OTHER] = { .name = "sess_other", .desc = "Total number of sessions other than HTTP since process started" },
267 [ST_F_H1SESS] = { .name = "h1sess", .desc = "Total number of HTTP/1 sessions since process started" },
268 [ST_F_H2SESS] = { .name = "h2sess", .desc = "Total number of HTTP/2 sessions since process started" },
269 [ST_F_H3SESS] = { .name = "h3sess", .desc = "Total number of HTTP/3 sessions since process started" },
270 [ST_F_REQ_OTHER] = { .name = "req_other", .desc = "Total number of sessions other than HTTP processed by this object since the worker process started" },
271 [ST_F_H1REQ] = { .name = "h1req", .desc = "Total number of HTTP/1 sessions processed by this object since the worker process started" },
272 [ST_F_H2REQ] = { .name = "h2req", .desc = "Total number of hTTP/2 sessions processed by this object since the worker process started" },
273 [ST_F_H3REQ] = { .name = "h3req", .desc = "Total number of HTTP/3 sessions processed by this object since the worker process started" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100274};
275
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100276/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100277THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200278
279/* description of statistics (static and dynamic) */
280static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
281static size_t stat_count[STATS_DOMAIN_COUNT];
282
283/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100284THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100285
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200286/* list of all registered stats module */
287static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
288 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Emeric Brunf8642ee2021-10-29 17:59:18 +0200289 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_RESOLVERS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200290};
291
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100292THREAD_LOCAL void *trash_counters;
Christopher Fauleta8b76842022-12-16 15:08:36 +0100293static THREAD_LOCAL struct buffer trash_chunk = BUF_NULL;
294
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100295
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200296static inline uint8_t stats_get_domain(uint32_t domain)
297{
298 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
299}
300
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200301static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
302{
303 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
304}
305
Christopher Faulet6338a082019-09-09 15:50:54 +0200306static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100307
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +0100308int stats_putchk(struct channel *chn, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +0100309{
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +0100310 struct buffer *chk = &trash_chunk;
311
Christopher Fauletef779222018-10-31 08:47:01 +0100312 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100313 if (chk->data >= channel_htx_recv_max(chn, htx))
314 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200315 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100316 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100317 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100318 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100319 }
320 else {
321 if (ci_putchk(chn, chk) == -1)
322 return 0;
323 }
324 return 1;
325}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100326
Willy Tarreaucaff6312022-05-27 10:17:46 +0200327static const char *stats_scope_ptr(struct appctx *appctx, struct stconn *sc)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100328{
Willy Tarreau91cefca2022-05-03 17:08:29 +0200329 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +0200330 struct channel *req = sc_oc(sc);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200331 struct htx *htx = htxbuf(&req->buf);
332 struct htx_blk *blk;
333 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100334
Christopher Fauletb7f88902019-07-15 21:56:43 +0200335 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100336 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200337 ALREADY_CHECKED(blk);
338 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Willy Tarreau91cefca2022-05-03 17:08:29 +0200339 return uri.ptr + ctx->scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100340}
341
William Lallemand74c24fb2016-11-21 17:18:36 +0100342/*
343 * http_stats_io_handler()
344 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
345 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100346 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100347 * -> stats_dump_html_head() // emits the HTML headers
348 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
349 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
350 * -> stats_dump_html_px_hdr()
351 * -> stats_dump_fe_stats()
352 * -> stats_dump_li_stats()
353 * -> stats_dump_sv_stats()
354 * -> stats_dump_be_stats()
355 * -> stats_dump_html_px_end()
356 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100357 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100358 */
359
360
Christopher Fauleta8b76842022-12-16 15:08:36 +0100361/* Dumps the stats CSV header to the local trash buffer. The caller is
362 * responsible for clearing it if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +0100363 * NOTE: Some tools happen to rely on the field position instead of its name,
364 * so please only append new fields at the end, never in the middle.
365 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200366static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100367{
368 int field;
369
Christopher Fauleta8b76842022-12-16 15:08:36 +0100370 chunk_appendf(&trash_chunk, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200371 if (stat_f[domain]) {
372 for (field = 0; field < stat_count[domain]; ++field) {
Christopher Fauleta8b76842022-12-16 15:08:36 +0100373 chunk_appendf(&trash_chunk, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100374
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200375 /* print special delimiter on proxy stats to mark end of
376 static fields */
377 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
Christopher Fauleta8b76842022-12-16 15:08:36 +0100378 chunk_appendf(&trash_chunk, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200379 }
380 }
381
Christopher Fauleta8b76842022-12-16 15:08:36 +0100382 chunk_appendf(&trash_chunk, "\n");
William Lallemand74c24fb2016-11-21 17:18:36 +0100383}
384
William Lallemand74c24fb2016-11-21 17:18:36 +0100385/* Emits a stats field without any surrounding element and properly encoded to
386 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
387 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200388int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100389{
390 switch (field_format(f, 0)) {
391 case FF_EMPTY: return 1;
392 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
393 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
394 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
395 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200396 case FF_FLT: {
397 size_t prev_data = out->data;
398 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
399 return out->data;
400 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100401 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
402 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
403 }
404}
405
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200406const char *field_to_html_str(const struct field *f)
407{
408 switch (field_format(f, 0)) {
409 case FF_S32: return U2H(f->u.s32);
410 case FF_S64: return U2H(f->u.s64);
411 case FF_U64: return U2H(f->u.u64);
412 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200413 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200414 case FF_STR: return field_str(f, 0);
415 case FF_EMPTY:
416 default:
417 return "";
418 }
419}
420
William Lallemand74c24fb2016-11-21 17:18:36 +0100421/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
422 * output is supposed to be used on its own line. Returns non-zero on success, 0
423 * if the buffer is full.
424 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200425int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100426{
427 switch (field_format(f, 0)) {
428 case FF_EMPTY: return 1;
429 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
430 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
431 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
432 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200433 case FF_FLT: {
434 size_t prev_data = out->data;
435 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
436 return out->data;
437 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100438 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
439 default: return chunk_appendf(out, "%08x:?", f->type);
440 }
441}
442
Simon Horman05ee2132017-01-04 09:37:25 +0100443/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
444 * the recommendation for interoperable integers in section 6 of RFC 7159.
445 */
446#define JSON_INT_MAX ((1ULL << 53) - 1)
447#define JSON_INT_MIN (0 - JSON_INT_MAX)
448
449/* Emits a stats field value and its type in JSON.
450 * Returns non-zero on success, 0 on error.
451 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200452int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100453{
454 int old_len;
455 char buf[20];
456 const char *type, *value = buf, *quote = "";
457
458 switch (field_format(f, 0)) {
459 case FF_EMPTY: return 1;
460 case FF_S32: type = "\"s32\"";
461 snprintf(buf, sizeof(buf), "%d", f->u.s32);
462 break;
463 case FF_U32: type = "\"u32\"";
464 snprintf(buf, sizeof(buf), "%u", f->u.u32);
465 break;
466 case FF_S64: type = "\"s64\"";
467 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
468 return 0;
469 type = "\"s64\"";
470 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
471 break;
472 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
473 return 0;
474 type = "\"u64\"";
475 snprintf(buf, sizeof(buf), "%llu",
476 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200477 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200478 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200479 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100480 break;
481 case FF_STR: type = "\"str\"";
482 value = field_str(f, 0);
483 quote = "\"";
484 break;
485 default: snprintf(buf, sizeof(buf), "%u", f->type);
486 type = buf;
487 value = "unknown";
488 quote = "\"";
489 break;
490 }
491
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200492 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100493 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
494 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200495 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100496}
497
William Lallemand74c24fb2016-11-21 17:18:36 +0100498/* Emits an encoding of the field type on 3 characters followed by a delimiter.
499 * Returns non-zero on success, 0 if the buffer is full.
500 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200501int stats_emit_field_tags(struct buffer *out, const struct field *f,
502 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100503{
504 char origin, nature, scope;
505
506 switch (field_origin(f, 0)) {
507 case FO_METRIC: origin = 'M'; break;
508 case FO_STATUS: origin = 'S'; break;
509 case FO_KEY: origin = 'K'; break;
510 case FO_CONFIG: origin = 'C'; break;
511 case FO_PRODUCT: origin = 'P'; break;
512 default: origin = '?'; break;
513 }
514
515 switch (field_nature(f, 0)) {
516 case FN_GAUGE: nature = 'G'; break;
517 case FN_LIMIT: nature = 'L'; break;
518 case FN_MIN: nature = 'm'; break;
519 case FN_MAX: nature = 'M'; break;
520 case FN_RATE: nature = 'R'; break;
521 case FN_COUNTER: nature = 'C'; break;
522 case FN_DURATION: nature = 'D'; break;
523 case FN_AGE: nature = 'A'; break;
524 case FN_TIME: nature = 'T'; break;
525 case FN_NAME: nature = 'N'; break;
526 case FN_OUTPUT: nature = 'O'; break;
527 case FN_AVG: nature = 'a'; break;
528 default: nature = '?'; break;
529 }
530
531 switch (field_scope(f, 0)) {
532 case FS_PROCESS: scope = 'P'; break;
533 case FS_SERVICE: scope = 'S'; break;
534 case FS_SYSTEM: scope = 's'; break;
535 case FS_CLUSTER: scope = 'C'; break;
536 default: scope = '?'; break;
537 }
538
539 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
540}
541
Simon Horman05ee2132017-01-04 09:37:25 +0100542/* Emits an encoding of the field type as JSON.
543 * Returns non-zero on success, 0 if the buffer is full.
544 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200545int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100546{
547 const char *origin, *nature, *scope;
548 int old_len;
549
550 switch (field_origin(f, 0)) {
551 case FO_METRIC: origin = "Metric"; break;
552 case FO_STATUS: origin = "Status"; break;
553 case FO_KEY: origin = "Key"; break;
554 case FO_CONFIG: origin = "Config"; break;
555 case FO_PRODUCT: origin = "Product"; break;
556 default: origin = "Unknown"; break;
557 }
558
559 switch (field_nature(f, 0)) {
560 case FN_GAUGE: nature = "Gauge"; break;
561 case FN_LIMIT: nature = "Limit"; break;
562 case FN_MIN: nature = "Min"; break;
563 case FN_MAX: nature = "Max"; break;
564 case FN_RATE: nature = "Rate"; break;
565 case FN_COUNTER: nature = "Counter"; break;
566 case FN_DURATION: nature = "Duration"; break;
567 case FN_AGE: nature = "Age"; break;
568 case FN_TIME: nature = "Time"; break;
569 case FN_NAME: nature = "Name"; break;
570 case FN_OUTPUT: nature = "Output"; break;
571 case FN_AVG: nature = "Avg"; break;
572 default: nature = "Unknown"; break;
573 }
574
575 switch (field_scope(f, 0)) {
576 case FS_PROCESS: scope = "Process"; break;
577 case FS_SERVICE: scope = "Service"; break;
578 case FS_SYSTEM: scope = "System"; break;
579 case FS_CLUSTER: scope = "Cluster"; break;
580 default: scope = "Unknown"; break;
581 }
582
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200583 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100584 chunk_appendf(out, "\"tags\":{"
585 "\"origin\":\"%s\","
586 "\"nature\":\"%s\","
587 "\"scope\":\"%s\""
588 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200589 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100590}
William Lallemand74c24fb2016-11-21 17:18:36 +0100591
592/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200593static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200594 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100595 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100596{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100597 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100598 int field;
599
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200600 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100601 if (!stats_emit_raw_data_field(out, &stats[field]))
602 return 0;
603 if (!chunk_strcat(out, ","))
604 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200605
606 /* print special delimiter on proxy stats to mark end of
607 static fields */
608 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
609 if (!chunk_strcat(out, "-,"))
610 return 0;
611 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100612 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200613
William Lallemand74c24fb2016-11-21 17:18:36 +0100614 chunk_strcat(out, "\n");
615 return 1;
616}
617
618/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200619static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200620 const struct field *stats,
621 size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100622 struct show_stat_ctx * ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100623{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100624 int flags = ctx->flags;
625 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100626 int field;
627
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200628 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100629 if (!stats[field].type)
630 continue;
631
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200632 switch (domain) {
633 case STATS_DOMAIN_PROXY:
634 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
635 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
636 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
637 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
638 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
639 '?',
640 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
641 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200642 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200643 stats[ST_F_PID].u.u32);
644 break;
645
Emeric Brunf8642ee2021-10-29 17:59:18 +0200646 case STATS_DOMAIN_RESOLVERS:
647 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200648 stat_f[domain][field].name);
649 break;
650
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200651 default:
652 break;
653 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100654
655 if (!stats_emit_field_tags(out, &stats[field], ':'))
656 return 0;
657 if (!stats_emit_typed_data_field(out, &stats[field]))
658 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200659
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200660 if (flags & STAT_SHOW_FDESC &&
661 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200662 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200663 }
664
William Lallemand74c24fb2016-11-21 17:18:36 +0100665 if (!chunk_strcat(out, "\n"))
666 return 0;
667 }
668 return 1;
669}
670
Simon Horman05ee2132017-01-04 09:37:25 +0100671/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200672static int stats_dump_json_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100673 const struct field *info,
674 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100675{
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100676 int started = (ctx->field) ? 1 : 0;
677 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100678
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100679 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100680 return 0;
681
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100682 for (; ctx->field < INF_TOTAL_FIELDS; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100683 int old_len;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100684 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100685
686 if (!field_format(info, field))
687 continue;
688
689 if (started && !chunk_strcat(out, ","))
690 goto err;
691 started = 1;
692
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200693 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100694 chunk_appendf(out,
695 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
696 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200697 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100698 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200699 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100700 goto err;
701
702 if (!stats_emit_json_field_tags(out, &info[field]))
703 goto err;
704
705 if (!stats_emit_json_data_field(out, &info[field]))
706 goto err;
707
708 if (!chunk_strcat(out, "}"))
709 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100710 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100711 }
712
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200713 if (!chunk_strcat(out, "]\n"))
Simon Horman05ee2132017-01-04 09:37:25 +0100714 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100715 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100716 return 1;
717
718err:
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100719 if (!ready_data) {
720 /* not enough buffer space for a single entry.. */
721 chunk_reset(out);
722 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
723 return 0; /* hard error */
724 }
725 /* push ready data and wait for a new buffer to complete the dump */
726 out->data = ready_data;
727 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100728}
729
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200730static void stats_print_proxy_field_json(struct buffer *out,
731 const struct field *stat,
732 const char *name,
733 int pos,
734 uint32_t field_type,
735 uint32_t iid,
736 uint32_t sid,
737 uint32_t pid)
738{
739 const char *obj_type;
740 switch (field_type) {
741 case STATS_TYPE_FE: obj_type = "Frontend"; break;
742 case STATS_TYPE_BE: obj_type = "Backend"; break;
743 case STATS_TYPE_SO: obj_type = "Listener"; break;
744 case STATS_TYPE_SV: obj_type = "Server"; break;
745 default: obj_type = "Unknown"; break;
746 }
747
748 chunk_appendf(out,
749 "{"
750 "\"objType\":\"%s\","
751 "\"proxyId\":%u,"
752 "\"id\":%u,"
753 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
754 "\"processNum\":%u,",
755 obj_type, iid, sid, pos, name, pid);
756}
757
Emeric Brunf8642ee2021-10-29 17:59:18 +0200758static void stats_print_rslv_field_json(struct buffer *out,
759 const struct field *stat,
760 const char *name,
761 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200762{
763 chunk_appendf(out,
764 "{"
765 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
766 pos, name);
767}
768
769
Simon Horman05ee2132017-01-04 09:37:25 +0100770/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200771static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200772 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100773 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100774{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100775 int flags = ctx->flags;
776 int domain = ctx->domain;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100777 int started = (ctx->field) ? 1 : 0;
778 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100779
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100780 if (!started && (flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100781 return 0;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100782 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100783 return 0;
784
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100785 for (; ctx->field < stats_count; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100786 int old_len;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100787 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100788
789 if (!stats[field].type)
790 continue;
791
792 if (started && !chunk_strcat(out, ","))
793 goto err;
794 started = 1;
795
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200796 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200797 if (domain == STATS_DOMAIN_PROXY) {
798 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200799 stat_f[domain][field].name,
800 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200801 stats[ST_F_TYPE].u.u32,
802 stats[ST_F_IID].u.u32,
803 stats[ST_F_SID].u.u32,
804 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200805 } else if (domain == STATS_DOMAIN_RESOLVERS) {
806 stats_print_rslv_field_json(out, &stats[field],
807 stat_f[domain][field].name,
808 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200809 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200810
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200811 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100812 goto err;
813
814 if (!stats_emit_json_field_tags(out, &stats[field]))
815 goto err;
816
817 if (!stats_emit_json_data_field(out, &stats[field]))
818 goto err;
819
820 if (!chunk_strcat(out, "}"))
821 goto err;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100822 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100823 }
824
825 if (!chunk_strcat(out, "]"))
826 goto err;
827
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100828 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100829 return 1;
830
831err:
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100832 if (!ready_data) {
833 /* not enough buffer space for a single entry.. */
834 chunk_reset(out);
835 if (ctx->flags & STAT_STARTED)
836 chunk_strcat(out, ",");
837 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
838 return 0; /* hard error */
839 }
840 /* push ready data and wait for a new buffer to complete the dump */
841 out->data = ready_data;
842 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100843}
844
William Lallemand74c24fb2016-11-21 17:18:36 +0100845/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200846 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200847 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
848 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
849 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100850 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200851static int stats_dump_fields_html(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100852 const struct field *stats,
853 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100854{
Willy Tarreau83061a82018-07-13 11:56:34 +0200855 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200856 struct stats_module *mod;
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100857 int flags = ctx->flags;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200858 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100859
860 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
861 chunk_appendf(out,
862 /* name, queue */
863 "<tr class=\"frontend\">");
864
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200865 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100866 /* Column sub-heading for Enable or Disable server */
867 chunk_appendf(out, "<td></td>");
868 }
869
870 chunk_appendf(out,
871 "<td class=ac>"
872 "<a name=\"%s/Frontend\"></a>"
873 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
874 "<td colspan=3></td>"
875 "",
876 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
877
878 chunk_appendf(out,
879 /* sessions rate : current */
880 "<td><u>%s<div class=tips><table class=det>"
881 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
882 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
883 "",
884 U2H(stats[ST_F_RATE].u.u32),
885 U2H(stats[ST_F_CONN_RATE].u.u32),
886 U2H(stats[ST_F_RATE].u.u32));
887
888 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
889 chunk_appendf(out,
890 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
891 U2H(stats[ST_F_REQ_RATE].u.u32));
892
893 chunk_appendf(out,
894 "</table></div></u></td>"
895 /* sessions rate : max */
896 "<td><u>%s<div class=tips><table class=det>"
897 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
898 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
899 "",
900 U2H(stats[ST_F_RATE_MAX].u.u32),
901 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
902 U2H(stats[ST_F_RATE_MAX].u.u32));
903
904 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
905 chunk_appendf(out,
906 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
907 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
908
909 chunk_appendf(out,
910 "</table></div></u></td>"
911 /* sessions rate : limit */
912 "<td>%s</td>",
913 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
914
915 chunk_appendf(out,
916 /* sessions: current, max, limit, total */
917 "<td>%s</td><td>%s</td><td>%s</td>"
918 "<td><u>%s<div class=tips><table class=det>"
919 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
920 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
921 "",
922 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
923 U2H(stats[ST_F_STOT].u.u64),
924 U2H(stats[ST_F_CONN_TOT].u.u64),
925 U2H(stats[ST_F_STOT].u.u64));
926
927 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
928 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
929 chunk_appendf(out,
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100930 "<tr><th>- HTTP/1 sessions:</th><td>%s</td></tr>"
931 "<tr><th>- HTTP/2 sessions:</th><td>%s</td></tr>"
932 "<tr><th>- HTTP/3 sessions:</th><td>%s</td></tr>"
933 "<tr><th>- other sessions:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100934 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100935 "<tr><th>- HTTP/1 requests:</th><td>%s</td></tr>"
936 "<tr><th>- HTTP/2 requests:</th><td>%s</td></tr>"
937 "<tr><th>- HTTP/3 requests:</th><td>%s</td></tr>"
938 "<tr><th>- other requests:</th><td>%s</td></tr>"
939 "",
940 U2H(stats[ST_F_H1SESS].u.u64),
941 U2H(stats[ST_F_H2SESS].u.u64),
942 U2H(stats[ST_F_H3SESS].u.u64),
943 U2H(stats[ST_F_SESS_OTHER].u.u64),
944 U2H(stats[ST_F_REQ_TOT].u.u64),
945 U2H(stats[ST_F_H1REQ].u.u64),
946 U2H(stats[ST_F_H2REQ].u.u64),
947 U2H(stats[ST_F_H3REQ].u.u64),
948 U2H(stats[ST_F_REQ_OTHER].u.u64));
949
950 chunk_appendf(out,
William Lallemand74c24fb2016-11-21 17:18:36 +0100951 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
952 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
953 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
954 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
955 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
956 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
957 "<tr><th>- other responses:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100958 "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100959 U2H(stats[ST_F_HRSP_1XX].u.u64),
960 U2H(stats[ST_F_HRSP_2XX].u.u64),
961 U2H(stats[ST_F_COMP_RSP].u.u64),
962 stats[ST_F_HRSP_2XX].u.u64 ?
963 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
964 U2H(stats[ST_F_HRSP_3XX].u.u64),
965 U2H(stats[ST_F_HRSP_4XX].u.u64),
966 U2H(stats[ST_F_HRSP_5XX].u.u64),
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100967 U2H(stats[ST_F_HRSP_OTHER].u.u64));
968
969 chunk_appendf(out,
970 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
971 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
972 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
973 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
974 "<tr><th>Internal errors:</th><td>%s</td></tr>"
975 "",
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200976 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100977 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
978 U2H(stats[ST_F_CACHE_HITS].u.u64),
979 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
980 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100981 U2H(stats[ST_F_WREW].u.u64),
982 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100983 }
984
985 chunk_appendf(out,
986 "</table></div></u></td>"
987 /* sessions: lbtot, lastsess */
988 "<td></td><td></td>"
989 /* bytes : in */
990 "<td>%s</td>"
991 "",
992 U2H(stats[ST_F_BIN].u.u64));
993
994 chunk_appendf(out,
995 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
996 "<td>%s%s<div class=tips><table class=det>"
997 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
998 "<tr><th>Compression in:</th><td>%s</td></tr>"
999 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1000 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1001 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1002 "</table></div>%s</td>",
1003 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1004 U2H(stats[ST_F_BOUT].u.u64),
1005 U2H(stats[ST_F_BOUT].u.u64),
1006 U2H(stats[ST_F_COMP_IN].u.u64),
1007 U2H(stats[ST_F_COMP_OUT].u.u64),
1008 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1009 U2H(stats[ST_F_COMP_BYP].u.u64),
1010 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1011 stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0,
1012 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1013
1014 chunk_appendf(out,
1015 /* denied: req, resp */
1016 "<td>%s</td><td>%s</td>"
1017 /* errors : request, connect, response */
1018 "<td>%s</td><td></td><td></td>"
1019 /* warnings: retries, redispatches */
1020 "<td></td><td></td>"
1021 /* server status : reflect frontend status */
1022 "<td class=ac>%s</td>"
1023 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001024 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001025 "",
1026 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1027 U2H(stats[ST_F_EREQ].u.u64),
1028 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001029
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001030 if (flags & STAT_SHMODULES) {
1031 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001032 chunk_appendf(out, "<td>");
1033
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001034 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001035 chunk_appendf(out,
1036 "<u>%s<div class=tips><table class=det>",
1037 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001038 for (j = 0; j < mod->stats_count; ++j) {
1039 chunk_appendf(out,
1040 "<tr><th>%s</th><td>%s</td></tr>",
1041 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1042 ++i;
1043 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001044 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001045 } else {
1046 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001047 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001048
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001049 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001050 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001051 }
1052
1053 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001054 }
1055 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
1056 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001057 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001058 /* Column sub-heading for Enable or Disable server */
1059 chunk_appendf(out, "<td></td>");
1060 }
1061
1062 chunk_appendf(out,
1063 /* frontend name, listener name */
1064 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1065 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1066 "",
1067 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001068 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001069 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1070
Willy Tarreau708c4162019-10-09 10:19:16 +02001071 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001072 chunk_appendf(out, "<div class=tips>");
1073
Willy Tarreau90807112020-02-25 08:16:33 +01001074 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001075 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1076 else if (*field_str(stats, ST_F_ADDR) == '[')
1077 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1078 else if (*field_str(stats, ST_F_ADDR))
1079 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1080
1081 /* id */
1082 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1083 }
1084
1085 chunk_appendf(out,
1086 /* queue */
1087 "%s</td><td colspan=3></td>"
1088 /* sessions rate: current, max, limit */
1089 "<td colspan=3>&nbsp;</td>"
1090 /* sessions: current, max, limit, total, lbtot, lastsess */
1091 "<td>%s</td><td>%s</td><td>%s</td>"
1092 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1093 /* bytes: in, out */
1094 "<td>%s</td><td>%s</td>"
1095 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001096 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001097 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1098 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1099
1100 chunk_appendf(out,
1101 /* denied: req, resp */
1102 "<td>%s</td><td>%s</td>"
1103 /* errors: request, connect, response */
1104 "<td>%s</td><td></td><td></td>"
1105 /* warnings: retries, redispatches */
1106 "<td></td><td></td>"
1107 /* server status: reflect listener status */
1108 "<td class=ac>%s</td>"
1109 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001110 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001111 "",
1112 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1113 U2H(stats[ST_F_EREQ].u.u64),
1114 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001115
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001116 if (flags & STAT_SHMODULES) {
1117 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001118 chunk_appendf(out, "<td>");
1119
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001120 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001121 chunk_appendf(out,
1122 "<u>%s<div class=tips><table class=det>",
1123 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001124 for (j = 0; j < mod->stats_count; ++j) {
1125 chunk_appendf(out,
1126 "<tr><th>%s</th><td>%s</td></tr>",
1127 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1128 ++i;
1129 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001130 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001131 } else {
1132 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001133 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001134
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001135 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001136 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001137 }
1138
1139 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001140 }
1141 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1142 const char *style;
1143
1144 /* determine the style to use depending on the server's state,
1145 * its health and weight. There isn't a 1-to-1 mapping between
1146 * state and styles for the cases where the server is (still)
1147 * up. The reason is that we don't want to report nolb and
1148 * drain with the same color.
1149 */
1150
1151 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1152 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1153 style = "down";
1154 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001155 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001156 style = "going_up";
1157 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001158 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1159 style = "draining";
1160 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001161 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001162 style = "going_down";
1163 }
1164 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1165 style = "nolb";
1166 }
1167 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1168 style = "no_check";
1169 }
1170 else if (!stats[ST_F_CHKFAIL].type ||
1171 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1172 /* no check or max health = UP */
1173 if (stats[ST_F_WEIGHT].u.u32)
1174 style = "up";
1175 else
1176 style = "draining";
1177 }
1178 else {
1179 style = "going_down";
1180 }
1181
Willy Tarreau7b524852020-08-11 10:26:36 +02001182 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001183 chunk_appendf(out, "<tr class=\"maintain\">");
1184 else
1185 chunk_appendf(out,
1186 "<tr class=\"%s_%s\">",
1187 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1188
1189
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001190 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001191 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001192 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1193 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001194 field_str(stats, ST_F_SVNAME));
1195
1196 chunk_appendf(out,
1197 "<td class=ac><a name=\"%s/%s\"></a>%s"
1198 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1199 "",
1200 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001201 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001202 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1203
Willy Tarreau708c4162019-10-09 10:19:16 +02001204 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001205 chunk_appendf(out, "<div class=tips>");
1206
Willy Tarreau90807112020-02-25 08:16:33 +01001207 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001208 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1209 else if (*field_str(stats, ST_F_ADDR) == '[')
1210 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1211 else if (*field_str(stats, ST_F_ADDR))
1212 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1213
1214 /* id */
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01001215 chunk_appendf(out, "id: %d, rid: %d", stats[ST_F_SID].u.u32, stats[ST_F_SRID].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001216
1217 /* cookie */
1218 if (stats[ST_F_COOKIE].type) {
1219 chunk_appendf(out, ", cookie: '");
1220 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1221 chunk_htmlencode(out, &src);
1222 chunk_appendf(out, "'");
1223 }
1224
1225 chunk_appendf(out, "</div>");
1226 }
1227
1228 chunk_appendf(out,
1229 /* queue : current, max, limit */
1230 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1231 /* sessions rate : current, max, limit */
1232 "<td>%s</td><td>%s</td><td></td>"
1233 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001234 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001235 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1236 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1237
1238 chunk_appendf(out,
1239 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001240 "<td><u>%s<div class=tips>"
1241 "<table class=det>"
1242 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001243 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001244 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001245 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1246 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001247 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001248 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1249 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1250 "</table></div></u>"
1251 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001252 "<td><u>%s<div class=tips><table class=det>"
1253 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1254 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001255 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001256 U2H(stats[ST_F_SCUR].u.u32),
1257 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1258 U2H(stats[ST_F_SRV_ICUR].u.u32),
1259 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1260 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001261 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001262
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001263 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1264 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1265 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001266 U2H(stats[ST_F_STOT].u.u64),
1267 U2H(stats[ST_F_STOT].u.u64));
1268
1269 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1270 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001271 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001272 "<tr><th>New connections:</th><td>%s</td></tr>"
1273 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001274 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001275 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1276 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1277 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1278 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1279 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1280 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001281 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001282 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001283 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001284 U2H(stats[ST_F_CONNECT].u.u64),
1285 U2H(stats[ST_F_REUSE].u.u64),
1286 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1287 (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0,
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001288 U2H(stats[ST_F_REQ_TOT].u.u64),
1289 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1290 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1291 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1292 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1293 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1294 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1295 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1296 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1297 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1298 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1299 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1300 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001301 U2H(stats[ST_F_WREW].u.u64),
1302 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001303 }
1304
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001305 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1306 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1307 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1308 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1309 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001310 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001311 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1312 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1313 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1314 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001315
1316 chunk_appendf(out,
1317 "</table></div></u></td>"
1318 /* sessions: lbtot, last */
1319 "<td>%s</td><td>%s</td>",
1320 U2H(stats[ST_F_LBTOT].u.u64),
1321 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1322
1323 chunk_appendf(out,
1324 /* bytes : in, out */
1325 "<td>%s</td><td>%s</td>"
1326 /* denied: req, resp */
1327 "<td></td><td>%s</td>"
1328 /* errors : request, connect */
1329 "<td></td><td>%s</td>"
1330 /* errors : response */
1331 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1332 /* warnings: retries, redispatches */
1333 "<td>%lld</td><td>%lld</td>"
1334 "",
1335 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1336 U2H(stats[ST_F_DRESP].u.u64),
1337 U2H(stats[ST_F_ECON].u.u64),
1338 U2H(stats[ST_F_ERESP].u.u64),
1339 (long long)stats[ST_F_CLI_ABRT].u.u64,
1340 (long long)stats[ST_F_SRV_ABRT].u.u64,
1341 (long long)stats[ST_F_WRETR].u.u64,
1342 (long long)stats[ST_F_WREDIS].u.u64);
1343
1344 /* status, last change */
1345 chunk_appendf(out, "<td class=ac>");
1346
1347 /* FIXME!!!!
1348 * LASTCHG should contain the last change for *this* server and must be computed
1349 * properly above, as was done below, ie: this server if maint, otherwise ref server
1350 * if tracking. Note that ref is either local or remote depending on tracking.
1351 */
1352
1353
Willy Tarreau7b524852020-08-11 10:26:36 +02001354 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001355 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1356 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001357 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001358 chunk_strcat(out, "<i>no check</i>");
1359 }
1360 else {
1361 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
Willy Tarreau7b524852020-08-11 10:26:36 +02001362 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001363 if (stats[ST_F_CHECK_HEALTH].u.u32)
1364 chunk_strcat(out, " &uarr;");
1365 }
1366 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1367 chunk_strcat(out, " &darr;");
1368 }
1369
Willy Tarreau7b524852020-08-11 10:26:36 +02001370 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001371 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1372 chunk_appendf(out,
1373 "</td><td class=ac><u> %s",
1374 field_str(stats, ST_F_AGENT_STATUS));
1375
1376 if (stats[ST_F_AGENT_CODE].type)
1377 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1378
1379 if (stats[ST_F_AGENT_DURATION].type)
1380 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1381
1382 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1383
1384 if (*field_str(stats, ST_F_LAST_AGT)) {
1385 chunk_appendf(out, ": ");
1386 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1387 chunk_htmlencode(out, &src);
1388 }
1389 chunk_appendf(out, "</div></u>");
1390 }
1391 else if (stats[ST_F_CHECK_STATUS].type) {
1392 chunk_appendf(out,
1393 "</td><td class=ac><u> %s",
1394 field_str(stats, ST_F_CHECK_STATUS));
1395
1396 if (stats[ST_F_CHECK_CODE].type)
1397 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1398
1399 if (stats[ST_F_CHECK_DURATION].type)
1400 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1401
1402 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1403
1404 if (*field_str(stats, ST_F_LAST_CHK)) {
1405 chunk_appendf(out, ": ");
1406 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1407 chunk_htmlencode(out, &src);
1408 }
1409 chunk_appendf(out, "</div></u>");
1410 }
1411 else
1412 chunk_appendf(out, "</td><td>");
1413
1414 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001415 /* weight / uweight */
1416 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001417 /* act, bck */
1418 "<td class=ac>%s</td><td class=ac>%s</td>"
1419 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001420 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001421 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1422 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1423
1424 /* check failures: unique, fatal, down time */
1425 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1426 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1427 }
1428 else if (stats[ST_F_CHKFAIL].type) {
1429 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1430
1431 if (stats[ST_F_HANAFAIL].type)
1432 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1433
1434 chunk_appendf(out,
1435 "<div class=tips>Failed Health Checks%s</div></u></td>"
1436 "<td>%lld</td><td>%s</td>"
1437 "",
1438 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1439 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1440 }
1441 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1442 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1443 chunk_appendf(out,
1444 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1445 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1446 }
1447 else
1448 chunk_appendf(out, "<td colspan=3></td>");
1449
1450 /* throttle */
1451 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001452 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001453 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001454 chunk_appendf(out, "<td class=ac>-</td>");
1455
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001456 if (flags & STAT_SHMODULES) {
1457 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001458 chunk_appendf(out, "<td>");
1459
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001460 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001461 chunk_appendf(out,
1462 "<u>%s<div class=tips><table class=det>",
1463 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001464 for (j = 0; j < mod->stats_count; ++j) {
1465 chunk_appendf(out,
1466 "<tr><th>%s</th><td>%s</td></tr>",
1467 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1468 ++i;
1469 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001470 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001471 } else {
1472 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001473 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001474
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001475 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001476 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001477 }
1478
1479 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001480 }
1481 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1482 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001483 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001484 /* Column sub-heading for Enable or Disable server */
1485 chunk_appendf(out, "<td></td>");
1486 }
1487 chunk_appendf(out,
1488 "<td class=ac>"
1489 /* name */
1490 "%s<a name=\"%s/Backend\"></a>"
1491 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1492 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001493 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001494 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1495
Willy Tarreau708c4162019-10-09 10:19:16 +02001496 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001497 /* balancing */
1498 chunk_appendf(out, "<div class=tips>balancing: %s",
1499 field_str(stats, ST_F_ALGO));
1500
1501 /* cookie */
1502 if (stats[ST_F_COOKIE].type) {
1503 chunk_appendf(out, ", cookie: '");
1504 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1505 chunk_htmlencode(out, &src);
1506 chunk_appendf(out, "'");
1507 }
1508 chunk_appendf(out, "</div>");
1509 }
1510
1511 chunk_appendf(out,
1512 "%s</td>"
1513 /* queue : current, max */
1514 "<td>%s</td><td>%s</td><td></td>"
1515 /* sessions rate : current, max, limit */
1516 "<td>%s</td><td>%s</td><td></td>"
1517 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001518 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001519 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1520 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1521
1522 chunk_appendf(out,
1523 /* sessions: current, max, limit, total */
1524 "<td>%s</td><td>%s</td><td>%s</td>"
1525 "<td><u>%s<div class=tips><table class=det>"
1526 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1527 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001528 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
William Lallemand74c24fb2016-11-21 17:18:36 +01001529 U2H(stats[ST_F_STOT].u.u64),
1530 U2H(stats[ST_F_STOT].u.u64));
1531
1532 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1533 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1534 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001535 "<tr><th>New connections:</th><td>%s</td></tr>"
1536 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001537 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1538 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1539 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1540 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1541 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1542 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1543 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1544 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001545 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1546 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001547 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001548 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001549 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001550 U2H(stats[ST_F_CONNECT].u.u64),
1551 U2H(stats[ST_F_REUSE].u.u64),
1552 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1553 (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0,
William Lallemand74c24fb2016-11-21 17:18:36 +01001554 U2H(stats[ST_F_REQ_TOT].u.u64),
1555 U2H(stats[ST_F_HRSP_1XX].u.u64),
1556 U2H(stats[ST_F_HRSP_2XX].u.u64),
1557 U2H(stats[ST_F_COMP_RSP].u.u64),
1558 stats[ST_F_HRSP_2XX].u.u64 ?
1559 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1560 U2H(stats[ST_F_HRSP_3XX].u.u64),
1561 U2H(stats[ST_F_HRSP_4XX].u.u64),
1562 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001563 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001564 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1565 U2H(stats[ST_F_CACHE_HITS].u.u64),
1566 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1567 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001568 U2H(stats[ST_F_WREW].u.u64),
1569 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001570 }
1571
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001572 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1573 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1574 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1575 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1576 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001577 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001578 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1579 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1580 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1581 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001582
1583 chunk_appendf(out,
1584 "</table></div></u></td>"
1585 /* sessions: lbtot, last */
1586 "<td>%s</td><td>%s</td>"
1587 /* bytes: in */
1588 "<td>%s</td>"
1589 "",
1590 U2H(stats[ST_F_LBTOT].u.u64),
1591 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1592 U2H(stats[ST_F_BIN].u.u64));
1593
1594 chunk_appendf(out,
1595 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1596 "<td>%s%s<div class=tips><table class=det>"
1597 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1598 "<tr><th>Compression in:</th><td>%s</td></tr>"
1599 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1600 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1601 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1602 "</table></div>%s</td>",
1603 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1604 U2H(stats[ST_F_BOUT].u.u64),
1605 U2H(stats[ST_F_BOUT].u.u64),
1606 U2H(stats[ST_F_COMP_IN].u.u64),
1607 U2H(stats[ST_F_COMP_OUT].u.u64),
1608 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1609 U2H(stats[ST_F_COMP_BYP].u.u64),
1610 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1611 stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0,
1612 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1613
1614 chunk_appendf(out,
1615 /* denied: req, resp */
1616 "<td>%s</td><td>%s</td>"
1617 /* errors : request, connect */
1618 "<td></td><td>%s</td>"
1619 /* errors : response */
1620 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1621 /* warnings: retries, redispatches */
1622 "<td>%lld</td><td>%lld</td>"
1623 /* backend status: reflect backend status (up/down): we display UP
1624 * if the backend has known working servers or if it has no server at
1625 * all (eg: for stats). Then we display the total weight, number of
1626 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001627 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001628 "<td class=ac>%d</td><td class=ac>%d</td>"
1629 "",
1630 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1631 U2H(stats[ST_F_ECON].u.u64),
1632 U2H(stats[ST_F_ERESP].u.u64),
1633 (long long)stats[ST_F_CLI_ABRT].u.u64,
1634 (long long)stats[ST_F_SRV_ABRT].u.u64,
1635 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1636 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1637 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreaubd715102020-10-23 22:44:30 +02001638 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001639 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1640
1641 chunk_appendf(out,
1642 /* rest of backend: nothing, down transitions, total downtime, throttle */
1643 "<td class=ac>&nbsp;</td><td>%d</td>"
1644 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001645 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001646 stats[ST_F_CHKDOWN].u.u32,
1647 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001648
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001649 if (flags & STAT_SHMODULES) {
1650 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001651 chunk_appendf(out, "<td>");
1652
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001653 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001654 chunk_appendf(out,
1655 "<u>%s<div class=tips><table class=det>",
1656 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001657 for (j = 0; j < mod->stats_count; ++j) {
1658 chunk_appendf(out,
1659 "<tr><th>%s</th><td>%s</td></tr>",
1660 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1661 ++i;
1662 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001663 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001664 } else {
1665 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001666 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001667
1668 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001669 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001670 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001671
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001672 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001673 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001674
William Lallemand74c24fb2016-11-21 17:18:36 +01001675 return 1;
1676}
1677
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001678int stats_dump_one_line(const struct field *stats, size_t stats_count,
1679 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001680{
Willy Tarreau91cefca2022-05-03 17:08:29 +02001681 struct show_stat_ctx *ctx = appctx->svcctx;
Simon Horman05ee2132017-01-04 09:37:25 +01001682 int ret;
1683
Willy Tarreau91cefca2022-05-03 17:08:29 +02001684 if (ctx->flags & STAT_FMT_HTML)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001685 ret = stats_dump_fields_html(&trash_chunk, stats, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001686 else if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001687 ret = stats_dump_fields_typed(&trash_chunk, stats, stats_count, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001688 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001689 ret = stats_dump_fields_json(&trash_chunk, stats, stats_count, ctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001690 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01001691 ret = stats_dump_fields_csv(&trash_chunk, stats, stats_count, ctx);
Simon Horman05ee2132017-01-04 09:37:25 +01001692
Simon Horman05ee2132017-01-04 09:37:25 +01001693 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001694}
1695
William Dauchy0ef54392021-01-17 18:27:45 +01001696/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001697 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1698 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1699 * this value, or if the selected field is not implemented for frontends, the
1700 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001701 */
William Dauchy0ef54392021-01-17 18:27:45 +01001702int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1703 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001704{
William Dauchy0ef54392021-01-17 18:27:45 +01001705 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001706
William Lallemand74c24fb2016-11-21 17:18:36 +01001707 if (len < ST_F_TOTAL_FIELDS)
1708 return 0;
1709
William Dauchy0ef54392021-01-17 18:27:45 +01001710 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001711 struct field metric = { 0 };
1712
William Dauchy0ef54392021-01-17 18:27:45 +01001713 switch (current_field) {
1714 case ST_F_PXNAME:
1715 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1716 break;
1717 case ST_F_SVNAME:
1718 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1719 break;
1720 case ST_F_MODE:
1721 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1722 break;
1723 case ST_F_SCUR:
1724 metric = mkf_u32(0, px->feconn);
1725 break;
1726 case ST_F_SMAX:
1727 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1728 break;
1729 case ST_F_SLIM:
1730 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1731 break;
1732 case ST_F_STOT:
1733 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1734 break;
1735 case ST_F_BIN:
1736 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1737 break;
1738 case ST_F_BOUT:
1739 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1740 break;
1741 case ST_F_DREQ:
1742 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1743 break;
1744 case ST_F_DRESP:
1745 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1746 break;
1747 case ST_F_EREQ:
1748 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1749 break;
1750 case ST_F_DCON:
1751 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1752 break;
1753 case ST_F_DSES:
1754 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1755 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001756 case ST_F_STATUS: {
1757 const char *state;
1758
1759 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1760 state = "STOP";
1761 else if (px->flags & PR_FL_PAUSED)
1762 state = "PAUSED";
1763 else
1764 state = "OPEN";
1765 metric = mkf_str(FO_STATUS, state);
William Dauchy0ef54392021-01-17 18:27:45 +01001766 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001767 }
William Dauchy0ef54392021-01-17 18:27:45 +01001768 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001769 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001770 break;
1771 case ST_F_IID:
1772 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1773 break;
1774 case ST_F_SID:
1775 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1776 break;
1777 case ST_F_TYPE:
1778 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1779 break;
1780 case ST_F_RATE:
1781 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1782 break;
1783 case ST_F_RATE_LIM:
1784 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1785 break;
1786 case ST_F_RATE_MAX:
1787 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1788 break;
1789 case ST_F_WREW:
1790 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1791 break;
1792 case ST_F_EINT:
1793 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1794 break;
1795 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001796 if (px->mode == PR_MODE_HTTP)
1797 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001798 break;
1799 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001800 if (px->mode == PR_MODE_HTTP)
1801 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001802 break;
1803 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001804 if (px->mode == PR_MODE_HTTP)
1805 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001806 break;
1807 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001808 if (px->mode == PR_MODE_HTTP)
1809 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001810 break;
1811 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001812 if (px->mode == PR_MODE_HTTP)
1813 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001814 break;
1815 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001816 if (px->mode == PR_MODE_HTTP)
1817 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001818 break;
1819 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001820 if (px->mode == PR_MODE_HTTP)
1821 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001822 break;
1823 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001824 if (px->mode == PR_MODE_HTTP)
1825 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001826 break;
1827 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001828 if (px->mode == PR_MODE_HTTP)
1829 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001830 break;
1831 case ST_F_REQ_RATE:
1832 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1833 break;
1834 case ST_F_REQ_RATE_MAX:
1835 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1836 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001837 case ST_F_REQ_TOT: {
1838 int i;
1839 uint64_t total_req;
1840 size_t nb_reqs =
1841 sizeof(px->fe_counters.p.http.cum_req) / sizeof(*px->fe_counters.p.http.cum_req);
1842
1843 total_req = 0;
1844 for (i = 0; i < nb_reqs; i++)
1845 total_req += px->fe_counters.p.http.cum_req[i];
1846 metric = mkf_u64(FN_COUNTER, total_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001847 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001848 }
William Dauchy0ef54392021-01-17 18:27:45 +01001849 case ST_F_COMP_IN:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001850 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001851 break;
1852 case ST_F_COMP_OUT:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001853 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001854 break;
1855 case ST_F_COMP_BYP:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001856 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001857 break;
1858 case ST_F_COMP_RSP:
1859 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1860 break;
1861 case ST_F_CONN_RATE:
1862 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1863 break;
1864 case ST_F_CONN_RATE_MAX:
1865 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1866 break;
1867 case ST_F_CONN_TOT:
1868 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1869 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001870 case ST_F_SESS_OTHER: {
1871 int i;
1872 uint64_t total_sess;
1873 size_t nb_sess =
1874 sizeof(px->fe_counters.cum_sess_ver) / sizeof(*px->fe_counters.cum_sess_ver);
1875
1876 total_sess = px->fe_counters.cum_sess;
1877 for (i = 0; i < nb_sess; i++)
1878 total_sess -= px->fe_counters.cum_sess_ver[i];
Frédéric Lécailled97d1d72023-02-06 09:23:56 +01001879 total_sess = (int64_t)total_sess < 0 ? 0 : total_sess;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001880 metric = mkf_u64(FN_COUNTER, total_sess);
1881 break;
1882 }
1883 case ST_F_H1SESS:
1884 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[0]);
1885 break;
1886 case ST_F_H2SESS:
1887 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[1]);
1888 break;
1889 case ST_F_H3SESS:
1890 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[2]);
1891 break;
1892 case ST_F_REQ_OTHER:
1893 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[0]);
1894 break;
1895 case ST_F_H1REQ:
1896 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[1]);
1897 break;
1898 case ST_F_H2REQ:
1899 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[2]);
1900 break;
1901 case ST_F_H3REQ:
1902 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[3]);
1903 break;
William Dauchy0ef54392021-01-17 18:27:45 +01001904 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001905 /* not used for frontends. If a specific metric
1906 * is requested, return an error. Otherwise continue.
1907 */
1908 if (selected_field != NULL)
1909 return 0;
1910 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001911 }
1912 stats[current_field] = metric;
1913 if (selected_field != NULL)
1914 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001915 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001916 return 1;
1917}
1918
Christopher Fauleta8b76842022-12-16 15:08:36 +01001919/* Dumps a frontend's line to the local trash buffer for the current proxy <px>
1920 * and uses the state from stream connector <sc>. The caller is responsible for
1921 * clearing the local trash buffer if needed. Returns non-zero if it emits
1922 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001923 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001924static int stats_dump_fe_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001925{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001926 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001927 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001928 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1929 struct stats_module *mod;
1930 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001931
1932 if (!(px->cap & PR_CAP_FE))
1933 return 0;
1934
Willy Tarreau91cefca2022-05-03 17:08:29 +02001935 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_FE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001936 return 0;
1937
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001938 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1939
William Dauchy0ef54392021-01-17 18:27:45 +01001940 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001941 return 0;
1942
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001943 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1944 void *counters;
1945
1946 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1947 stats_count += mod->stats_count;
1948 continue;
1949 }
1950
1951 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1952 mod->fill_stats(counters, stats + stats_count);
1953 stats_count += mod->stats_count;
1954 }
1955
1956 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001957}
1958
William Dauchy655e14e2021-02-14 23:22:54 +01001959/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1960 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1961 * this length is less then this value, the function returns 0, otherwise, it
1962 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1963 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001964 */
1965int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001966 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001967{
William Dauchy655e14e2021-02-14 23:22:54 +01001968 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001969 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001970
1971 if (len < ST_F_TOTAL_FIELDS)
1972 return 0;
1973
1974 if (!l->counters)
1975 return 0;
1976
1977 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001978
William Dauchy655e14e2021-02-14 23:22:54 +01001979 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1980 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001981
William Dauchy655e14e2021-02-14 23:22:54 +01001982 switch (current_field) {
1983 case ST_F_PXNAME:
1984 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1985 break;
1986 case ST_F_SVNAME:
1987 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1988 break;
1989 case ST_F_MODE:
1990 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1991 break;
1992 case ST_F_SCUR:
1993 metric = mkf_u32(0, l->nbconn);
1994 break;
1995 case ST_F_SMAX:
1996 metric = mkf_u32(FN_MAX, l->counters->conn_max);
1997 break;
1998 case ST_F_SLIM:
Willy Tarreau758c69d2023-01-12 18:59:37 +01001999 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->bind_conf->maxconn);
William Dauchy655e14e2021-02-14 23:22:54 +01002000 break;
2001 case ST_F_STOT:
2002 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
2003 break;
2004 case ST_F_BIN:
2005 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
2006 break;
2007 case ST_F_BOUT:
2008 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
2009 break;
2010 case ST_F_DREQ:
2011 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
2012 break;
2013 case ST_F_DRESP:
2014 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
2015 break;
2016 case ST_F_EREQ:
2017 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
2018 break;
2019 case ST_F_DCON:
2020 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
2021 break;
2022 case ST_F_DSES:
2023 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
2024 break;
2025 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01002026 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01002027 break;
2028 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002029 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01002030 break;
2031 case ST_F_IID:
2032 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2033 break;
2034 case ST_F_SID:
2035 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
2036 break;
2037 case ST_F_TYPE:
2038 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
2039 break;
2040 case ST_F_WREW:
2041 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
2042 break;
2043 case ST_F_EINT:
2044 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
2045 break;
2046 case ST_F_ADDR:
2047 if (flags & STAT_SHLGNDS) {
2048 char str[INET6_ADDRSTRLEN];
2049 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01002050
William Dauchy655e14e2021-02-14 23:22:54 +01002051 port = get_host_port(&l->rx.addr);
2052 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
2053 case AF_INET:
2054 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2055 chunk_appendf(out, "%s:%d", str, port);
2056 break;
2057 case AF_INET6:
2058 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2059 chunk_appendf(out, "[%s]:%d", str, port);
2060 break;
2061 case AF_UNIX:
2062 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2063 break;
2064 case -1:
2065 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2066 chunk_strcat(out, strerror(errno));
2067 break;
2068 default: /* address family not supported */
2069 break;
2070 }
2071 }
2072 break;
2073 default:
2074 /* not used for listen. If a specific metric
2075 * is requested, return an error. Otherwise continue.
2076 */
2077 if (selected_field != NULL)
2078 return 0;
2079 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002080 }
William Dauchy655e14e2021-02-14 23:22:54 +01002081 stats[current_field] = metric;
2082 if (selected_field != NULL)
2083 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002084 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002085 return 1;
2086}
2087
Christopher Fauleta8b76842022-12-16 15:08:36 +01002088/* Dumps a line for listener <l> and proxy <px> to the local trash buffer and
2089 * uses the state from stream connector <sc>. The caller is responsible for
2090 * clearing the local trash buffer if needed. Returns non-zero if it emits
2091 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002092 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002093static int stats_dump_li_stats(struct stconn *sc, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01002094{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002095 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002096 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002097 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2098 struct stats_module *mod;
2099 size_t stats_count = ST_F_TOTAL_FIELDS;
2100
2101 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002102
Willy Tarreau91cefca2022-05-03 17:08:29 +02002103 if (!stats_fill_li_stats(px, l, ctx->flags, stats,
William Dauchy655e14e2021-02-14 23:22:54 +01002104 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002105 return 0;
2106
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002107 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2108 void *counters;
2109
2110 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
2111 stats_count += mod->stats_count;
2112 continue;
2113 }
2114
2115 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2116 mod->fill_stats(counters, stats + stats_count);
2117 stats_count += mod->stats_count;
2118 }
2119
2120 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002121}
2122
2123enum srv_stats_state {
2124 SRV_STATS_STATE_DOWN = 0,
2125 SRV_STATS_STATE_DOWN_AGENT,
2126 SRV_STATS_STATE_GOING_UP,
2127 SRV_STATS_STATE_UP_GOING_DOWN,
2128 SRV_STATS_STATE_UP,
2129 SRV_STATS_STATE_NOLB_GOING_DOWN,
2130 SRV_STATS_STATE_NOLB,
2131 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2132 SRV_STATS_STATE_DRAIN,
2133 SRV_STATS_STATE_DRAIN_AGENT,
2134 SRV_STATS_STATE_NO_CHECK,
2135
2136 SRV_STATS_STATE_COUNT, /* Must be last */
2137};
2138
2139static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2140 [SRV_STATS_STATE_DOWN] = "DOWN",
2141 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2142 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2143 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2144 [SRV_STATS_STATE_UP] = "UP",
2145 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2146 [SRV_STATS_STATE_NOLB] = "NOLB",
2147 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2148 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2149 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2150 [SRV_STATS_STATE_NO_CHECK] = "no check"
2151};
2152
William Dauchyd3a9a492021-01-25 17:29:03 +01002153/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002154 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002155static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2156 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002157{
Emeric Brun52a91d32017-08-31 14:41:55 +02002158 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002159 if ((ref->check.state & CHK_ST_ENABLED) &&
2160 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002161 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002162 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002163 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002164 }
2165
Emeric Brun52a91d32017-08-31 14:41:55 +02002166 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002167 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002168 *state = SRV_STATS_STATE_DRAIN_AGENT;
2169 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2170 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002171 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002172 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002173 }
2174
William Dauchyd3a9a492021-01-25 17:29:03 +01002175 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2176 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002177 }
2178 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002179 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002180 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2181 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002182 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002183 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002184 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002185 }
2186 }
2187 else { /* stopped */
2188 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002189 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002190 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002191 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002192 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002193 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002194 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002195 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002196 }
2197 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002198}
William Lallemand74c24fb2016-11-21 17:18:36 +01002199
William Dauchyd3a9a492021-01-25 17:29:03 +01002200/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2201 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2202 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2203 * this value, or if the selected field is not implemented for servers, the
2204 * function returns 0, otherwise, it returns 1. <flags> can take the value
2205 * STAT_SHLGNDS.
2206 */
2207int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2208 struct field *stats, int len,
2209 enum stat_field *selected_field)
2210{
2211 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2212 struct server *via = sv->track ? sv->track : sv;
2213 struct server *ref = via;
2214 enum srv_stats_state state = 0;
2215 char str[INET6_ADDRSTRLEN];
2216 struct buffer *out = get_trash_chunk();
2217 char *fld_status;
2218 long long srv_samples_counter;
2219 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002220
William Dauchyd3a9a492021-01-25 17:29:03 +01002221 if (len < ST_F_TOTAL_FIELDS)
2222 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002223
William Dauchyd3a9a492021-01-25 17:29:03 +01002224 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002225
William Dauchyd3a9a492021-01-25 17:29:03 +01002226 /* compute state for later use */
2227 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2228 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2229 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2230 /* we have "via" which is the tracked server as described in the configuration,
2231 * and "ref" which is the checked server and the end of the chain.
2232 */
2233 while (ref->track)
2234 ref = ref->track;
2235 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002236 }
2237
William Dauchyd3a9a492021-01-25 17:29:03 +01002238 /* compue time values for later use */
2239 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2240 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2241 *selected_field == ST_F_TTIME) {
2242 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2243 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2244 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002245 }
2246
William Dauchyd3a9a492021-01-25 17:29:03 +01002247 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2248 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002249
William Dauchyd3a9a492021-01-25 17:29:03 +01002250 switch (current_field) {
2251 case ST_F_PXNAME:
2252 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2253 break;
2254 case ST_F_SVNAME:
2255 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2256 break;
2257 case ST_F_MODE:
2258 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002259 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002260 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002261 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002262 break;
2263 case ST_F_QMAX:
2264 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2265 break;
2266 case ST_F_SCUR:
2267 metric = mkf_u32(0, sv->cur_sess);
2268 break;
2269 case ST_F_SMAX:
2270 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2271 break;
2272 case ST_F_SLIM:
2273 if (sv->maxconn)
2274 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2275 break;
2276 case ST_F_SRV_ICUR:
2277 metric = mkf_u32(0, sv->curr_idle_conns);
2278 break;
2279 case ST_F_SRV_ILIM:
2280 if (sv->max_idle_conns != -1)
2281 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2282 break;
2283 case ST_F_STOT:
2284 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2285 break;
2286 case ST_F_BIN:
2287 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2288 break;
2289 case ST_F_BOUT:
2290 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2291 break;
2292 case ST_F_DRESP:
2293 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2294 break;
2295 case ST_F_ECON:
2296 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2297 break;
2298 case ST_F_ERESP:
2299 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2300 break;
2301 case ST_F_WRETR:
2302 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2303 break;
2304 case ST_F_WREDIS:
2305 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2306 break;
2307 case ST_F_WREW:
2308 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2309 break;
2310 case ST_F_EINT:
2311 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2312 break;
2313 case ST_F_CONNECT:
2314 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2315 break;
2316 case ST_F_REUSE:
2317 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2318 break;
2319 case ST_F_IDLE_CONN_CUR:
2320 metric = mkf_u32(0, sv->curr_idle_nb);
2321 break;
2322 case ST_F_SAFE_CONN_CUR:
2323 metric = mkf_u32(0, sv->curr_safe_nb);
2324 break;
2325 case ST_F_USED_CONN_CUR:
2326 metric = mkf_u32(0, sv->curr_used_conns);
2327 break;
2328 case ST_F_NEED_CONN_EST:
2329 metric = mkf_u32(0, sv->est_need_conns);
2330 break;
2331 case ST_F_STATUS:
2332 fld_status = chunk_newstr(out);
2333 if (sv->cur_admin & SRV_ADMF_RMAINT)
2334 chunk_appendf(out, "MAINT (resolution)");
2335 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2336 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2337 else if (sv->cur_admin & SRV_ADMF_MAINT)
2338 chunk_appendf(out, "MAINT");
2339 else
2340 chunk_appendf(out,
2341 srv_hlt_st[state],
2342 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2343 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002344
William Dauchyd3a9a492021-01-25 17:29:03 +01002345 metric = mkf_str(FO_STATUS, fld_status);
2346 break;
2347 case ST_F_LASTCHG:
2348 metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
2349 break;
2350 case ST_F_WEIGHT:
2351 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2352 break;
2353 case ST_F_UWEIGHT:
2354 metric = mkf_u32(FN_AVG, sv->uweight);
2355 break;
2356 case ST_F_ACT:
2357 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2358 break;
2359 case ST_F_BCK:
2360 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2361 break;
2362 case ST_F_CHKFAIL:
2363 if (sv->check.state & CHK_ST_ENABLED)
2364 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2365 break;
2366 case ST_F_CHKDOWN:
2367 if (sv->check.state & CHK_ST_ENABLED)
2368 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2369 break;
2370 case ST_F_DOWNTIME:
2371 if (sv->check.state & CHK_ST_ENABLED)
2372 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2373 break;
2374 case ST_F_QLIMIT:
2375 if (sv->maxqueue)
2376 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2377 break;
2378 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002379 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002380 break;
2381 case ST_F_IID:
2382 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2383 break;
2384 case ST_F_SID:
2385 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2386 break;
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01002387 case ST_F_SRID:
2388 metric = mkf_u32(FN_COUNTER, sv->rid);
2389 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002390 case ST_F_THROTTLE:
2391 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2392 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2393 break;
2394 case ST_F_LBTOT:
2395 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2396 break;
2397 case ST_F_TRACKED:
2398 if (sv->track) {
2399 char *fld_track = chunk_newstr(out);
2400 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2401 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2402 }
2403 break;
2404 case ST_F_TYPE:
2405 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2406 break;
2407 case ST_F_RATE:
2408 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2409 break;
2410 case ST_F_RATE_MAX:
2411 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2412 break;
2413 case ST_F_CHECK_STATUS:
2414 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2415 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002416
William Dauchyd3a9a492021-01-25 17:29:03 +01002417 fld_chksts = chunk_newstr(out);
2418 chunk_strcat(out, "* "); // for check in progress
2419 chunk_strcat(out, get_check_status_info(sv->check.status));
2420 if (!(sv->check.state & CHK_ST_INPROGRESS))
2421 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002422 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002423 }
2424 break;
2425 case ST_F_CHECK_CODE:
2426 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2427 sv->check.status >= HCHK_STATUS_L57DATA)
2428 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2429 break;
2430 case ST_F_CHECK_DURATION:
2431 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2432 sv->check.status >= HCHK_STATUS_CHECKED)
Aurelien DARRAGONb118f2f2022-12-07 14:52:10 +01002433 metric = mkf_u64(FN_DURATION, MAX(sv->check.duration, 0));
William Dauchyd3a9a492021-01-25 17:29:03 +01002434 break;
2435 case ST_F_CHECK_DESC:
2436 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2437 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2438 break;
2439 case ST_F_LAST_CHK:
2440 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2441 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2442 break;
2443 case ST_F_CHECK_RISE:
2444 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2445 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2446 break;
2447 case ST_F_CHECK_FALL:
2448 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2449 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2450 break;
2451 case ST_F_CHECK_HEALTH:
2452 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2453 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2454 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002455 case ST_F_AGENT_STATUS:
2456 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2457 const char *fld_chksts;
2458
2459 fld_chksts = chunk_newstr(out);
2460 chunk_strcat(out, "* "); // for check in progress
2461 chunk_strcat(out, get_check_status_info(sv->agent.status));
2462 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2463 fld_chksts += 2; // skip "* "
2464 metric = mkf_str(FN_OUTPUT, fld_chksts);
2465 }
2466 break;
2467 case ST_F_AGENT_CODE:
2468 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2469 (sv->agent.status >= HCHK_STATUS_L57DATA))
2470 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2471 break;
2472 case ST_F_AGENT_DURATION:
2473 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2474 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2475 break;
2476 case ST_F_AGENT_DESC:
2477 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2478 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2479 break;
2480 case ST_F_LAST_AGT:
2481 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2482 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2483 break;
2484 case ST_F_AGENT_RISE:
2485 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2486 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2487 break;
2488 case ST_F_AGENT_FALL:
2489 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2490 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2491 break;
2492 case ST_F_AGENT_HEALTH:
2493 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2494 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2495 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002496 case ST_F_REQ_TOT:
2497 if (px->mode == PR_MODE_HTTP)
2498 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2499 break;
2500 case ST_F_HRSP_1XX:
2501 if (px->mode == PR_MODE_HTTP)
2502 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2503 break;
2504 case ST_F_HRSP_2XX:
2505 if (px->mode == PR_MODE_HTTP)
2506 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2507 break;
2508 case ST_F_HRSP_3XX:
2509 if (px->mode == PR_MODE_HTTP)
2510 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2511 break;
2512 case ST_F_HRSP_4XX:
2513 if (px->mode == PR_MODE_HTTP)
2514 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2515 break;
2516 case ST_F_HRSP_5XX:
2517 if (px->mode == PR_MODE_HTTP)
2518 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2519 break;
2520 case ST_F_HRSP_OTHER:
2521 if (px->mode == PR_MODE_HTTP)
2522 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2523 break;
2524 case ST_F_HANAFAIL:
2525 if (ref->observe)
2526 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2527 break;
2528 case ST_F_CLI_ABRT:
2529 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2530 break;
2531 case ST_F_SRV_ABRT:
2532 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2533 break;
2534 case ST_F_LASTSESS:
2535 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2536 break;
2537 case ST_F_QTIME:
2538 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2539 break;
2540 case ST_F_CTIME:
2541 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2542 break;
2543 case ST_F_RTIME:
2544 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2545 break;
2546 case ST_F_TTIME:
2547 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2548 break;
2549 case ST_F_QT_MAX:
2550 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2551 break;
2552 case ST_F_CT_MAX:
2553 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2554 break;
2555 case ST_F_RT_MAX:
2556 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2557 break;
2558 case ST_F_TT_MAX:
2559 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2560 break;
2561 case ST_F_ADDR:
2562 if (flags & STAT_SHLGNDS) {
2563 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2564 case AF_INET:
2565 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2566 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2567 break;
2568 case AF_INET6:
2569 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2570 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2571 break;
2572 case AF_UNIX:
2573 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2574 break;
2575 case -1:
2576 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2577 chunk_strcat(out, strerror(errno));
2578 break;
2579 default: /* address family not supported */
2580 break;
2581 }
2582 }
2583 break;
2584 case ST_F_COOKIE:
2585 if (flags & STAT_SHLGNDS && sv->cookie)
2586 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2587 break;
2588 default:
2589 /* not used for servers. If a specific metric
2590 * is requested, return an error. Otherwise continue.
2591 */
2592 if (selected_field != NULL)
2593 return 0;
2594 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002595 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002596 stats[current_field] = metric;
2597 if (selected_field != NULL)
2598 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002599 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002600 return 1;
2601}
2602
Christopher Fauleta8b76842022-12-16 15:08:36 +01002603/* Dumps a line for server <sv> and proxy <px> to the local trash vbuffer and
2604 * uses the state from stream connector <sc>, and server state <state>. The
2605 * caller is responsible for clearing the local trash buffer if needed. Returns
2606 * non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002607 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002608static int stats_dump_sv_stats(struct stconn *sc, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002609{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002610 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002611 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002612 struct stats_module *mod;
2613 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2614 size_t stats_count = ST_F_TOTAL_FIELDS;
2615
2616 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002617
Willy Tarreau91cefca2022-05-03 17:08:29 +02002618 if (!stats_fill_sv_stats(px, sv, ctx->flags, stats,
William Dauchyd3a9a492021-01-25 17:29:03 +01002619 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002620 return 0;
2621
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002622 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2623 void *counters;
2624
2625 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2626 continue;
2627
2628 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2629 stats_count += mod->stats_count;
2630 continue;
2631 }
2632
2633 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2634 mod->fill_stats(counters, stats + stats_count);
2635 stats_count += mod->stats_count;
2636 }
2637
2638 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002639}
2640
William Dauchyda3b4662021-01-25 17:29:01 +01002641/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002642 */
William Dauchyda3b4662021-01-25 17:29:01 +01002643static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002644{
William Dauchyda3b4662021-01-25 17:29:01 +01002645 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002646 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002647
William Dauchyda3b4662021-01-25 17:29:01 +01002648 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002649 for (srv = px->srv; srv; srv = srv->next) {
2650 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002651 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002652 if (srv_currently_usable(srv) &&
2653 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002654 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002655 }
William Dauchyda3b4662021-01-25 17:29:01 +01002656 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002657 }
2658
Willy Tarreaubd715102020-10-23 22:44:30 +02002659 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2660 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002661 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002662 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2663
William Dauchyda3b4662021-01-25 17:29:01 +01002664 /* use tmp variable then assign result to make gcc happy */
2665 *nbup = nbup_tmp;
2666 *nbsrv = nbsrv_tmp;
2667 *totuw = totuw_tmp;
2668}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002669
William Dauchyda3b4662021-01-25 17:29:01 +01002670/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2671 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2672 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2673 * this value, or if the selected field is not implemented for backends, the
2674 * function returns 0, otherwise, it returns 1. <flags> can take the value
2675 * STAT_SHLGNDS.
2676 */
2677int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2678 enum stat_field *selected_field)
2679{
2680 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2681 long long be_samples_counter;
2682 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2683 struct buffer *out = get_trash_chunk();
2684 int nbup, nbsrv, totuw;
2685 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002686
William Dauchyda3b4662021-01-25 17:29:01 +01002687 if (len < ST_F_TOTAL_FIELDS)
2688 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002689
William Dauchyda3b4662021-01-25 17:29:01 +01002690 nbup = nbsrv = totuw = 0;
2691 /* some srv values compute for later if we either select all fields or
2692 * need them for one of the mentioned ones */
2693 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2694 *selected_field == ST_F_UWEIGHT)
2695 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002696
William Dauchyda3b4662021-01-25 17:29:01 +01002697 /* same here but specific to time fields */
2698 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2699 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2700 *selected_field == ST_F_TTIME) {
2701 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2702 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2703 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002704 }
2705
William Dauchyda3b4662021-01-25 17:29:01 +01002706 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2707 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002708
William Dauchyda3b4662021-01-25 17:29:01 +01002709 switch (current_field) {
2710 case ST_F_PXNAME:
2711 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2712 break;
2713 case ST_F_SVNAME:
2714 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2715 break;
2716 case ST_F_MODE:
2717 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2718 break;
2719 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002720 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002721 break;
2722 case ST_F_QMAX:
2723 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2724 break;
2725 case ST_F_SCUR:
2726 metric = mkf_u32(0, px->beconn);
2727 break;
2728 case ST_F_SMAX:
2729 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2730 break;
2731 case ST_F_SLIM:
2732 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2733 break;
2734 case ST_F_STOT:
2735 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2736 break;
2737 case ST_F_BIN:
2738 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2739 break;
2740 case ST_F_BOUT:
2741 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2742 break;
2743 case ST_F_DREQ:
2744 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2745 break;
2746 case ST_F_DRESP:
2747 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2748 break;
2749 case ST_F_ECON:
2750 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2751 break;
2752 case ST_F_ERESP:
2753 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2754 break;
2755 case ST_F_WRETR:
2756 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2757 break;
2758 case ST_F_WREDIS:
2759 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2760 break;
2761 case ST_F_WREW:
2762 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2763 break;
2764 case ST_F_EINT:
2765 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2766 break;
2767 case ST_F_CONNECT:
2768 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2769 break;
2770 case ST_F_REUSE:
2771 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2772 break;
2773 case ST_F_STATUS:
2774 fld = chunk_newstr(out);
2775 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2776 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2777 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2778 metric = mkf_str(FO_STATUS, fld);
2779 break;
Cedric Paillet7d6644e2022-12-08 09:17:00 +00002780 case ST_F_AGG_SRV_CHECK_STATUS: // DEPRECATED
2781 case ST_F_AGG_SRV_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +01002782 metric = mkf_u32(FN_GAUGE, 0);
2783 break;
Cedric Paillete06e31e2022-12-08 09:17:01 +00002784 case ST_F_AGG_CHECK_STATUS:
2785 metric = mkf_u32(FN_GAUGE, 0);
2786 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002787 case ST_F_WEIGHT:
2788 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2789 break;
2790 case ST_F_UWEIGHT:
2791 metric = mkf_u32(FN_AVG, totuw);
2792 break;
2793 case ST_F_ACT:
2794 metric = mkf_u32(0, px->srv_act);
2795 break;
2796 case ST_F_BCK:
2797 metric = mkf_u32(0, px->srv_bck);
2798 break;
2799 case ST_F_CHKDOWN:
2800 metric = mkf_u64(FN_COUNTER, px->down_trans);
2801 break;
2802 case ST_F_LASTCHG:
2803 metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
2804 break;
2805 case ST_F_DOWNTIME:
2806 if (px->srv)
2807 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2808 break;
2809 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002810 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002811 break;
2812 case ST_F_IID:
2813 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2814 break;
2815 case ST_F_SID:
2816 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2817 break;
2818 case ST_F_LBTOT:
2819 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2820 break;
2821 case ST_F_TYPE:
2822 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2823 break;
2824 case ST_F_RATE:
2825 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2826 break;
2827 case ST_F_RATE_MAX:
2828 metric = mkf_u32(0, px->be_counters.sps_max);
2829 break;
2830 case ST_F_COOKIE:
2831 if (flags & STAT_SHLGNDS && px->cookie_name)
2832 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2833 break;
2834 case ST_F_ALGO:
2835 if (flags & STAT_SHLGNDS)
2836 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2837 break;
2838 case ST_F_REQ_TOT:
2839 if (px->mode == PR_MODE_HTTP)
2840 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2841 break;
2842 case ST_F_HRSP_1XX:
2843 if (px->mode == PR_MODE_HTTP)
2844 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2845 break;
2846 case ST_F_HRSP_2XX:
2847 if (px->mode == PR_MODE_HTTP)
2848 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2849 break;
2850 case ST_F_HRSP_3XX:
2851 if (px->mode == PR_MODE_HTTP)
2852 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2853 break;
2854 case ST_F_HRSP_4XX:
2855 if (px->mode == PR_MODE_HTTP)
2856 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2857 break;
2858 case ST_F_HRSP_5XX:
2859 if (px->mode == PR_MODE_HTTP)
2860 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2861 break;
2862 case ST_F_HRSP_OTHER:
2863 if (px->mode == PR_MODE_HTTP)
2864 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2865 break;
2866 case ST_F_CACHE_LOOKUPS:
2867 if (px->mode == PR_MODE_HTTP)
2868 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2869 break;
2870 case ST_F_CACHE_HITS:
2871 if (px->mode == PR_MODE_HTTP)
2872 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2873 break;
2874 case ST_F_CLI_ABRT:
2875 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2876 break;
2877 case ST_F_SRV_ABRT:
2878 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2879 break;
2880 case ST_F_COMP_IN:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002881 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002882 break;
2883 case ST_F_COMP_OUT:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002884 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002885 break;
2886 case ST_F_COMP_BYP:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002887 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002888 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
Willy Tarreaufc458ec2023-04-07 18:11:39 +02002997 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
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;
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003145 int px_st = ctx->px_st;
William Lallemand74c24fb2016-11-21 17:18:36 +01003146
Christopher Fauleta8b76842022-12-16 15:08:36 +01003147 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003148more:
3149 current_field = ctx->field;
William Lallemand74c24fb2016-11-21 17:18:36 +01003150
Willy Tarreau91cefca2022-05-03 17:08:29 +02003151 switch (ctx->px_st) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003152 case STAT_PX_ST_INIT:
3153 /* we are on a new proxy */
3154 if (uri && uri->scope) {
3155 /* we have a limited scope, we have to check the proxy name */
3156 struct stat_scope *scope;
3157 int len;
3158
3159 len = strlen(px->id);
3160 scope = uri->scope;
3161
3162 while (scope) {
3163 /* match exact proxy name */
3164 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3165 break;
3166
3167 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003168 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003169 break;
3170 scope = scope->next;
3171 }
3172
3173 /* proxy name not found : don't dump anything */
3174 if (scope == NULL)
3175 return 1;
3176 }
3177
3178 /* if the user has requested a limited output and the proxy
3179 * name does not match, skip it.
3180 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003181 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003182 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003183
Willy Tarreau91cefca2022-05-03 17:08:29 +02003184 if (strnistr(px->id, strlen(px->id), scope_ptr, ctx->scope_len) == NULL)
Christopher Fauleted7a0662019-01-14 11:07:34 +01003185 return 1;
3186 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003187
Willy Tarreau91cefca2022-05-03 17:08:29 +02003188 if ((ctx->flags & STAT_BOUND) &&
3189 (ctx->iid != -1) &&
3190 (px->uuid != ctx->iid))
William Lallemand74c24fb2016-11-21 17:18:36 +01003191 return 1;
3192
Willy Tarreau91cefca2022-05-03 17:08:29 +02003193 ctx->px_st = STAT_PX_ST_TH;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003194 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003195
3196 case STAT_PX_ST_TH:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003197 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003198 stats_dump_html_px_hdr(sc, px);
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003199 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003200 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003201 }
3202
Willy Tarreau91cefca2022-05-03 17:08:29 +02003203 ctx->px_st = STAT_PX_ST_FE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003204 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003205
3206 case STAT_PX_ST_FE:
3207 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003208 if (stats_dump_fe_stats(sc, px)) {
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003209 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003210 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003211 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003212 if (ctx->field)
3213 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003214 }
3215
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003216 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003217 ctx->obj2 = px->conf.listeners.n;
3218 ctx->px_st = STAT_PX_ST_LI;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003219 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003220
3221 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003222 /* obj2 points to listeners list as initialized above */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003223 for (; ctx->obj2 != &px->conf.listeners; ctx->obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003224 if (htx) {
3225 if (htx_almost_full(htx))
3226 goto full;
3227 }
3228 else {
3229 if (buffer_almost_full(&rep->buf))
3230 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003231 }
3232
Willy Tarreau91cefca2022-05-03 17:08:29 +02003233 l = LIST_ELEM(ctx->obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003234 if (!l->counters)
3235 continue;
3236
Willy Tarreau91cefca2022-05-03 17:08:29 +02003237 if (ctx->flags & STAT_BOUND) {
3238 if (!(ctx->type & (1 << STATS_TYPE_SO)))
William Lallemand74c24fb2016-11-21 17:18:36 +01003239 break;
3240
Willy Tarreau91cefca2022-05-03 17:08:29 +02003241 if (ctx->sid != -1 && l->luid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003242 continue;
3243 }
3244
3245 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003246 if (stats_dump_li_stats(sc, px, l)) {
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003247 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003248 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003249 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003250 if (ctx->field)
3251 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003252 }
Aurelien DARRAGON9b07d4f2023-02-03 11:43:05 +01003253 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003254 }
3255
Willy Tarreau91cefca2022-05-03 17:08:29 +02003256 ctx->obj2 = px->srv; /* may be NULL */
3257 ctx->px_st = STAT_PX_ST_SV;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003258 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003259
3260 case STAT_PX_ST_SV:
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003261 /* check for dump resumption */
3262 if (px_st == STAT_PX_ST_SV) {
3263 struct server *cur = ctx->obj2;
3264
3265 /* re-entrant dump */
3266 BUG_ON(!cur);
3267 if (cur->flags & SRV_F_DELETED) {
3268 /* the server could have been marked as deleted
3269 * between two dumping attempts, skip it.
3270 */
3271 cur = cur->next;
3272 }
3273 srv_drop(ctx->obj2); /* drop old srv taken on last dumping attempt */
3274 ctx->obj2 = cur; /* could be NULL */
3275 /* back to normal */
3276 }
3277
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003278 /* obj2 points to servers list as initialized above.
3279 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003280 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003281 * Temporarily increment its refcount to prevent its
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003282 * anticipated cleaning. Call srv_drop() to release it.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003283 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003284 for (; ctx->obj2 != NULL;
3285 ctx->obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003286
Willy Tarreau91cefca2022-05-03 17:08:29 +02003287 sv = ctx->obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003288 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003289
Christopher Fauletef779222018-10-31 08:47:01 +01003290 if (htx) {
3291 if (htx_almost_full(htx))
3292 goto full;
3293 }
3294 else {
3295 if (buffer_almost_full(&rep->buf))
3296 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003297 }
3298
Willy Tarreau91cefca2022-05-03 17:08:29 +02003299 if (ctx->flags & STAT_BOUND) {
3300 if (!(ctx->type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003301 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003302 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003303 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003304
Willy Tarreau91cefca2022-05-03 17:08:29 +02003305 if (ctx->sid != -1 && sv->puid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003306 continue;
3307 }
3308
Willy Tarreau3e320362020-10-23 17:28:57 +02003309 /* do not report disabled servers */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003310 if (ctx->flags & STAT_HIDE_MAINT &&
Willy Tarreau3e320362020-10-23 17:28:57 +02003311 sv->cur_admin & SRV_ADMF_MAINT) {
3312 continue;
3313 }
3314
William Lallemand74c24fb2016-11-21 17:18:36 +01003315 svs = sv;
3316 while (svs->track)
3317 svs = svs->track;
3318
3319 /* do not report servers which are DOWN and not changing state */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003320 if ((ctx->flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003321 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3322 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003323 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3324 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3325 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3326 continue;
3327 }
3328
Willy Tarreaucaff6312022-05-27 10:17:46 +02003329 if (stats_dump_sv_stats(sc, px, sv)) {
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003330 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003331 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003332 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003333 if (ctx->field)
3334 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003335 }
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003336 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003337 } /* for sv */
3338
Willy Tarreau91cefca2022-05-03 17:08:29 +02003339 ctx->px_st = STAT_PX_ST_BE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003340 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003341
3342 case STAT_PX_ST_BE:
3343 /* print the backend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003344 if (stats_dump_be_stats(sc, px)) {
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003345 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003346 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003347 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003348 if (ctx->field)
3349 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003350 }
3351
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003352 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003353 ctx->px_st = STAT_PX_ST_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003354 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003355
3356 case STAT_PX_ST_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003357 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003358 stats_dump_html_px_end(sc, px);
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003359 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003360 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003361 }
3362
Willy Tarreau91cefca2022-05-03 17:08:29 +02003363 ctx->px_st = STAT_PX_ST_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003364 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003365
3366 case STAT_PX_ST_FIN:
3367 return 1;
3368
3369 default:
3370 /* unknown state, we should put an abort() here ! */
3371 return 1;
3372 }
Christopher Fauletef779222018-10-31 08:47:01 +01003373
3374 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003375 sc_need_room(sc);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003376 /* restore previous field */
3377 ctx->field = current_field;
Christopher Fauletef779222018-10-31 08:47:01 +01003378 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003379}
3380
Christopher Fauleta8b76842022-12-16 15:08:36 +01003381/* Dumps the HTTP stats head block to the local trash buffer for and uses the
3382 * per-uri parameters <uri>. The caller is responsible for clearing the local
3383 * trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003384 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003385static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003386{
Willy Tarreau91cefca2022-05-03 17:08:29 +02003387 struct show_stat_ctx *ctx = appctx->svcctx;
3388
William Lallemand74c24fb2016-11-21 17:18:36 +01003389 /* WARNING! This must fit in the first buffer !!! */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003390 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003391 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3392 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3393 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003394 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003395 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3396 "<style type=\"text/css\"><!--\n"
3397 "body {"
3398 " font-family: arial, helvetica, sans-serif;"
3399 " font-size: 12px;"
3400 " font-weight: normal;"
3401 " color: black;"
3402 " background: white;"
3403 "}\n"
3404 "th,td {"
3405 " font-size: 10px;"
3406 "}\n"
3407 "h1 {"
3408 " font-size: x-large;"
3409 " margin-bottom: 0.5em;"
3410 "}\n"
3411 "h2 {"
3412 " font-family: helvetica, arial;"
3413 " font-size: x-large;"
3414 " font-weight: bold;"
3415 " font-style: italic;"
3416 " color: #6020a0;"
3417 " margin-top: 0em;"
3418 " margin-bottom: 0em;"
3419 "}\n"
3420 "h3 {"
3421 " font-family: helvetica, arial;"
3422 " font-size: 16px;"
3423 " font-weight: bold;"
3424 " color: #b00040;"
3425 " background: #e8e8d0;"
3426 " margin-top: 0em;"
3427 " margin-bottom: 0em;"
3428 "}\n"
3429 "li {"
3430 " margin-top: 0.25em;"
3431 " margin-right: 2em;"
3432 "}\n"
3433 ".hr {margin-top: 0.25em;"
3434 " border-color: black;"
3435 " border-bottom-style: solid;"
3436 "}\n"
3437 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3438 ".total {background: #20D0D0;color: #ffff80;}\n"
3439 ".frontend {background: #e8e8d0;}\n"
3440 ".socket {background: #d0d0d0;}\n"
3441 ".backend {background: #e8e8d0;}\n"
3442 ".active_down {background: #ff9090;}\n"
3443 ".active_going_up {background: #ffd020;}\n"
3444 ".active_going_down {background: #ffffa0;}\n"
3445 ".active_up {background: #c0ffc0;}\n"
3446 ".active_nolb {background: #20a0ff;}\n"
3447 ".active_draining {background: #20a0FF;}\n"
3448 ".active_no_check {background: #e0e0e0;}\n"
3449 ".backup_down {background: #ff9090;}\n"
3450 ".backup_going_up {background: #ff80ff;}\n"
3451 ".backup_going_down {background: #c060ff;}\n"
3452 ".backup_up {background: #b0d0ff;}\n"
3453 ".backup_nolb {background: #90b0e0;}\n"
3454 ".backup_draining {background: #cc9900;}\n"
3455 ".backup_no_check {background: #e0e0e0;}\n"
3456 ".maintain {background: #c07820;}\n"
3457 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3458 "\n"
3459 "a.px:link {color: #ffff40; text-decoration: none;}"
3460 "a.px:visited {color: #ffff40; text-decoration: none;}"
3461 "a.px:hover {color: #ffffff; text-decoration: none;}"
3462 "a.lfsb:link {color: #000000; text-decoration: none;}"
3463 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3464 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3465 "\n"
3466 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3467 "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"
3468 "table.tbl td.ac { text-align: center;}\n"
3469 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3470 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3471 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3472 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3473 "\n"
3474 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3475 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3476 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3477 "table.det { border-collapse: collapse; border-style: none; }\n"
3478 "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"
3479 "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"
3480 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003481 "div.tips {\n"
3482 " display:block;\n"
3483 " visibility:hidden;\n"
3484 " z-index:2147483647;\n"
3485 " position:absolute;\n"
3486 " padding:2px 4px 3px;\n"
3487 " background:#f0f060; color:#000000;\n"
3488 " border:1px solid #7040c0;\n"
3489 " white-space:nowrap;\n"
3490 " font-style:normal;font-size:11px;font-weight:normal;\n"
3491 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3492 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3493 "}\n"
3494 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003495 "@media (prefers-color-scheme: dark) {\n"
3496 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3497 " h1 { color: #a265e0!important; }\n"
3498 " h2 { color: #a265e0; }\n"
3499 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3500 " a { color: #3391ff; }\n"
3501 " input { background-color: #2f3437; }\n"
3502 " .hr { border-color: #8c8273; }\n"
3503 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3504 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003505 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003506 " .backend {background: #2f3437;}\n"
3507 " .active_down {background: #760000;}\n"
3508 " .active_going_up {background: #b99200;}\n"
3509 " .active_going_down {background: #6c6c00;}\n"
3510 " .active_up {background: #165900;}\n"
3511 " .active_nolb {background: #006ab9;}\n"
3512 " .active_draining {background: #006ab9;}\n"
3513 " .active_no_check {background: #2a2d2f;}\n"
3514 " .backup_down {background: #760000;}\n"
3515 " .backup_going_up {background: #7f007f;}\n"
3516 " .backup_going_down {background: #580092;}\n"
3517 " .backup_up {background: #2e3234;}\n"
3518 " .backup_nolb {background: #1e3c6a;}\n"
3519 " .backup_draining {background: #a37a00;}\n"
3520 " .backup_no_check {background: #2a2d2f;}\n"
3521 " .maintain {background: #9a601a;}\n"
3522 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3523 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3524 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3525 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3526 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3527 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3528 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003529 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003530 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3531 " table.tbl th { border-color: #808080; }\n"
3532 " table.tbl td { border-color: #808080; }\n"
3533 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3534 " div.tips {\n"
3535 " background:#8e8e0d;\n"
3536 " color:#e8e6e3;\n"
3537 " border-color: #4e2c86;\n"
3538 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3539 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3540 " box-shadow: #60686c 2px 2px 3px;\n"
3541 " }\n"
3542 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003543 "-->\n"
3544 "</style></head>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003545 (ctx->flags & STAT_SHNODE) ? " on " : "",
3546 (ctx->flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003547 );
3548}
3549
Christopher Fauleta8b76842022-12-16 15:08:36 +01003550/* Dumps the HTML stats information block to the local trash buffer for and uses
3551 * the state from stream connector <sc> and per-uri parameters <uri>. The caller
3552 * is responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003553 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003554static void stats_dump_html_info(struct stconn *sc, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003555{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003556 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003557 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaub685ad02023-02-10 16:53:35 +01003558 unsigned int up = (now.tv_sec - start_time.tv_sec);
William Lallemand74c24fb2016-11-21 17:18:36 +01003559 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02003560 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Willy Tarreau6be8d092023-01-12 16:08:41 +01003561 unsigned long long bps;
3562 int thr;
3563
3564 for (bps = thr = 0; thr < global.nbthread; thr++)
3565 bps += 32ULL * read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
Willy Tarreau1713c032019-05-23 12:23:55 +02003566
3567 /* Turn the bytes per second to bits per second and take care of the
3568 * usual ethernet overhead in order to help figure how far we are from
3569 * interface saturation since it's the only case which usually matters.
3570 * For this we count the total size of an Ethernet frame on the wire
3571 * including preamble and IFG (1538) for the largest TCP segment it
3572 * transports (1448 with TCP timestamps). This is not valid for smaller
3573 * packets (under-estimated), but it gives a reasonably accurate
3574 * estimation of how far we are from uplink saturation.
3575 */
3576 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003577
3578 /* WARNING! this has to fit the first packet too.
3579 * We are around 3.5 kB, add adding entries will
3580 * become tricky if we want to support 4kB buffers !
3581 */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003582 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003583 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3584 PRODUCT_NAME "%s</a></h1>\n"
3585 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3586 "<hr width=\"100%%\" class=\"hr\">\n"
3587 "<h3>&gt; General process information</h3>\n"
3588 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003589 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003590 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3591 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3592 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003593 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003594 "Running tasks: %d/%d; idle = %d %%<br>\n"
3595 "</td><td align=\"center\" nowrap>\n"
3596 "<table class=\"lgd\"><tr>\n"
3597 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3598 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3599 "</tr><tr>\n"
3600 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3601 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3602 "</tr><tr>\n"
3603 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3604 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3605 "</tr><tr>\n"
3606 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3607 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3608 "</tr><tr>\n"
3609 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3610 "</tr><tr>\n"
3611 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3612 "</tr></table>\n"
3613 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3614 "</td>"
3615 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3616 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3617 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003618 (ctx->flags & STAT_HIDEVER) ? "" : (stats_version_string),
3619 pid, (ctx->flags & STAT_SHNODE) ? " on " : "",
3620 (ctx->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3621 (ctx->flags & STAT_SHDESC) ? ": " : "",
3622 (ctx->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003623 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003624 up / 86400, (up % 86400) / 3600,
3625 (up % 3600) / 60, (up % 60),
3626 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3627 global.rlimit_memmax ? " MB" : "",
3628 global.rlimit_nofile,
3629 global.maxsock, global.maxconn, global.maxpipes,
3630 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003631 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3632 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003633 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003634 );
3635
Willy Tarreau91cefca2022-05-03 17:08:29 +02003636 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3637 memcpy(scope_txt, scope_ptr, ctx->scope_len);
3638 scope_txt[ctx->scope_len] = '\0';
William Lallemand74c24fb2016-11-21 17:18:36 +01003639
Christopher Fauleta8b76842022-12-16 15:08:36 +01003640 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003641 "<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 +02003642 (ctx->scope_len > 0) ? scope_txt : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003643 STAT_SCOPE_TXT_MAXLEN);
3644
Willy Tarreau91cefca2022-05-03 17:08:29 +02003645 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003646 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003647 if (ctx->scope_len) {
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003648 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02003649 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3650 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003651 }
3652
Willy Tarreau91cefca2022-05-03 17:08:29 +02003653 if (ctx->flags & STAT_HIDE_DOWN)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003654 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003655 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3656 uri->uri_prefix,
3657 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003658 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003659 scope_txt);
3660 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003661 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003662 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3663 uri->uri_prefix,
3664 ";up",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003665 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003666 scope_txt);
3667
3668 if (uri->refresh > 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003669 if (ctx->flags & STAT_NO_REFRESH)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003670 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003671 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3672 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003673 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003674 "",
3675 scope_txt);
3676 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003677 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003678 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3679 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003680 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003681 ";norefresh",
3682 scope_txt);
3683 }
3684
Christopher Fauleta8b76842022-12-16 15:08:36 +01003685 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003686 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3687 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003688 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3689 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003690 scope_txt);
3691
Christopher Fauleta8b76842022-12-16 15:08:36 +01003692 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003693 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3694 uri->uri_prefix,
3695 (uri->refresh > 0) ? ";norefresh" : "",
3696 scope_txt);
3697
Christopher Fauleta8b76842022-12-16 15:08:36 +01003698 chunk_appendf(&trash_chunk,
Christopher Faulet6338a082019-09-09 15:50:54 +02003699 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3700 uri->uri_prefix,
3701 (uri->refresh > 0) ? ";norefresh" : "",
3702 scope_txt, uri->uri_prefix);
3703
Christopher Fauleta8b76842022-12-16 15:08:36 +01003704 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003705 "</ul></td>"
3706 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3707 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3708 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3709 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3710 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3711 "</ul>"
3712 "</td>"
3713 "</tr></table>\n"
3714 ""
3715 );
3716
Willy Tarreau91cefca2022-05-03 17:08:29 +02003717 if (ctx->st_code) {
3718 switch (ctx->st_code) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003719 case STAT_STATUS_DONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003720 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003721 "<p><div class=active_up>"
3722 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3723 "Action processed successfully."
3724 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003725 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3726 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003727 scope_txt);
3728 break;
3729 case STAT_STATUS_NONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003730 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003731 "<p><div class=active_going_down>"
3732 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3733 "Nothing has changed."
3734 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003735 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3736 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003737 scope_txt);
3738 break;
3739 case STAT_STATUS_PART:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003740 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003741 "<p><div class=active_going_down>"
3742 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3743 "Action partially processed.<br>"
3744 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3745 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003746 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3747 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003748 scope_txt);
3749 break;
3750 case STAT_STATUS_ERRP:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003751 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003752 "<p><div class=active_down>"
3753 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3754 "Action not processed because of invalid parameters."
3755 "<ul>"
3756 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003757 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003758 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3759 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3760 "</ul>"
3761 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003762 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3763 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003764 scope_txt);
3765 break;
3766 case STAT_STATUS_EXCD:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003767 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003768 "<p><div class=active_down>"
3769 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3770 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3771 "You should retry with less servers at a time.</b>"
3772 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003773 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3774 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003775 scope_txt);
3776 break;
3777 case STAT_STATUS_DENY:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003778 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003779 "<p><div class=active_down>"
3780 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3781 "<b>Action denied.</b>"
3782 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003783 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3784 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003785 scope_txt);
3786 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003787 case STAT_STATUS_IVAL:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003788 chunk_appendf(&trash_chunk,
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003789 "<p><div class=active_down>"
3790 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3791 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3792 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003793 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3794 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003795 scope_txt);
3796 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003797 default:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003798 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003799 "<p><div class=active_no_check>"
3800 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3801 "Unexpected result."
3802 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003803 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3804 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003805 scope_txt);
3806 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003807 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003808 }
3809}
3810
Christopher Fauleta8b76842022-12-16 15:08:36 +01003811/* Dumps the HTML stats trailer block to the local trash buffer. The caller is
3812 * responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003813 */
3814static void stats_dump_html_end()
3815{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003816 chunk_appendf(&trash_chunk, "</body></html>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003817}
3818
Christopher Fauleta8b76842022-12-16 15:08:36 +01003819/* Dumps the stats JSON header to the local trash buffer buffer which. The
3820 * caller is responsible for clearing it if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003821 */
3822static void stats_dump_json_header()
3823{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003824 chunk_strcat(&trash_chunk, "[");
Simon Horman05ee2132017-01-04 09:37:25 +01003825}
3826
3827
Christopher Fauleta8b76842022-12-16 15:08:36 +01003828/* Dumps the JSON stats trailer block to the local trash buffer. The caller is
3829 * responsible for clearing the local trash buffer if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003830 */
3831static void stats_dump_json_end()
3832{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003833 chunk_strcat(&trash_chunk, "]\n");
Simon Horman05ee2132017-01-04 09:37:25 +01003834}
3835
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003836/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3837 * a pointer to the current server/listener.
3838 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003839static int stats_dump_proxies(struct stconn *sc,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003840 struct htx *htx,
3841 struct uri_auth *uri)
3842{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003843 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003844 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003845 struct channel *rep = sc_ic(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003846 struct proxy *px;
3847
3848 /* dump proxies */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003849 while (ctx->obj1) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003850 if (htx) {
3851 if (htx_almost_full(htx))
3852 goto full;
3853 }
3854 else {
3855 if (buffer_almost_full(&rep->buf))
3856 goto full;
3857 }
3858
Willy Tarreau91cefca2022-05-03 17:08:29 +02003859 px = ctx->obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003860 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003861 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003862 * This change allows retrieving stats from "old" proxies after a reload.
3863 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003864 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003865 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003866 if (stats_dump_proxy_to_buffer(sc, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003867 return 0;
3868 }
3869
Willy Tarreau91cefca2022-05-03 17:08:29 +02003870 ctx->obj1 = px->next;
3871 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003872 ctx->field = 0;
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003873 }
3874
3875 return 1;
3876
3877 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003878 sc_need_room(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003879 return 0;
3880}
3881
Willy Tarreau4596fe22022-05-17 19:07:51 +02003882/* This function dumps statistics onto the stream connector's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003883 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3884 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3885 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3886 * and the stream must be closed, or -1 in case of any error. This function is
3887 * used by both the CLI and the HTTP handlers.
3888 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003889static int stats_dump_stat_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003890 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003891{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003892 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003893 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003894 struct channel *rep = sc_ic(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003895 enum stats_domain domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003896
Christopher Fauleta8b76842022-12-16 15:08:36 +01003897 chunk_reset(&trash_chunk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003898
Willy Tarreau41f88522022-05-03 18:39:27 +02003899 switch (ctx->state) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003900 case STAT_STATE_INIT:
3901 ctx->state = STAT_STATE_HEAD; /* let's start producing data */
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003902 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003903
Willy Tarreau6ef16482022-05-06 18:07:53 +02003904 case STAT_STATE_HEAD:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003905 if (ctx->flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003906 stats_dump_html_head(appctx, uri);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003907 else if (ctx->flags & STAT_JSON_SCHM)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003908 stats_dump_json_schema(&trash_chunk);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003909 else if (ctx->flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003910 stats_dump_json_header();
Willy Tarreau91cefca2022-05-03 17:08:29 +02003911 else if (!(ctx->flags & STAT_FMT_TYPED))
3912 stats_dump_csv_header(ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003913
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003914 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003915 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003916
Willy Tarreau91cefca2022-05-03 17:08:29 +02003917 if (ctx->flags & STAT_JSON_SCHM) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003918 ctx->state = STAT_STATE_FIN;
Christopher Faulet6338a082019-09-09 15:50:54 +02003919 return 1;
3920 }
Willy Tarreau6ef16482022-05-06 18:07:53 +02003921 ctx->state = STAT_STATE_INFO;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003922 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003923
Willy Tarreau6ef16482022-05-06 18:07:53 +02003924 case STAT_STATE_INFO:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003925 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003926 stats_dump_html_info(sc, uri);
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003927 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003928 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003929 }
3930
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003931 if (domain == STATS_DOMAIN_PROXY)
Willy Tarreau91cefca2022-05-03 17:08:29 +02003932 ctx->obj1 = proxies_list;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003933
Willy Tarreau91cefca2022-05-03 17:08:29 +02003934 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003935 ctx->field = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003936 ctx->state = STAT_STATE_LIST;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003937 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003938
Willy Tarreau6ef16482022-05-06 18:07:53 +02003939 case STAT_STATE_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003940 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003941 case STATS_DOMAIN_RESOLVERS:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003942 if (!stats_dump_resolvers(sc, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003943 stat_count[domain],
3944 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003945 return 0;
3946 }
3947 break;
3948
3949 case STATS_DOMAIN_PROXY:
3950 default:
3951 /* dump proxies */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003952 if (!stats_dump_proxies(sc, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003953 return 0;
3954 break;
3955 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003956
Willy Tarreau6ef16482022-05-06 18:07:53 +02003957 ctx->state = STAT_STATE_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003958 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003959
Willy Tarreau6ef16482022-05-06 18:07:53 +02003960 case STAT_STATE_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003961 if (ctx->flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3962 if (ctx->flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01003963 stats_dump_html_end();
3964 else
3965 stats_dump_json_end();
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +01003966 if (!stats_putchk(rep, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003967 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003968 }
3969
Willy Tarreau6ef16482022-05-06 18:07:53 +02003970 ctx->state = STAT_STATE_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003971 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003972
Willy Tarreau6ef16482022-05-06 18:07:53 +02003973 case STAT_STATE_FIN:
William Lallemand74c24fb2016-11-21 17:18:36 +01003974 return 1;
3975
3976 default:
3977 /* unknown state ! */
Willy Tarreau6ef16482022-05-06 18:07:53 +02003978 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01003979 return -1;
3980 }
Christopher Fauletef779222018-10-31 08:47:01 +01003981
3982 full:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003983 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01003984 return 0;
3985
William Lallemand74c24fb2016-11-21 17:18:36 +01003986}
3987
3988/* We reached the stats page through a POST request. The appctx is
3989 * expected to have already been allocated by the caller.
3990 * Parse the posted data and enable/disable servers if necessary.
3991 * Returns 1 if request was parsed or zero if it needs more data.
3992 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003993static int stats_process_http_post(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01003994{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003995 struct stream *s = __sc_strm(sc);
3996 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003997 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003998
3999 struct proxy *px = NULL;
4000 struct server *sv = NULL;
4001
4002 char key[LINESIZE];
4003 int action = ST_ADM_ACTION_NONE;
4004 int reprocess = 0;
4005
4006 int total_servers = 0;
4007 int altered_servers = 0;
4008
4009 char *first_param, *cur_param, *next_param, *end_params;
4010 char *st_cur_param = NULL;
4011 char *st_next_param = NULL;
4012
Christopher Fauleta3618372018-12-13 21:59:56 +01004013 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01004014
Christopher Fauletb7f88902019-07-15 21:56:43 +02004015 struct htx *htx = htxbuf(&s->req.buf);
4016 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01004017
Christopher Fauletb7f88902019-07-15 21:56:43 +02004018 /* we need more data */
4019 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
4020 /* check if we can receive more */
4021 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004022 ctx->st_code = STAT_STATUS_EXCD;
Christopher Fauletb7f88902019-07-15 21:56:43 +02004023 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01004024 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004025 goto wait;
4026 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004027
Christopher Fauletb7f88902019-07-15 21:56:43 +02004028 /* The request was fully received. Copy data */
4029 blk = htx_get_head_blk(htx);
4030 while (blk) {
4031 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004032
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004033 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02004034 break;
4035 if (type == HTX_BLK_DATA) {
4036 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004037
Christopher Fauletb7f88902019-07-15 21:56:43 +02004038 if (!chunk_memcat(temp, v.ptr, v.len)) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004039 ctx->st_code = STAT_STATUS_EXCD;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004040 goto out;
4041 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004042 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004043 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01004044 }
4045
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004046 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01004047 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01004048 cur_param = next_param = end_params;
4049 *end_params = '\0';
4050
Willy Tarreau91cefca2022-05-03 17:08:29 +02004051 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004052
4053 /*
4054 * Parse the parameters in reverse order to only store the last value.
4055 * From the html form, the backend and the action are at the end.
4056 */
4057 while (cur_param > first_param) {
4058 char *value;
4059 int poffset, plen;
4060
4061 cur_param--;
4062
4063 if ((*cur_param == '&') || (cur_param == first_param)) {
4064 reprocess_servers:
4065 /* Parse the key */
4066 poffset = (cur_param != first_param ? 1 : 0);
4067 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4068 if ((plen > 0) && (plen <= sizeof(key))) {
4069 strncpy(key, cur_param + poffset, plen);
4070 key[plen - 1] = '\0';
4071 } else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004072 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004073 goto out;
4074 }
4075
4076 /* Parse the value */
4077 value = key;
4078 while (*value != '\0' && *value != '=') {
4079 value++;
4080 }
4081 if (*value == '=') {
4082 /* Ok, a value is found, we can mark the end of the key */
4083 *value++ = '\0';
4084 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02004085 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01004086 break;
4087
4088 /* Now we can check the key to see what to do */
4089 if (!px && (strcmp(key, "b") == 0)) {
4090 if ((px = proxy_be_by_name(value)) == NULL) {
4091 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004092 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004093 goto out;
4094 }
4095 }
4096 else if (!action && (strcmp(key, "action") == 0)) {
4097 if (strcmp(value, "ready") == 0) {
4098 action = ST_ADM_ACTION_READY;
4099 }
4100 else if (strcmp(value, "drain") == 0) {
4101 action = ST_ADM_ACTION_DRAIN;
4102 }
4103 else if (strcmp(value, "maint") == 0) {
4104 action = ST_ADM_ACTION_MAINT;
4105 }
4106 else if (strcmp(value, "shutdown") == 0) {
4107 action = ST_ADM_ACTION_SHUTDOWN;
4108 }
4109 else if (strcmp(value, "dhlth") == 0) {
4110 action = ST_ADM_ACTION_DHLTH;
4111 }
4112 else if (strcmp(value, "ehlth") == 0) {
4113 action = ST_ADM_ACTION_EHLTH;
4114 }
4115 else if (strcmp(value, "hrunn") == 0) {
4116 action = ST_ADM_ACTION_HRUNN;
4117 }
4118 else if (strcmp(value, "hnolb") == 0) {
4119 action = ST_ADM_ACTION_HNOLB;
4120 }
4121 else if (strcmp(value, "hdown") == 0) {
4122 action = ST_ADM_ACTION_HDOWN;
4123 }
4124 else if (strcmp(value, "dagent") == 0) {
4125 action = ST_ADM_ACTION_DAGENT;
4126 }
4127 else if (strcmp(value, "eagent") == 0) {
4128 action = ST_ADM_ACTION_EAGENT;
4129 }
4130 else if (strcmp(value, "arunn") == 0) {
4131 action = ST_ADM_ACTION_ARUNN;
4132 }
4133 else if (strcmp(value, "adown") == 0) {
4134 action = ST_ADM_ACTION_ADOWN;
4135 }
4136 /* else these are the old supported methods */
4137 else if (strcmp(value, "disable") == 0) {
4138 action = ST_ADM_ACTION_DISABLE;
4139 }
4140 else if (strcmp(value, "enable") == 0) {
4141 action = ST_ADM_ACTION_ENABLE;
4142 }
4143 else if (strcmp(value, "stop") == 0) {
4144 action = ST_ADM_ACTION_STOP;
4145 }
4146 else if (strcmp(value, "start") == 0) {
4147 action = ST_ADM_ACTION_START;
4148 }
4149 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004150 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004151 goto out;
4152 }
4153 }
4154 else if (strcmp(key, "s") == 0) {
4155 if (!(px && action)) {
4156 /*
4157 * Indicates that we'll need to reprocess the parameters
4158 * as soon as backend and action are known
4159 */
4160 if (!reprocess) {
4161 st_cur_param = cur_param;
4162 st_next_param = next_param;
4163 }
4164 reprocess = 1;
4165 }
4166 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004167 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004168 switch (action) {
4169 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004170 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004171 altered_servers++;
4172 total_servers++;
4173 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
4174 }
4175 break;
4176 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004177 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004178 altered_servers++;
4179 total_servers++;
4180 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4181 }
4182 break;
4183 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004184 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004185 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
4186 altered_servers++;
4187 total_servers++;
4188 }
4189 break;
4190 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004191 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004192 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4193 altered_servers++;
4194 total_servers++;
4195 }
4196 break;
4197 case ST_ADM_ACTION_DHLTH:
4198 if (sv->check.state & CHK_ST_CONFIGURED) {
4199 sv->check.state &= ~CHK_ST_ENABLED;
4200 altered_servers++;
4201 total_servers++;
4202 }
4203 break;
4204 case ST_ADM_ACTION_EHLTH:
4205 if (sv->check.state & CHK_ST_CONFIGURED) {
4206 sv->check.state |= CHK_ST_ENABLED;
4207 altered_servers++;
4208 total_servers++;
4209 }
4210 break;
4211 case ST_ADM_ACTION_HRUNN:
4212 if (!(sv->track)) {
4213 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004214 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004215 altered_servers++;
4216 total_servers++;
4217 }
4218 break;
4219 case ST_ADM_ACTION_HNOLB:
4220 if (!(sv->track)) {
4221 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004222 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004223 altered_servers++;
4224 total_servers++;
4225 }
4226 break;
4227 case ST_ADM_ACTION_HDOWN:
4228 if (!(sv->track)) {
4229 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004230 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004231 altered_servers++;
4232 total_servers++;
4233 }
4234 break;
4235 case ST_ADM_ACTION_DAGENT:
4236 if (sv->agent.state & CHK_ST_CONFIGURED) {
4237 sv->agent.state &= ~CHK_ST_ENABLED;
4238 altered_servers++;
4239 total_servers++;
4240 }
4241 break;
4242 case ST_ADM_ACTION_EAGENT:
4243 if (sv->agent.state & CHK_ST_CONFIGURED) {
4244 sv->agent.state |= CHK_ST_ENABLED;
4245 altered_servers++;
4246 total_servers++;
4247 }
4248 break;
4249 case ST_ADM_ACTION_ARUNN:
4250 if (sv->agent.state & CHK_ST_ENABLED) {
4251 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004252 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004253 altered_servers++;
4254 total_servers++;
4255 }
4256 break;
4257 case ST_ADM_ACTION_ADOWN:
4258 if (sv->agent.state & CHK_ST_ENABLED) {
4259 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004260 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01004261 altered_servers++;
4262 total_servers++;
4263 }
4264 break;
4265 case ST_ADM_ACTION_READY:
4266 srv_adm_set_ready(sv);
4267 altered_servers++;
4268 total_servers++;
4269 break;
4270 case ST_ADM_ACTION_DRAIN:
4271 srv_adm_set_drain(sv);
4272 altered_servers++;
4273 total_servers++;
4274 break;
4275 case ST_ADM_ACTION_MAINT:
4276 srv_adm_set_maint(sv);
4277 altered_servers++;
4278 total_servers++;
4279 break;
4280 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004281 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004282 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004283 altered_servers++;
4284 total_servers++;
4285 }
4286 break;
4287 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004288 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004289 } else {
4290 /* the server name is unknown or ambiguous (duplicate names) */
4291 total_servers++;
4292 }
4293 }
4294 if (reprocess && px && action) {
4295 /* Now, we know the backend and the action chosen by the user.
4296 * We can safely restart from the first server parameter
4297 * to reprocess them
4298 */
4299 cur_param = st_cur_param;
4300 next_param = st_next_param;
4301 reprocess = 0;
4302 goto reprocess_servers;
4303 }
4304
4305 next_param = cur_param;
4306 }
4307 }
4308
4309 if (total_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004310 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004311 }
4312 else if (altered_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004313 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004314 }
4315 else if (altered_servers == total_servers) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004316 ctx->st_code = STAT_STATUS_DONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004317 }
4318 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004319 ctx->st_code = STAT_STATUS_PART;
William Lallemand74c24fb2016-11-21 17:18:36 +01004320 }
4321 out:
4322 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004323 wait:
Willy Tarreau91cefca2022-05-03 17:08:29 +02004324 ctx->st_code = STAT_STATUS_NONE;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004325 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004326}
4327
4328
Willy Tarreaucaff6312022-05-27 10:17:46 +02004329static int stats_send_http_headers(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004330{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004331 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004332 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004333 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004334 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004335 struct htx_sl *sl;
4336 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004337
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004338 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);
4339 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4340 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004341 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004342 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004343
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004344 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004345 goto full;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004346 if (ctx->flags & STAT_FMT_HTML) {
Christopher Fauletef779222018-10-31 08:47:01 +01004347 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4348 goto full;
4349 }
Willy Tarreau91cefca2022-05-03 17:08:29 +02004350 else if (ctx->flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004351 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4352 goto full;
4353 }
Christopher Fauletef779222018-10-31 08:47:01 +01004354 else {
4355 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4356 goto full;
4357 }
4358
Willy Tarreau91cefca2022-05-03 17:08:29 +02004359 if (uri->refresh > 0 && !(ctx->flags & STAT_NO_REFRESH)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004360 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004361 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004362 goto full;
4363 }
4364
Willy Tarreau91cefca2022-05-03 17:08:29 +02004365 if (ctx->flags & STAT_CHUNKED) {
Christopher Fauletef779222018-10-31 08:47:01 +01004366 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4367 goto full;
4368 }
4369
4370 if (!htx_add_endof(htx, HTX_BLK_EOH))
4371 goto full;
4372
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004373 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004374 return 1;
4375
4376 full:
4377 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004378 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004379 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004380}
4381
Christopher Fauletef779222018-10-31 08:47:01 +01004382
Willy Tarreaucaff6312022-05-27 10:17:46 +02004383static int stats_send_http_redirect(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004384{
4385 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02004386 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004387 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004388 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004389 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004390 struct htx_sl *sl;
4391 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004392
Willy Tarreau91cefca2022-05-03 17:08:29 +02004393 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauletef779222018-10-31 08:47:01 +01004394 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004395 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004396 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004397
Willy Tarreaufc458ec2023-04-07 18:11:39 +02004398 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02004399 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
4400 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004401 }
4402
4403 /* We don't want to land on the posted stats page because a refresh will
4404 * repost the data. We don't want this to happen on accident so we redirect
4405 * the browse to the stats page with a GET.
4406 */
4407 chunk_printf(&trash, "%s;st=%s%s%s%s",
4408 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02004409 ((ctx->st_code > STAT_STATUS_INIT) &&
4410 (ctx->st_code < STAT_STATUS_SIZE) &&
4411 stat_status_codes[ctx->st_code]) ?
4412 stat_status_codes[ctx->st_code] :
Christopher Fauletef779222018-10-31 08:47:01 +01004413 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau91cefca2022-05-03 17:08:29 +02004414 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
4415 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Fauletef779222018-10-31 08:47:01 +01004416 scope_txt);
4417
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004418 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4419 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4420 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004421 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004422 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004423
4424 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004425 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4426 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4427 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4428 goto full;
4429
4430 if (!htx_add_endof(htx, HTX_BLK_EOH))
4431 goto full;
4432
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004433 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004434 return 1;
4435
4436full:
4437 htx_reset(htx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004438 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004439 return 0;
4440}
William Lallemand74c24fb2016-11-21 17:18:36 +01004441
Simon Horman05ee2132017-01-04 09:37:25 +01004442
Willy Tarreau4596fe22022-05-17 19:07:51 +02004443/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004444 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4445 * appctx->st0 contains the operation in progress (dump, done). The handler
4446 * automatically unregisters itself once transfer is complete.
4447 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004448static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004449{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004450 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02004451 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004452 struct stream *s = __sc_strm(sc);
4453 struct channel *req = sc_oc(sc);
4454 struct channel *res = sc_ic(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004455 struct htx *req_htx, *res_htx;
4456
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004457 /* only proxy stats are available via http */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004458 ctx->domain = STATS_DOMAIN_PROXY;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004459
Christopher Fauletef779222018-10-31 08:47:01 +01004460 res_htx = htx_from_buf(&res->buf);
4461
Christopher Faulet9837bd82023-04-11 07:41:30 +02004462 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) {
4463 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004464 goto out;
Christopher Faulet9837bd82023-04-11 07:41:30 +02004465 }
Christopher Fauletef779222018-10-31 08:47:01 +01004466
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004467 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004468 if (!b_size(&res->buf)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004469 sc_need_room(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004470 goto out;
4471 }
4472
Christopher Fauletef779222018-10-31 08:47:01 +01004473 /* all states are processed in sequence */
4474 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004475 if (stats_send_http_headers(sc, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004476 if (s->txn->meth == HTTP_METH_HEAD)
4477 appctx->st0 = STAT_HTTP_DONE;
4478 else
4479 appctx->st0 = STAT_HTTP_DUMP;
4480 }
4481 }
4482
4483 if (appctx->st0 == STAT_HTTP_DUMP) {
Aurelien DARRAGON14656842023-02-03 08:31:42 +01004484 trash_chunk = b_make(trash.area, res->buf.size, 0, 0);
Aurelien DARRAGON5e7ecbe2023-02-02 15:03:12 +01004485 /* adjust buffer size to take htx overhead into account,
4486 * make sure to perform this call on an empty buffer
4487 */
4488 trash_chunk.size = buf_room_for_htx_data(&trash_chunk);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004489 if (stats_dump_stat_to_buffer(sc, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004490 appctx->st0 = STAT_HTTP_DONE;
4491 }
4492
4493 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004494 if (stats_process_http_post(sc))
Christopher Fauletef779222018-10-31 08:47:01 +01004495 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet87633c32023-04-03 18:32:50 +02004496 else if (chn_prod(req)->flags & SC_FL_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01004497 appctx->st0 = STAT_HTTP_DONE;
4498 }
4499
4500 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004501 if (stats_send_http_redirect(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004502 appctx->st0 = STAT_HTTP_DONE;
4503 }
4504
4505 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004506 /* no more data are expected. If the response buffer is empty,
4507 * be sure to add something (EOT block in this case) to have
4508 * something to send. It is important to be sure the EOM flags
4509 * will be handled by the endpoint.
4510 */
4511 if (htx_is_empty(res_htx)) {
4512 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004513 sc_need_room(sc);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004514 goto out;
4515 }
4516 channel_add_input(res, 1);
4517 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004518 res_htx->flags |= HTX_FL_EOM;
Willy Tarreaud869e132022-05-17 18:05:31 +02004519 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004520 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004521 }
4522
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004523 if (appctx->st0 == STAT_HTTP_END) {
Christopher Fauletdf15a5d2023-03-31 11:28:22 +02004524 se_fl_set(appctx->sedesc, SE_FL_EOS);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004525 applet_will_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004526 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004527
Christopher Fauletef779222018-10-31 08:47:01 +01004528 out:
4529 /* we have left the request in the buffer for the case where we
4530 * process a POST, and this automatically re-enables activity on
4531 * read. It's better to indicate that we want to stop reading when
4532 * we're sending, so that we know there's at most one direction
4533 * deciding to wake the applet up. It saves it from looping when
4534 * emitting large blocks into small TCP windows.
4535 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004536 htx_to_buf(res_htx, &res->buf);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004537 if (appctx->st0 == STAT_HTTP_END) {
4538 /* eat the whole request */
4539 if (co_data(req)) {
4540 req_htx = htx_from_buf(&req->buf);
4541 co_htx_skip(req, req_htx, co_data(req));
4542 htx_to_buf(req_htx, &req->buf);
4543 }
4544 }
4545 else if (!channel_is_empty(res))
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02004546 applet_wont_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004547}
4548
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004549/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004550static int stats_dump_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004551 const struct field *info,
4552 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004553{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004554 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004555 int field;
4556
4557 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4558 if (!field_format(info, field))
4559 continue;
4560
Willy Tarreaueaa55372019-10-09 07:39:11 +02004561 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004562 return 0;
4563 if (!stats_emit_raw_data_field(out, &info[field]))
4564 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004565 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4566 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004567 if (!chunk_strcat(out, "\n"))
4568 return 0;
4569 }
4570 return 1;
4571}
4572
4573/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004574static int stats_dump_typed_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004575 const struct field *info,
4576 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004577{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004578 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004579 int field;
4580
4581 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4582 if (!field_format(info, field))
4583 continue;
4584
Willy Tarreaueaa55372019-10-09 07:39:11 +02004585 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 +01004586 return 0;
4587 if (!stats_emit_field_tags(out, &info[field], ':'))
4588 return 0;
4589 if (!stats_emit_typed_data_field(out, &info[field]))
4590 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004591 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4592 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004593 if (!chunk_strcat(out, "\n"))
4594 return 0;
4595 }
4596 return 1;
4597}
4598
Willy Tarreau0b26b382021-05-08 07:43:53 +02004599/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4600 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4601 * less then this value, the function returns 0, otherwise, it returns 1. Some
4602 * fields' presence or precision may depend on some of the STAT_* flags present
4603 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004604 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004605int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004606{
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004607 struct timeval up;
Willy Tarreau83061a82018-07-13 11:56:34 +02004608 struct buffer *out = get_trash_chunk();
Willy Tarreau6be8d092023-01-12 16:08:41 +01004609 uint64_t glob_out_bytes, glob_spl_bytes, glob_out_b32;
4610 int thr;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004611
4612#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004613 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4614 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4615 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004616
Willy Tarreauc5977722021-05-08 08:14:04 +02004617 if (ssl_key_rate < ssl_sess_rate)
4618 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004619#endif
4620
Willy Tarreau6be8d092023-01-12 16:08:41 +01004621 /* sum certain per-thread totals (mostly byte counts) */
4622 glob_out_bytes = glob_spl_bytes = glob_out_b32 = 0;
4623 for (thr = 0; thr < global.nbthread; thr++) {
4624 glob_out_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].out_bytes);
4625 glob_spl_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].spliced_out_bytes);
4626 glob_out_b32 += read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
4627 }
4628 glob_out_b32 *= 32; // values are 32-byte units
4629
Willy Tarreau6093ba42023-02-07 15:52:14 +01004630 tv_remain(&start_time, &now, &up);
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004631
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004632 if (len < INF_TOTAL_FIELDS)
4633 return 0;
4634
4635 chunk_reset(out);
4636 memset(info, 0, sizeof(*info) * len);
4637
4638 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004639 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004640 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004641 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004642
Yves Lafon95317282018-02-26 11:10:37 +01004643 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004644 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004645 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004646 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4647
4648 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004649 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 +01004650
Willy Tarreaue8abc322021-05-08 07:56:56 +02004651 info[INF_UPTIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, up.tv_sec + up.tv_usec / 1000000.0) : mkf_u32(FN_DURATION, up.tv_sec);
Willy Tarreau6093ba42023-02-07 15:52:14 +01004652 info[INF_START_TIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, start_time.tv_sec + start_time.tv_usec / 1000000.0) : mkf_u32(FN_DURATION, start_time.tv_sec);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004653 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004654 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004655 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004656 info[INF_POOL_ALLOC_BYTES] = mkf_u64(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004657 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004658 info[INF_POOL_USED_BYTES] = mkf_u64(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004659 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4660 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4661 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4662 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4663 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4664 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4665 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4666 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4667#ifdef USE_OPENSSL
4668 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004669 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4670 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004671#endif
4672 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4673 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4674 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004675 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 +01004676 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4677 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004678 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 +01004679 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4680 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4681
4682#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004683 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 +01004684 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4685 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004686 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 +01004687 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004688 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4689 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 +01004690 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4691 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4692 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4693#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004694 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));
4695 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 +01004696 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4697#ifdef USE_ZLIB
4698 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4699 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4700#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004701 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004702 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004703 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004704 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4705 if (global.desc)
4706 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004707 info[INF_STOPPING] = mkf_u32(0, stopping);
4708 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004709 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004710 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004711 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004712 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004713 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004714 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004715 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau6be8d092023-01-12 16:08:41 +01004716 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, glob_out_bytes);
4717 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, glob_spl_bytes);
4718 info[INF_BYTES_OUT_RATE] = mkf_u64(FN_RATE, glob_out_b32);
Willy Tarreau9b013702019-10-24 18:18:02 +02004719 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004720 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004721
4722 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
4723 chunk_appendf(out, "%#x", get_tainted());
4724
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004725 return 1;
4726}
4727
Willy Tarreau4596fe22022-05-17 19:07:51 +02004728/* This function dumps information onto the stream connector's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004729 * It returns 0 as long as it does not complete, non-zero upon completion.
4730 * No state is used.
4731 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004732static int stats_dump_info_to_buffer(struct stconn *sc)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004733{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004734 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004735 struct show_stat_ctx *ctx = appctx->svcctx;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004736 int ret;
4737 int current_field;
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004738
Willy Tarreau91cefca2022-05-03 17:08:29 +02004739 if (!stats_fill_info(info, INF_TOTAL_FIELDS, ctx->flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004740 return 0;
4741
Christopher Fauleta8b76842022-12-16 15:08:36 +01004742 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004743more:
4744 current_field = ctx->field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004745
Willy Tarreau91cefca2022-05-03 17:08:29 +02004746 if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004747 ret = stats_dump_typed_info_fields(&trash_chunk, info, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004748 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004749 ret = stats_dump_json_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004750 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01004751 ret = stats_dump_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004752
Christopher Fauleta8b76842022-12-16 15:08:36 +01004753 if (applet_putchk(appctx, &trash_chunk) == -1) {
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004754 /* restore previous field */
4755 ctx->field = current_field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004756 return 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004757 }
4758 if (ret && ctx->field) {
4759 /* partial dump */
4760 goto more;
4761 }
4762 ctx->field = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004763 return 1;
4764}
4765
Willy Tarreau4596fe22022-05-17 19:07:51 +02004766/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004767 * It returns 0 as long as it does not complete, non-zero upon completion.
4768 * No state is used.
4769 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004770 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004771 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4772 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004773static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004774{
4775
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004776 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004777
4778 chunk_strcat(out,
4779 "{"
4780 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4781 "\"oneOf\":["
4782 "{"
4783 "\"title\":\"Info\","
4784 "\"type\":\"array\","
4785 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004786 "\"title\":\"InfoItem\","
4787 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004788 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004789 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4790 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4791 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4792 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4793 "},"
4794 "\"required\":[\"field\",\"processNum\",\"tags\","
4795 "\"value\"]"
4796 "}"
4797 "},"
4798 "{"
4799 "\"title\":\"Stat\","
4800 "\"type\":\"array\","
4801 "\"items\":{"
4802 "\"title\":\"InfoItem\","
4803 "\"type\":\"object\","
4804 "\"properties\":{"
4805 "\"objType\":{"
4806 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4807 "\"Server\",\"Unknown\"]"
4808 "},"
4809 "\"proxyId\":{"
4810 "\"type\":\"integer\","
4811 "\"minimum\":0"
4812 "},"
4813 "\"id\":{"
4814 "\"type\":\"integer\","
4815 "\"minimum\":0"
4816 "},"
4817 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4818 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4819 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4820 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4821 "},"
4822 "\"required\":[\"objType\",\"proxyId\",\"id\","
4823 "\"field\",\"processNum\",\"tags\","
4824 "\"value\"]"
4825 "}"
4826 "},"
4827 "{"
4828 "\"title\":\"Error\","
4829 "\"type\":\"object\","
4830 "\"properties\":{"
4831 "\"errorStr\":{"
4832 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004833 "}"
4834 "},"
4835 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004836 "}"
4837 "],"
4838 "\"definitions\":{"
4839 "\"field\":{"
4840 "\"type\":\"object\","
4841 "\"pos\":{"
4842 "\"type\":\"integer\","
4843 "\"minimum\":0"
4844 "},"
4845 "\"name\":{"
4846 "\"type\":\"string\""
4847 "},"
4848 "\"required\":[\"pos\",\"name\"]"
4849 "},"
4850 "\"processNum\":{"
4851 "\"type\":\"integer\","
4852 "\"minimum\":1"
4853 "},"
4854 "\"tags\":{"
4855 "\"type\":\"object\","
4856 "\"origin\":{"
4857 "\"type\":\"string\","
4858 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4859 "\"Config\",\"Product\",\"Unknown\"]"
4860 "},"
4861 "\"nature\":{"
4862 "\"type\":\"string\","
4863 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4864 "\"Rate\",\"Counter\",\"Duration\","
4865 "\"Age\",\"Time\",\"Name\",\"Output\","
4866 "\"Avg\", \"Unknown\"]"
4867 "},"
4868 "\"scope\":{"
4869 "\"type\":\"string\","
4870 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4871 "\"System\",\"Unknown\"]"
4872 "},"
4873 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4874 "},"
4875 "\"typedValue\":{"
4876 "\"type\":\"object\","
4877 "\"oneOf\":["
4878 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4879 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4880 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4881 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4882 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4883 "],"
4884 "\"definitions\":{"
4885 "\"s32Value\":{"
4886 "\"properties\":{"
4887 "\"type\":{"
4888 "\"type\":\"string\","
4889 "\"enum\":[\"s32\"]"
4890 "},"
4891 "\"value\":{"
4892 "\"type\":\"integer\","
4893 "\"minimum\":-2147483648,"
4894 "\"maximum\":2147483647"
4895 "}"
4896 "},"
4897 "\"required\":[\"type\",\"value\"]"
4898 "},"
4899 "\"s64Value\":{"
4900 "\"properties\":{"
4901 "\"type\":{"
4902 "\"type\":\"string\","
4903 "\"enum\":[\"s64\"]"
4904 "},"
4905 "\"value\":{"
4906 "\"type\":\"integer\","
4907 "\"minimum\":-9007199254740991,"
4908 "\"maximum\":9007199254740991"
4909 "}"
4910 "},"
4911 "\"required\":[\"type\",\"value\"]"
4912 "},"
4913 "\"u32Value\":{"
4914 "\"properties\":{"
4915 "\"type\":{"
4916 "\"type\":\"string\","
4917 "\"enum\":[\"u32\"]"
4918 "},"
4919 "\"value\":{"
4920 "\"type\":\"integer\","
4921 "\"minimum\":0,"
4922 "\"maximum\":4294967295"
4923 "}"
4924 "},"
4925 "\"required\":[\"type\",\"value\"]"
4926 "},"
4927 "\"u64Value\":{"
4928 "\"properties\":{"
4929 "\"type\":{"
4930 "\"type\":\"string\","
4931 "\"enum\":[\"u64\"]"
4932 "},"
4933 "\"value\":{"
4934 "\"type\":\"integer\","
4935 "\"minimum\":0,"
4936 "\"maximum\":9007199254740991"
4937 "}"
4938 "},"
4939 "\"required\":[\"type\",\"value\"]"
4940 "},"
4941 "\"strValue\":{"
4942 "\"properties\":{"
4943 "\"type\":{"
4944 "\"type\":\"string\","
4945 "\"enum\":[\"str\"]"
4946 "},"
4947 "\"value\":{\"type\":\"string\"}"
4948 "},"
4949 "\"required\":[\"type\",\"value\"]"
4950 "},"
4951 "\"unknownValue\":{"
4952 "\"properties\":{"
4953 "\"type\":{"
4954 "\"type\":\"integer\","
4955 "\"minimum\":0"
4956 "},"
4957 "\"value\":{"
4958 "\"type\":\"string\","
4959 "\"enum\":[\"unknown\"]"
4960 "}"
4961 "},"
4962 "\"required\":[\"type\",\"value\"]"
4963 "}"
4964 "}"
4965 "}"
4966 "}"
4967 "}");
4968
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004969 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004970 chunk_reset(out);
4971 chunk_appendf(out,
4972 "{\"errorStr\":\"output buffer too short\"}");
4973 }
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02004974 chunk_appendf(out, "\n");
Simon Horman6f6bb382017-01-04 09:37:26 +01004975}
4976
Willy Tarreau4596fe22022-05-17 19:07:51 +02004977/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004978 * It returns 0 as long as it does not complete, non-zero upon completion.
4979 * No state is used.
4980 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004981static int stats_dump_json_schema_to_buffer(struct appctx *appctx)
Simon Horman6f6bb382017-01-04 09:37:26 +01004982{
Simon Horman6f6bb382017-01-04 09:37:26 +01004983
Christopher Fauleta8b76842022-12-16 15:08:36 +01004984 chunk_reset(&trash_chunk);
Simon Horman6f6bb382017-01-04 09:37:26 +01004985
Christopher Fauleta8b76842022-12-16 15:08:36 +01004986 stats_dump_json_schema(&trash_chunk);
4987
4988 if (applet_putchk(appctx, &trash_chunk) == -1)
Simon Horman6f6bb382017-01-04 09:37:26 +01004989 return 0;
Simon Horman6f6bb382017-01-04 09:37:26 +01004990
4991 return 1;
4992}
4993
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004994static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01004995{
4996 struct proxy *px;
4997 struct server *sv;
4998 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02004999 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005000 int clrall = 0;
5001
5002 if (strcmp(args[2], "all") == 0)
5003 clrall = 1;
5004
5005 /* check permissions */
5006 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
5007 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
5008 return 1;
5009
Olivier Houchardfbc74e82017-11-24 16:54:05 +01005010 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01005011 if (clrall) {
5012 memset(&px->be_counters, 0, sizeof(px->be_counters));
5013 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
5014 }
5015 else {
5016 px->be_counters.conn_max = 0;
5017 px->be_counters.p.http.rps_max = 0;
5018 px->be_counters.sps_max = 0;
5019 px->be_counters.cps_max = 0;
5020 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005021 px->be_counters.qtime_max = 0;
5022 px->be_counters.ctime_max = 0;
5023 px->be_counters.dtime_max = 0;
5024 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005025
5026 px->fe_counters.conn_max = 0;
5027 px->fe_counters.p.http.rps_max = 0;
5028 px->fe_counters.sps_max = 0;
5029 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005030 }
5031
5032 for (sv = px->srv; sv; sv = sv->next)
5033 if (clrall)
5034 memset(&sv->counters, 0, sizeof(sv->counters));
5035 else {
5036 sv->counters.cur_sess_max = 0;
5037 sv->counters.nbpend_max = 0;
5038 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005039 sv->counters.qtime_max = 0;
5040 sv->counters.ctime_max = 0;
5041 sv->counters.dtime_max = 0;
5042 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005043 }
5044
5045 list_for_each_entry(li, &px->conf.listeners, by_fe)
5046 if (li->counters) {
5047 if (clrall)
5048 memset(li->counters, 0, sizeof(*li->counters));
5049 else
5050 li->counters->conn_max = 0;
5051 }
5052 }
5053
5054 global.cps_max = 0;
5055 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02005056 global.ssl_max = 0;
5057 global.ssl_fe_keys_max = 0;
5058 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005059
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005060 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5061 if (!mod->clearable && !clrall)
5062 continue;
5063
5064 for (px = proxies_list; px; px = px->next) {
5065 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
5066
5067 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
5068 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
5069 mod,
5070 mod->counters,
5071 mod->counters_size);
5072 }
5073
5074 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
5075 EXTRA_COUNTERS_INIT(px->extra_counters_be,
5076 mod,
5077 mod->counters,
5078 mod->counters_size);
5079 }
5080
5081 if (mod_cap & STATS_PX_CAP_SRV) {
5082 for (sv = px->srv; sv; sv = sv->next) {
5083 EXTRA_COUNTERS_INIT(sv->extra_counters,
5084 mod,
5085 mod->counters,
5086 mod->counters_size);
5087 }
5088 }
5089
5090 if (mod_cap & STATS_PX_CAP_LI) {
5091 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5092 EXTRA_COUNTERS_INIT(li->extra_counters,
5093 mod,
5094 mod->counters,
5095 mod->counters_size);
5096 }
5097 }
5098 }
5099 }
5100
Emeric Brunf8642ee2021-10-29 17:59:18 +02005101 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005102
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005103 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01005104 return 1;
5105}
5106
5107
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005108static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005109{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005110 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005111 int arg = 2;
5112
Willy Tarreau91cefca2022-05-03 17:08:29 +02005113 ctx->scope_str = 0;
5114 ctx->scope_len = 0;
5115 ctx->flags = 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01005116 ctx->field = 0; /* explicit default value */
Willy Tarreaud25fc792016-12-16 12:33:47 +01005117
Willy Tarreau2f397382019-10-09 11:43:59 +02005118 while (*args[arg]) {
5119 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005120 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005121 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005122 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005123 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005124 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02005125 else if (strcmp(args[arg], "float") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005126 ctx->flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02005127 arg++;
5128 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005129 return 0;
5130}
5131
5132
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005133static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01005134{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005135 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005136 int arg = 2;
5137
Willy Tarreau91cefca2022-05-03 17:08:29 +02005138 ctx->scope_str = 0;
5139 ctx->scope_len = 0;
5140 ctx->flags = STAT_SHNODE | STAT_SHDESC;
Willy Tarreau578d6e42019-10-09 11:00:22 +02005141
Willy Tarreau0698c802022-05-11 14:09:57 +02005142 if ((strm_li(appctx_strm(appctx))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005143 ctx->flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01005144
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005145 /* proxy is the default domain */
Willy Tarreau91cefca2022-05-03 17:08:29 +02005146 ctx->domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005147 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005148 ++args;
5149
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005150 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005151 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005152 } else if (strcmp(args[arg], "resolvers") == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02005153 ctx->domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005154 ++args;
5155 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005156 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005157 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005158 }
5159
Willy Tarreau91cefca2022-05-03 17:08:29 +02005160 if (ctx->domain == STATS_DOMAIN_PROXY
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005161 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005162 struct proxy *px;
5163
Willy Tarreau2f397382019-10-09 11:43:59 +02005164 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005165 if (px)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005166 ctx->iid = px->uuid;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005167 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02005168 ctx->iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005169
Willy Tarreau91cefca2022-05-03 17:08:29 +02005170 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02005171 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005172
Willy Tarreau91cefca2022-05-03 17:08:29 +02005173 ctx->flags |= STAT_BOUND;
5174 ctx->type = atoi(args[arg+1]);
5175 ctx->sid = atoi(args[arg+2]);
Willy Tarreau2f397382019-10-09 11:43:59 +02005176 arg += 3;
5177 }
5178
5179 while (*args[arg]) {
5180 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005181 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005182 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005183 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005184 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005185 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02005186 else if (strcmp(args[arg], "no-maint") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005187 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02005188 else if (strcmp(args[arg], "up") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005189 ctx->flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02005190 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01005191 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01005192
Willy Tarreau2b812e22016-11-22 16:18:05 +01005193 return 0;
5194}
5195
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005196static int cli_io_handler_dump_info(struct appctx *appctx)
5197{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005198 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005199 return stats_dump_info_to_buffer(appctx_sc(appctx));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005200}
5201
Willy Tarreau4596fe22022-05-17 19:07:51 +02005202/* This I/O handler runs as an applet embedded in a stream connector. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01005203 * used to send raw stats over a socket.
5204 */
5205static int cli_io_handler_dump_stat(struct appctx *appctx)
5206{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005207 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005208 return stats_dump_stat_to_buffer(appctx_sc(appctx), NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01005209}
5210
Simon Horman6f6bb382017-01-04 09:37:26 +01005211static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5212{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005213 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005214 return stats_dump_json_schema_to_buffer(appctx);
Simon Horman6f6bb382017-01-04 09:37:26 +01005215}
5216
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005217int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5218 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005219{
5220 struct stats_module *mod;
5221
5222 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5223
5224 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5225 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5226 continue;
5227
5228 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5229 }
5230
5231 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5232
5233 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5234 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5235 continue;
5236
5237 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5238 }
5239
5240 return 1;
5241
5242 alloc_failed:
5243 return 0;
5244}
5245
5246/* Initialize and allocate all extra counters for a proxy and its attached
5247 * servers/listeners with all already registered stats module
5248 */
5249int stats_allocate_proxy_counters(struct proxy *px)
5250{
5251 struct server *sv;
5252 struct listener *li;
5253
5254 if (px->cap & PR_CAP_FE) {
5255 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5256 COUNTERS_FE,
5257 STATS_PX_CAP_FE)) {
5258 return 0;
5259 }
5260 }
5261
5262 if (px->cap & PR_CAP_BE) {
5263 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5264 COUNTERS_BE,
5265 STATS_PX_CAP_BE)) {
5266 return 0;
5267 }
5268 }
5269
5270 for (sv = px->srv; sv; sv = sv->next) {
5271 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5272 COUNTERS_SV,
5273 STATS_PX_CAP_SRV)) {
5274 return 0;
5275 }
5276 }
5277
5278 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5279 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5280 COUNTERS_LI,
5281 STATS_PX_CAP_LI)) {
5282 return 0;
5283 }
5284 }
5285
5286 return 1;
5287}
5288
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005289void stats_register_module(struct stats_module *m)
5290{
5291 const uint8_t domain = stats_get_domain(m->domain_flags);
5292
Willy Tarreau2b718102021-04-21 07:32:39 +02005293 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005294 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005295}
5296
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005297static int allocate_stats_px_postcheck(void)
5298{
5299 struct stats_module *mod;
5300 size_t i = ST_F_TOTAL_FIELDS;
5301 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005302 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005303
5304 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5305
5306 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5307 if (!stat_f[STATS_DOMAIN_PROXY]) {
5308 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5309 err_code |= ERR_ALERT | ERR_FATAL;
5310 return err_code;
5311 }
5312
5313 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5314 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5315
5316 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5317 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5318 mod->stats,
5319 mod->stats_count * sizeof(struct name_desc));
5320 i += mod->stats_count;
5321 }
5322
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005323 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005324 if (!stats_allocate_proxy_counters(px)) {
5325 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5326 err_code |= ERR_ALERT | ERR_FATAL;
5327 return err_code;
5328 }
5329 }
5330
Christopher Fauletde79cd22021-01-06 07:41:56 +01005331 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005332
5333 return err_code;
5334}
5335
5336REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5337
Emeric Brunf8642ee2021-10-29 17:59:18 +02005338static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005339{
5340 struct stats_module *mod;
5341 size_t i = 0;
5342 int err_code = 0;
5343
Emeric Brunf8642ee2021-10-29 17:59:18 +02005344 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5345 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5346 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005347 err_code |= ERR_ALERT | ERR_FATAL;
5348 return err_code;
5349 }
5350
Emeric Brunf8642ee2021-10-29 17:59:18 +02005351 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5352 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005353 mod->stats,
5354 mod->stats_count * sizeof(struct name_desc));
5355 i += mod->stats_count;
5356 }
5357
Emeric Brunf8642ee2021-10-29 17:59:18 +02005358 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5359 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005360 err_code |= ERR_ALERT | ERR_FATAL;
5361 return err_code;
5362 }
5363
Christopher Fauletde79cd22021-01-06 07:41:56 +01005364 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005365
5366 return err_code;
5367}
5368
Emeric Brunf8642ee2021-10-29 17:59:18 +02005369REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005370
Christopher Fauletde79cd22021-01-06 07:41:56 +01005371static int allocate_stat_lines_per_thread(void)
5372{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005373 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005374
5375 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5376 const int domain = domains[i];
5377
5378 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5379 if (!stat_l[domain])
5380 return 0;
5381 }
5382 return 1;
5383}
5384
5385REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5386
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005387static int allocate_trash_counters(void)
5388{
5389 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005390 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005391 size_t max_counters_size = 0;
5392
5393 /* calculate the greatest counters used by any stats modules */
5394 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5395 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5396 max_counters_size = mod->counters_size > max_counters_size ?
5397 mod->counters_size : max_counters_size;
5398 }
5399 }
5400
5401 /* allocate the trash with the size of the greatest counters */
5402 if (max_counters_size) {
5403 trash_counters = malloc(max_counters_size);
5404 if (!trash_counters) {
5405 ha_alert("stats: cannot allocate trash counters for statistics\n");
5406 return 0;
5407 }
5408 }
5409
5410 return 1;
5411}
5412
5413REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5414
Christopher Fauletde79cd22021-01-06 07:41:56 +01005415static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005416{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005417 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005418
5419 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5420 const int domain = domains[i];
5421
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005422 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005423 }
5424}
5425
5426
5427REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5428
5429static void deinit_stats(void)
5430{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005431 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005432
5433 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5434 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005435
5436 if (stat_f[domain])
5437 free(stat_f[domain]);
5438 }
5439}
5440
5441REGISTER_POST_DEINIT(deinit_stats);
5442
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005443static void free_trash_counters(void)
5444{
5445 if (trash_counters)
5446 free(trash_counters);
5447}
5448
5449REGISTER_PER_THREAD_FREE(free_trash_counters);
5450
Willy Tarreau2b812e22016-11-22 16:18:05 +01005451/* register cli keywords */
5452static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005453 { { "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 +02005454 { { "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 +02005455 { { "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 },
5456 { { "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 +01005457 {{},}
5458}};
5459
Willy Tarreau0108d902018-11-25 19:14:37 +01005460INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5461
William Lallemand74c24fb2016-11-21 17:18:36 +01005462struct applet http_stats_applet = {
5463 .obj_type = OBJ_TYPE_APPLET,
5464 .name = "<STATS>", /* used for logging */
5465 .fct = http_stats_io_handler,
5466 .release = NULL,
5467};
5468
William Lallemand74c24fb2016-11-21 17:18:36 +01005469/*
5470 * Local variables:
5471 * c-indent-level: 8
5472 * c-basic-offset: 8
5473 * End:
5474 */