blob: 8a4f30a46d0174f184ff9909a5ca671f081dd855 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <pwd.h>
20#include <grp.h>
21
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020027#include <haproxy/activity.h>
Willy Tarreau91cefca2022-05-03 17:08:29 +020028#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020029#include <haproxy/backend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020031#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020032#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020033#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau55542642021-10-08 09:33:24 +020035#include <haproxy/clock.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020036#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020037#include <haproxy/debug.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020038#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/fd.h>
40#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020041#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020042#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020043#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020044#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020045#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020046#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020047#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020048#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020049#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020050#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020051#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020052#include <haproxy/pipe.h>
53#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020054#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010055#include <haproxy/resolvers.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020056#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020057#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020058#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020059#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020060#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020061#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020062#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020063#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020064#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020065#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020066#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020067#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010068
William Lallemand74c24fb2016-11-21 17:18:36 +010069
Christopher Faulet2b9b6782019-02-27 16:42:58 +010070/* status codes available for the stats admin page (strictly 4 chars length) */
71const char *stat_status_codes[STAT_STATUS_SIZE] = {
72 [STAT_STATUS_DENY] = "DENY",
73 [STAT_STATUS_DONE] = "DONE",
74 [STAT_STATUS_ERRP] = "ERRP",
75 [STAT_STATUS_EXCD] = "EXCD",
76 [STAT_STATUS_NONE] = "NONE",
77 [STAT_STATUS_PART] = "PART",
78 [STAT_STATUS_UNKN] = "UNKN",
79 [STAT_STATUS_IVAL] = "IVAL",
80};
81
Willy Tarreau0baac8c2016-11-22 16:36:53 +010082/* These are the field names for each INF_* field position. Please pay attention
83 * to always use the exact same name except that the strings for new names must
84 * be lower case or CamelCase while the enum entries must be upper case.
85 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020086const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020087 [INF_NAME] = { .name = "Name", .desc = "Product name" },
88 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
89 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
90 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
Willy Tarreau91358592021-06-15 08:08:04 +020091 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (historical, always 1)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +020092 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020093 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
94 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
95 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
William Dauchydefd1562021-01-15 22:41:38 +010096 [INF_START_TIME_SEC] = { .name = "Start_time_sec", .desc = "Start time in seconds" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020097 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
William Dauchya8766cf2021-01-15 22:41:37 +010098 [INF_MEMMAX_BYTES] = { .name = "Memmax_bytes", .desc = "Worker process's hard limit on memory usage in byes (-m on command line)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020099 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100100 [INF_POOL_ALLOC_BYTES] = { .name = "PoolAlloc_bytes", .desc = "Amount of memory allocated in pools (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200101 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100102 [INF_POOL_USED_BYTES] = { .name = "PoolUsed_bytes", .desc = "Amount of pool memory currently used (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200103 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
104 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
105 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
106 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
107 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
108 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
109 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
110 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
111 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
112 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
113 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
114 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
115 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
116 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
117 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
118 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
119 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
120 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
121 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
122 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
123 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
124 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
125 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
126 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
127 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
128 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
129 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
130 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
131 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
132 [INF_SSL_CACHE_MISSES] = { .name = "SslCacheMisses", .desc = "Total number of SSL session ID lookups that didn't find a session in the SSL session cache on this worker since started" },
133 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
134 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
135 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
136 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
137 [INF_MAX_ZLIB_MEM_USAGE] = { .name = "MaxZlibMemUsage", .desc = "Limit on the amount of memory used by HTTP compression above which it is automatically disabled (in bytes, see global.maxzlibmem)" },
138 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
139 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
140 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
141 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
142 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
143 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
144 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
145 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
146 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
147 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
148 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
149 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
150 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
151 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
152 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
Tim Duesterhus0ababda2023-03-27 15:23:44 +0200153 [INF_TOTAL_SPLICED_BYTES_OUT] = { .name = "TotalSplicedBytesOut", .desc = "Total number of bytes emitted by current worker process through a kernel pipe since started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200154 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200155 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Emeric Brun45c457a2020-07-09 23:23:34 +0200156 [INF_CUM_LOG_MSGS] = { .name = "CumRecvLogs", .desc = "Total number of log messages received by log-forwarding listeners on this worker process since started" },
Adis Nezirovicb62b78b2021-01-15 13:12:33 +0100157 [INF_BUILD_INFO] = { .name = "Build info", .desc = "Build info" },
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +0200158 [INF_TAINTED] = { .name = "Tainted", .desc = "Experimental features used" },
Willy Tarreau3c4a2972023-05-11 12:02:21 +0200159 [INF_WARNINGS] = { .name = "TotalWarnings", .desc = "Total warnings issued" },
Willy Tarreau96151022023-05-11 13:51:31 +0200160 [INF_MAXCONN_REACHED] = { .name = "MaxconnReached", .desc = "Number of times an accepted connection resulted in Maxconn being reached" },
Willy Tarreau5723b382023-05-17 09:05:20 +0200161 [INF_BOOTTIME_MS] = { .name = "BootTime_ms", .desc = "How long ago it took to parse and process the config before being ready (milliseconds)" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100162};
163
Willy Tarreaueaa55372019-10-09 07:39:11 +0200164const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200165 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
166 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100167 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
168 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100169 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
170 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200171 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
172 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
173 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
174 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
175 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
176 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
177 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
178 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
179 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
180 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
181 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
182 [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 +0200183 [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 +0200184 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
185 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
186 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
187 [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" },
188 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
189 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
190 [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 +0200191 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200192 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
193 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
194 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
195 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
196 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
197 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
198 [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)" },
199 [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 +0100200 [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 +0200201 [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" },
202 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
203 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
204 [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" },
205 [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" },
206 [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" },
207 [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" },
208 [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" },
209 [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)" },
210 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
211 [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 +0100212 [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 +0200213 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
214 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
215 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
216 [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" },
217 [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" },
218 [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)" },
219 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
220 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
221 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
222 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
223 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
224 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
225 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
226 [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)" },
227 [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" },
228 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
229 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
230 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
231 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
232 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
233 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
234 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
235 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
236 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
237 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
238 [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" },
239 [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" },
240 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
241 [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" },
242 [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 +0100243 [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 +0200244 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
245 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
246 [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" },
247 [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" },
248 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
249 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
250 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
251 [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" },
252 [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" },
253 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
254 [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 +0100255 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
256 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
257 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
258 [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 +0100259 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200260 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
261 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
262 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200263 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200264 [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 +0000265 [ST_F_AGG_SRV_CHECK_STATUS] = { .name = "agg_server_check_status", .desc = "[DEPRECATED] Backend's aggregated gauge of servers' status" },
266 [ST_F_AGG_SRV_STATUS ] = { .name = "agg_server_status", .desc = "Backend's aggregated gauge of servers' status" },
Cedric Paillete06e31e2022-12-08 09:17:01 +0000267 [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 +0100268 [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 +0100269 [ST_F_SESS_OTHER] = { .name = "sess_other", .desc = "Total number of sessions other than HTTP since process started" },
270 [ST_F_H1SESS] = { .name = "h1sess", .desc = "Total number of HTTP/1 sessions since process started" },
271 [ST_F_H2SESS] = { .name = "h2sess", .desc = "Total number of HTTP/2 sessions since process started" },
272 [ST_F_H3SESS] = { .name = "h3sess", .desc = "Total number of HTTP/3 sessions since process started" },
273 [ST_F_REQ_OTHER] = { .name = "req_other", .desc = "Total number of sessions other than HTTP processed by this object since the worker process started" },
274 [ST_F_H1REQ] = { .name = "h1req", .desc = "Total number of HTTP/1 sessions processed by this object since the worker process started" },
275 [ST_F_H2REQ] = { .name = "h2req", .desc = "Total number of hTTP/2 sessions processed by this object since the worker process started" },
276 [ST_F_H3REQ] = { .name = "h3req", .desc = "Total number of HTTP/3 sessions processed by this object since the worker process started" },
Tim Duesterhus7a38d472023-06-01 18:58:08 +0200277 [ST_F_PROTO] = { .name = "proto", .desc = "Protocol" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100278};
279
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100280/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100281THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200282
283/* description of statistics (static and dynamic) */
284static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
285static size_t stat_count[STATS_DOMAIN_COUNT];
286
287/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100288THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100289
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200290/* list of all registered stats module */
291static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
292 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Emeric Brunf8642ee2021-10-29 17:59:18 +0200293 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_RESOLVERS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200294};
295
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100296THREAD_LOCAL void *trash_counters;
Christopher Fauleta8b76842022-12-16 15:08:36 +0100297static THREAD_LOCAL struct buffer trash_chunk = BUF_NULL;
298
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100299
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200300static inline uint8_t stats_get_domain(uint32_t domain)
301{
302 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
303}
304
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200305static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
306{
307 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
308}
309
Christopher Faulet6338a082019-09-09 15:50:54 +0200310static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100311
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200312int stats_putchk(struct appctx *appctx, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +0100313{
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200314 struct stconn *sc = appctx_sc(appctx);
315 struct channel *chn = sc_ic(sc);
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +0100316 struct buffer *chk = &trash_chunk;
317
Christopher Fauletef779222018-10-31 08:47:01 +0100318 if (htx) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200319 if (chk->data >= channel_htx_recv_max(chn, htx)) {
320 sc_need_room(sc, chk->data);
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100321 return 0;
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200322 }
323 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data))) {
324 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +0100325 return 0;
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200326 }
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100327 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100328 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100329 }
330 else {
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200331 if (applet_putchk(appctx, chk) == -1)
Christopher Fauletef779222018-10-31 08:47:01 +0100332 return 0;
333 }
334 return 1;
335}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100336
Willy Tarreaucaff6312022-05-27 10:17:46 +0200337static const char *stats_scope_ptr(struct appctx *appctx, struct stconn *sc)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100338{
Willy Tarreau91cefca2022-05-03 17:08:29 +0200339 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +0200340 struct channel *req = sc_oc(sc);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200341 struct htx *htx = htxbuf(&req->buf);
342 struct htx_blk *blk;
343 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100344
Christopher Fauletb7f88902019-07-15 21:56:43 +0200345 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100346 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200347 ALREADY_CHECKED(blk);
348 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Willy Tarreau91cefca2022-05-03 17:08:29 +0200349 return uri.ptr + ctx->scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100350}
351
William Lallemand74c24fb2016-11-21 17:18:36 +0100352/*
353 * http_stats_io_handler()
354 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
355 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100356 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100357 * -> stats_dump_html_head() // emits the HTML headers
358 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
359 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
360 * -> stats_dump_html_px_hdr()
361 * -> stats_dump_fe_stats()
362 * -> stats_dump_li_stats()
363 * -> stats_dump_sv_stats()
364 * -> stats_dump_be_stats()
365 * -> stats_dump_html_px_end()
366 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100367 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100368 */
369
370
Christopher Fauleta8b76842022-12-16 15:08:36 +0100371/* Dumps the stats CSV header to the local trash buffer. The caller is
372 * responsible for clearing it if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +0100373 * NOTE: Some tools happen to rely on the field position instead of its name,
374 * so please only append new fields at the end, never in the middle.
375 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200376static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100377{
378 int field;
379
Christopher Fauleta8b76842022-12-16 15:08:36 +0100380 chunk_appendf(&trash_chunk, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200381 if (stat_f[domain]) {
382 for (field = 0; field < stat_count[domain]; ++field) {
Christopher Fauleta8b76842022-12-16 15:08:36 +0100383 chunk_appendf(&trash_chunk, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100384
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200385 /* print special delimiter on proxy stats to mark end of
386 static fields */
387 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
Christopher Fauleta8b76842022-12-16 15:08:36 +0100388 chunk_appendf(&trash_chunk, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200389 }
390 }
391
Christopher Fauleta8b76842022-12-16 15:08:36 +0100392 chunk_appendf(&trash_chunk, "\n");
William Lallemand74c24fb2016-11-21 17:18:36 +0100393}
394
William Lallemand74c24fb2016-11-21 17:18:36 +0100395/* Emits a stats field without any surrounding element and properly encoded to
396 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
397 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200398int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100399{
400 switch (field_format(f, 0)) {
401 case FF_EMPTY: return 1;
402 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
403 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
404 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
405 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200406 case FF_FLT: {
407 size_t prev_data = out->data;
408 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
409 return out->data;
410 }
Emeric Brun36e54272024-01-23 15:44:32 +0100411 case FF_STR: return csv_enc_append(field_str(f, 0), 1, 2, out) != NULL;
William Lallemand74c24fb2016-11-21 17:18:36 +0100412 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
413 }
414}
415
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200416const char *field_to_html_str(const struct field *f)
417{
418 switch (field_format(f, 0)) {
419 case FF_S32: return U2H(f->u.s32);
420 case FF_S64: return U2H(f->u.s64);
421 case FF_U64: return U2H(f->u.u64);
422 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200423 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200424 case FF_STR: return field_str(f, 0);
425 case FF_EMPTY:
426 default:
427 return "";
428 }
429}
430
William Lallemand74c24fb2016-11-21 17:18:36 +0100431/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
432 * output is supposed to be used on its own line. Returns non-zero on success, 0
433 * if the buffer is full.
434 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200435int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100436{
437 switch (field_format(f, 0)) {
438 case FF_EMPTY: return 1;
439 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
440 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
441 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
442 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200443 case FF_FLT: {
444 size_t prev_data = out->data;
445 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
446 return out->data;
447 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100448 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
449 default: return chunk_appendf(out, "%08x:?", f->type);
450 }
451}
452
Simon Horman05ee2132017-01-04 09:37:25 +0100453/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
454 * the recommendation for interoperable integers in section 6 of RFC 7159.
455 */
456#define JSON_INT_MAX ((1ULL << 53) - 1)
457#define JSON_INT_MIN (0 - JSON_INT_MAX)
458
459/* Emits a stats field value and its type in JSON.
460 * Returns non-zero on success, 0 on error.
461 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200462int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100463{
464 int old_len;
465 char buf[20];
466 const char *type, *value = buf, *quote = "";
467
468 switch (field_format(f, 0)) {
469 case FF_EMPTY: return 1;
470 case FF_S32: type = "\"s32\"";
471 snprintf(buf, sizeof(buf), "%d", f->u.s32);
472 break;
473 case FF_U32: type = "\"u32\"";
474 snprintf(buf, sizeof(buf), "%u", f->u.u32);
475 break;
476 case FF_S64: type = "\"s64\"";
477 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
478 return 0;
479 type = "\"s64\"";
480 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
481 break;
482 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
483 return 0;
484 type = "\"u64\"";
485 snprintf(buf, sizeof(buf), "%llu",
486 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200487 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200488 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200489 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100490 break;
491 case FF_STR: type = "\"str\"";
492 value = field_str(f, 0);
493 quote = "\"";
494 break;
495 default: snprintf(buf, sizeof(buf), "%u", f->type);
496 type = buf;
497 value = "unknown";
498 quote = "\"";
499 break;
500 }
501
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200502 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100503 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
504 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200505 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100506}
507
William Lallemand74c24fb2016-11-21 17:18:36 +0100508/* Emits an encoding of the field type on 3 characters followed by a delimiter.
509 * Returns non-zero on success, 0 if the buffer is full.
510 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200511int stats_emit_field_tags(struct buffer *out, const struct field *f,
512 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100513{
514 char origin, nature, scope;
515
516 switch (field_origin(f, 0)) {
517 case FO_METRIC: origin = 'M'; break;
518 case FO_STATUS: origin = 'S'; break;
519 case FO_KEY: origin = 'K'; break;
520 case FO_CONFIG: origin = 'C'; break;
521 case FO_PRODUCT: origin = 'P'; break;
522 default: origin = '?'; break;
523 }
524
525 switch (field_nature(f, 0)) {
526 case FN_GAUGE: nature = 'G'; break;
527 case FN_LIMIT: nature = 'L'; break;
528 case FN_MIN: nature = 'm'; break;
529 case FN_MAX: nature = 'M'; break;
530 case FN_RATE: nature = 'R'; break;
531 case FN_COUNTER: nature = 'C'; break;
532 case FN_DURATION: nature = 'D'; break;
533 case FN_AGE: nature = 'A'; break;
534 case FN_TIME: nature = 'T'; break;
535 case FN_NAME: nature = 'N'; break;
536 case FN_OUTPUT: nature = 'O'; break;
537 case FN_AVG: nature = 'a'; break;
538 default: nature = '?'; break;
539 }
540
541 switch (field_scope(f, 0)) {
542 case FS_PROCESS: scope = 'P'; break;
543 case FS_SERVICE: scope = 'S'; break;
544 case FS_SYSTEM: scope = 's'; break;
545 case FS_CLUSTER: scope = 'C'; break;
546 default: scope = '?'; break;
547 }
548
549 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
550}
551
Simon Horman05ee2132017-01-04 09:37:25 +0100552/* Emits an encoding of the field type as JSON.
553 * Returns non-zero on success, 0 if the buffer is full.
554 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200555int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100556{
557 const char *origin, *nature, *scope;
558 int old_len;
559
560 switch (field_origin(f, 0)) {
561 case FO_METRIC: origin = "Metric"; break;
562 case FO_STATUS: origin = "Status"; break;
563 case FO_KEY: origin = "Key"; break;
564 case FO_CONFIG: origin = "Config"; break;
565 case FO_PRODUCT: origin = "Product"; break;
566 default: origin = "Unknown"; break;
567 }
568
569 switch (field_nature(f, 0)) {
570 case FN_GAUGE: nature = "Gauge"; break;
571 case FN_LIMIT: nature = "Limit"; break;
572 case FN_MIN: nature = "Min"; break;
573 case FN_MAX: nature = "Max"; break;
574 case FN_RATE: nature = "Rate"; break;
575 case FN_COUNTER: nature = "Counter"; break;
576 case FN_DURATION: nature = "Duration"; break;
577 case FN_AGE: nature = "Age"; break;
578 case FN_TIME: nature = "Time"; break;
579 case FN_NAME: nature = "Name"; break;
580 case FN_OUTPUT: nature = "Output"; break;
581 case FN_AVG: nature = "Avg"; break;
582 default: nature = "Unknown"; break;
583 }
584
585 switch (field_scope(f, 0)) {
586 case FS_PROCESS: scope = "Process"; break;
587 case FS_SERVICE: scope = "Service"; break;
588 case FS_SYSTEM: scope = "System"; break;
589 case FS_CLUSTER: scope = "Cluster"; break;
590 default: scope = "Unknown"; break;
591 }
592
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200593 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100594 chunk_appendf(out, "\"tags\":{"
595 "\"origin\":\"%s\","
596 "\"nature\":\"%s\","
597 "\"scope\":\"%s\""
598 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200599 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100600}
William Lallemand74c24fb2016-11-21 17:18:36 +0100601
602/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200603static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200604 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100605 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100606{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100607 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100608 int field;
609
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200610 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100611 if (!stats_emit_raw_data_field(out, &stats[field]))
612 return 0;
613 if (!chunk_strcat(out, ","))
614 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200615
616 /* print special delimiter on proxy stats to mark end of
617 static fields */
618 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
619 if (!chunk_strcat(out, "-,"))
620 return 0;
621 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100622 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200623
William Lallemand74c24fb2016-11-21 17:18:36 +0100624 chunk_strcat(out, "\n");
625 return 1;
626}
627
628/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200629static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200630 const struct field *stats,
631 size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100632 struct show_stat_ctx * ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100633{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100634 int flags = ctx->flags;
635 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100636 int field;
637
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200638 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100639 if (!stats[field].type)
640 continue;
641
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200642 switch (domain) {
643 case STATS_DOMAIN_PROXY:
644 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
645 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
646 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
647 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
648 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
649 '?',
650 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
651 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200652 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200653 stats[ST_F_PID].u.u32);
654 break;
655
Emeric Brunf8642ee2021-10-29 17:59:18 +0200656 case STATS_DOMAIN_RESOLVERS:
657 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200658 stat_f[domain][field].name);
659 break;
660
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200661 default:
662 break;
663 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100664
665 if (!stats_emit_field_tags(out, &stats[field], ':'))
666 return 0;
667 if (!stats_emit_typed_data_field(out, &stats[field]))
668 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200669
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200670 if (flags & STAT_SHOW_FDESC &&
671 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200672 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200673 }
674
William Lallemand74c24fb2016-11-21 17:18:36 +0100675 if (!chunk_strcat(out, "\n"))
676 return 0;
677 }
678 return 1;
679}
680
Simon Horman05ee2132017-01-04 09:37:25 +0100681/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200682static int stats_dump_json_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100683 const struct field *info,
684 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100685{
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100686 int started = (ctx->field) ? 1 : 0;
687 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100688
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100689 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100690 return 0;
691
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100692 for (; ctx->field < INF_TOTAL_FIELDS; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100693 int old_len;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100694 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100695
696 if (!field_format(info, field))
697 continue;
698
699 if (started && !chunk_strcat(out, ","))
700 goto err;
701 started = 1;
702
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200703 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100704 chunk_appendf(out,
705 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
706 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200707 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100708 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200709 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100710 goto err;
711
712 if (!stats_emit_json_field_tags(out, &info[field]))
713 goto err;
714
715 if (!stats_emit_json_data_field(out, &info[field]))
716 goto err;
717
718 if (!chunk_strcat(out, "}"))
719 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100720 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100721 }
722
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200723 if (!chunk_strcat(out, "]\n"))
Simon Horman05ee2132017-01-04 09:37:25 +0100724 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100725 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100726 return 1;
727
728err:
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100729 if (!ready_data) {
730 /* not enough buffer space for a single entry.. */
731 chunk_reset(out);
732 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
733 return 0; /* hard error */
734 }
735 /* push ready data and wait for a new buffer to complete the dump */
736 out->data = ready_data;
737 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100738}
739
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200740static void stats_print_proxy_field_json(struct buffer *out,
741 const struct field *stat,
742 const char *name,
743 int pos,
744 uint32_t field_type,
745 uint32_t iid,
746 uint32_t sid,
747 uint32_t pid)
748{
749 const char *obj_type;
750 switch (field_type) {
751 case STATS_TYPE_FE: obj_type = "Frontend"; break;
752 case STATS_TYPE_BE: obj_type = "Backend"; break;
753 case STATS_TYPE_SO: obj_type = "Listener"; break;
754 case STATS_TYPE_SV: obj_type = "Server"; break;
755 default: obj_type = "Unknown"; break;
756 }
757
758 chunk_appendf(out,
759 "{"
760 "\"objType\":\"%s\","
761 "\"proxyId\":%u,"
762 "\"id\":%u,"
763 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
764 "\"processNum\":%u,",
765 obj_type, iid, sid, pos, name, pid);
766}
767
Emeric Brunf8642ee2021-10-29 17:59:18 +0200768static void stats_print_rslv_field_json(struct buffer *out,
769 const struct field *stat,
770 const char *name,
771 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200772{
773 chunk_appendf(out,
774 "{"
775 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
776 pos, name);
777}
778
779
Simon Horman05ee2132017-01-04 09:37:25 +0100780/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200781static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200782 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100783 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100784{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100785 int flags = ctx->flags;
786 int domain = ctx->domain;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100787 int started = (ctx->field) ? 1 : 0;
788 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100789
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100790 if (!started && (flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100791 return 0;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100792 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100793 return 0;
794
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100795 for (; ctx->field < stats_count; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100796 int old_len;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100797 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100798
799 if (!stats[field].type)
800 continue;
801
802 if (started && !chunk_strcat(out, ","))
803 goto err;
804 started = 1;
805
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200806 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200807 if (domain == STATS_DOMAIN_PROXY) {
808 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200809 stat_f[domain][field].name,
810 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200811 stats[ST_F_TYPE].u.u32,
812 stats[ST_F_IID].u.u32,
813 stats[ST_F_SID].u.u32,
814 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200815 } else if (domain == STATS_DOMAIN_RESOLVERS) {
816 stats_print_rslv_field_json(out, &stats[field],
817 stat_f[domain][field].name,
818 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200819 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200820
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200821 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100822 goto err;
823
824 if (!stats_emit_json_field_tags(out, &stats[field]))
825 goto err;
826
827 if (!stats_emit_json_data_field(out, &stats[field]))
828 goto err;
829
830 if (!chunk_strcat(out, "}"))
831 goto err;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100832 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100833 }
834
835 if (!chunk_strcat(out, "]"))
836 goto err;
837
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100838 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100839 return 1;
840
841err:
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100842 if (!ready_data) {
843 /* not enough buffer space for a single entry.. */
844 chunk_reset(out);
845 if (ctx->flags & STAT_STARTED)
846 chunk_strcat(out, ",");
847 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
848 return 0; /* hard error */
849 }
850 /* push ready data and wait for a new buffer to complete the dump */
851 out->data = ready_data;
852 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100853}
854
William Lallemand74c24fb2016-11-21 17:18:36 +0100855/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200856 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200857 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
858 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
859 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100860 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200861static int stats_dump_fields_html(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100862 const struct field *stats,
863 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100864{
Willy Tarreau83061a82018-07-13 11:56:34 +0200865 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200866 struct stats_module *mod;
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100867 int flags = ctx->flags;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200868 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100869
870 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
871 chunk_appendf(out,
872 /* name, queue */
873 "<tr class=\"frontend\">");
874
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200875 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100876 /* Column sub-heading for Enable or Disable server */
877 chunk_appendf(out, "<td></td>");
878 }
879
880 chunk_appendf(out,
881 "<td class=ac>"
882 "<a name=\"%s/Frontend\"></a>"
883 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
884 "<td colspan=3></td>"
885 "",
886 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
887
888 chunk_appendf(out,
889 /* sessions rate : current */
890 "<td><u>%s<div class=tips><table class=det>"
891 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
892 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
893 "",
894 U2H(stats[ST_F_RATE].u.u32),
895 U2H(stats[ST_F_CONN_RATE].u.u32),
896 U2H(stats[ST_F_RATE].u.u32));
897
898 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
899 chunk_appendf(out,
900 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
901 U2H(stats[ST_F_REQ_RATE].u.u32));
902
903 chunk_appendf(out,
904 "</table></div></u></td>"
905 /* sessions rate : max */
906 "<td><u>%s<div class=tips><table class=det>"
907 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
908 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
909 "",
910 U2H(stats[ST_F_RATE_MAX].u.u32),
911 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
912 U2H(stats[ST_F_RATE_MAX].u.u32));
913
914 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
915 chunk_appendf(out,
916 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
917 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
918
919 chunk_appendf(out,
920 "</table></div></u></td>"
921 /* sessions rate : limit */
922 "<td>%s</td>",
923 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
924
925 chunk_appendf(out,
926 /* sessions: current, max, limit, total */
927 "<td>%s</td><td>%s</td><td>%s</td>"
928 "<td><u>%s<div class=tips><table class=det>"
929 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
930 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
931 "",
932 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
933 U2H(stats[ST_F_STOT].u.u64),
934 U2H(stats[ST_F_CONN_TOT].u.u64),
935 U2H(stats[ST_F_STOT].u.u64));
936
937 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
938 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
939 chunk_appendf(out,
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100940 "<tr><th>- HTTP/1 sessions:</th><td>%s</td></tr>"
941 "<tr><th>- HTTP/2 sessions:</th><td>%s</td></tr>"
942 "<tr><th>- HTTP/3 sessions:</th><td>%s</td></tr>"
943 "<tr><th>- other sessions:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100944 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100945 "<tr><th>- HTTP/1 requests:</th><td>%s</td></tr>"
946 "<tr><th>- HTTP/2 requests:</th><td>%s</td></tr>"
947 "<tr><th>- HTTP/3 requests:</th><td>%s</td></tr>"
948 "<tr><th>- other requests:</th><td>%s</td></tr>"
949 "",
950 U2H(stats[ST_F_H1SESS].u.u64),
951 U2H(stats[ST_F_H2SESS].u.u64),
952 U2H(stats[ST_F_H3SESS].u.u64),
953 U2H(stats[ST_F_SESS_OTHER].u.u64),
954 U2H(stats[ST_F_REQ_TOT].u.u64),
955 U2H(stats[ST_F_H1REQ].u.u64),
956 U2H(stats[ST_F_H2REQ].u.u64),
957 U2H(stats[ST_F_H3REQ].u.u64),
958 U2H(stats[ST_F_REQ_OTHER].u.u64));
959
960 chunk_appendf(out,
William Lallemand74c24fb2016-11-21 17:18:36 +0100961 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
962 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
963 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
964 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
965 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
966 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
967 "<tr><th>- other responses:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100968 "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100969 U2H(stats[ST_F_HRSP_1XX].u.u64),
970 U2H(stats[ST_F_HRSP_2XX].u.u64),
971 U2H(stats[ST_F_COMP_RSP].u.u64),
972 stats[ST_F_HRSP_2XX].u.u64 ?
973 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
974 U2H(stats[ST_F_HRSP_3XX].u.u64),
975 U2H(stats[ST_F_HRSP_4XX].u.u64),
976 U2H(stats[ST_F_HRSP_5XX].u.u64),
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100977 U2H(stats[ST_F_HRSP_OTHER].u.u64));
978
979 chunk_appendf(out,
980 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
981 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
982 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
983 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
984 "<tr><th>Internal errors:</th><td>%s</td></tr>"
985 "",
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200986 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100987 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
988 U2H(stats[ST_F_CACHE_HITS].u.u64),
989 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
990 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100991 U2H(stats[ST_F_WREW].u.u64),
992 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100993 }
994
995 chunk_appendf(out,
996 "</table></div></u></td>"
997 /* sessions: lbtot, lastsess */
998 "<td></td><td></td>"
999 /* bytes : in */
1000 "<td>%s</td>"
1001 "",
1002 U2H(stats[ST_F_BIN].u.u64));
1003
1004 chunk_appendf(out,
1005 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1006 "<td>%s%s<div class=tips><table class=det>"
1007 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1008 "<tr><th>Compression in:</th><td>%s</td></tr>"
1009 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1010 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1011 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1012 "</table></div>%s</td>",
1013 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1014 U2H(stats[ST_F_BOUT].u.u64),
1015 U2H(stats[ST_F_BOUT].u.u64),
1016 U2H(stats[ST_F_COMP_IN].u.u64),
1017 U2H(stats[ST_F_COMP_OUT].u.u64),
1018 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1019 U2H(stats[ST_F_COMP_BYP].u.u64),
1020 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1021 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,
1022 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1023
1024 chunk_appendf(out,
1025 /* denied: req, resp */
1026 "<td>%s</td><td>%s</td>"
1027 /* errors : request, connect, response */
1028 "<td>%s</td><td></td><td></td>"
1029 /* warnings: retries, redispatches */
1030 "<td></td><td></td>"
1031 /* server status : reflect frontend status */
1032 "<td class=ac>%s</td>"
1033 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001034 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001035 "",
1036 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1037 U2H(stats[ST_F_EREQ].u.u64),
1038 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001039
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001040 if (flags & STAT_SHMODULES) {
1041 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001042 chunk_appendf(out, "<td>");
1043
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001044 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001045 chunk_appendf(out,
1046 "<u>%s<div class=tips><table class=det>",
1047 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001048 for (j = 0; j < mod->stats_count; ++j) {
1049 chunk_appendf(out,
1050 "<tr><th>%s</th><td>%s</td></tr>",
1051 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1052 ++i;
1053 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001054 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001055 } else {
1056 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001057 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001058
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001059 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001060 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001061 }
1062
1063 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001064 }
1065 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
1066 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001067 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001068 /* Column sub-heading for Enable or Disable server */
1069 chunk_appendf(out, "<td></td>");
1070 }
1071
1072 chunk_appendf(out,
1073 /* frontend name, listener name */
1074 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1075 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1076 "",
1077 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001078 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001079 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1080
Willy Tarreau708c4162019-10-09 10:19:16 +02001081 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001082 chunk_appendf(out, "<div class=tips>");
1083
Willy Tarreau90807112020-02-25 08:16:33 +01001084 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001085 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1086 else if (*field_str(stats, ST_F_ADDR) == '[')
1087 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1088 else if (*field_str(stats, ST_F_ADDR))
1089 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1090
Willy Tarreau4cfb0012023-05-11 14:49:43 +02001091 chunk_appendf(out, "proto=%s, ", field_str(stats, ST_F_PROTO));
1092
William Lallemand74c24fb2016-11-21 17:18:36 +01001093 /* id */
1094 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1095 }
1096
1097 chunk_appendf(out,
1098 /* queue */
1099 "%s</td><td colspan=3></td>"
1100 /* sessions rate: current, max, limit */
1101 "<td colspan=3>&nbsp;</td>"
1102 /* sessions: current, max, limit, total, lbtot, lastsess */
1103 "<td>%s</td><td>%s</td><td>%s</td>"
1104 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1105 /* bytes: in, out */
1106 "<td>%s</td><td>%s</td>"
1107 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001108 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001109 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1110 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1111
1112 chunk_appendf(out,
1113 /* denied: req, resp */
1114 "<td>%s</td><td>%s</td>"
1115 /* errors: request, connect, response */
1116 "<td>%s</td><td></td><td></td>"
1117 /* warnings: retries, redispatches */
1118 "<td></td><td></td>"
1119 /* server status: reflect listener status */
1120 "<td class=ac>%s</td>"
1121 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001122 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001123 "",
1124 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1125 U2H(stats[ST_F_EREQ].u.u64),
1126 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001127
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001128 if (flags & STAT_SHMODULES) {
1129 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001130 chunk_appendf(out, "<td>");
1131
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001132 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001133 chunk_appendf(out,
1134 "<u>%s<div class=tips><table class=det>",
1135 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001136 for (j = 0; j < mod->stats_count; ++j) {
1137 chunk_appendf(out,
1138 "<tr><th>%s</th><td>%s</td></tr>",
1139 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1140 ++i;
1141 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001142 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001143 } else {
1144 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001145 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001146
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001147 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001148 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001149 }
1150
1151 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001152 }
1153 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1154 const char *style;
1155
1156 /* determine the style to use depending on the server's state,
1157 * its health and weight. There isn't a 1-to-1 mapping between
1158 * state and styles for the cases where the server is (still)
1159 * up. The reason is that we don't want to report nolb and
1160 * drain with the same color.
1161 */
1162
1163 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1164 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1165 style = "down";
1166 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001167 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001168 style = "going_up";
1169 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001170 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1171 style = "draining";
1172 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001173 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001174 style = "going_down";
1175 }
1176 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1177 style = "nolb";
1178 }
1179 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1180 style = "no_check";
1181 }
1182 else if (!stats[ST_F_CHKFAIL].type ||
1183 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1184 /* no check or max health = UP */
1185 if (stats[ST_F_WEIGHT].u.u32)
1186 style = "up";
1187 else
1188 style = "draining";
1189 }
1190 else {
1191 style = "going_down";
1192 }
1193
Willy Tarreau7b524852020-08-11 10:26:36 +02001194 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001195 chunk_appendf(out, "<tr class=\"maintain\">");
1196 else
1197 chunk_appendf(out,
1198 "<tr class=\"%s_%s\">",
1199 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1200
1201
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001202 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001203 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001204 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1205 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001206 field_str(stats, ST_F_SVNAME));
1207
1208 chunk_appendf(out,
1209 "<td class=ac><a name=\"%s/%s\"></a>%s"
1210 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1211 "",
1212 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001213 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001214 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1215
Willy Tarreau708c4162019-10-09 10:19:16 +02001216 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001217 chunk_appendf(out, "<div class=tips>");
1218
Willy Tarreau90807112020-02-25 08:16:33 +01001219 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001220 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1221 else if (*field_str(stats, ST_F_ADDR) == '[')
1222 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1223 else if (*field_str(stats, ST_F_ADDR))
1224 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1225
1226 /* id */
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01001227 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 +01001228
1229 /* cookie */
1230 if (stats[ST_F_COOKIE].type) {
1231 chunk_appendf(out, ", cookie: '");
1232 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1233 chunk_htmlencode(out, &src);
1234 chunk_appendf(out, "'");
1235 }
1236
1237 chunk_appendf(out, "</div>");
1238 }
1239
1240 chunk_appendf(out,
1241 /* queue : current, max, limit */
1242 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1243 /* sessions rate : current, max, limit */
1244 "<td>%s</td><td>%s</td><td></td>"
1245 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001246 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001247 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1248 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1249
1250 chunk_appendf(out,
1251 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001252 "<td><u>%s<div class=tips>"
1253 "<table class=det>"
1254 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001255 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001256 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001257 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1258 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001259 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001260 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1261 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1262 "</table></div></u>"
1263 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001264 "<td><u>%s<div class=tips><table class=det>"
1265 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1266 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001267 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001268 U2H(stats[ST_F_SCUR].u.u32),
1269 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1270 U2H(stats[ST_F_SRV_ICUR].u.u32),
1271 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1272 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001273 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001274
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001275 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1276 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1277 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001278 U2H(stats[ST_F_STOT].u.u64),
1279 U2H(stats[ST_F_STOT].u.u64));
1280
1281 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1282 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001283 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001284 "<tr><th>New connections:</th><td>%s</td></tr>"
1285 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001286 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001287 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1288 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1289 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1290 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1291 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1292 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001293 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001294 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001295 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001296 U2H(stats[ST_F_CONNECT].u.u64),
1297 U2H(stats[ST_F_REUSE].u.u64),
1298 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1299 (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 +02001300 U2H(stats[ST_F_REQ_TOT].u.u64),
1301 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1302 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1303 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1304 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1305 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1306 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1307 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1308 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1309 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1310 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1311 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1312 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001313 U2H(stats[ST_F_WREW].u.u64),
1314 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001315 }
1316
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001317 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1318 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1319 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1320 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1321 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001322 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001323 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1324 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1325 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1326 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001327
1328 chunk_appendf(out,
1329 "</table></div></u></td>"
1330 /* sessions: lbtot, last */
1331 "<td>%s</td><td>%s</td>",
1332 U2H(stats[ST_F_LBTOT].u.u64),
1333 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1334
1335 chunk_appendf(out,
1336 /* bytes : in, out */
1337 "<td>%s</td><td>%s</td>"
1338 /* denied: req, resp */
1339 "<td></td><td>%s</td>"
1340 /* errors : request, connect */
1341 "<td></td><td>%s</td>"
1342 /* errors : response */
1343 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1344 /* warnings: retries, redispatches */
1345 "<td>%lld</td><td>%lld</td>"
1346 "",
1347 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1348 U2H(stats[ST_F_DRESP].u.u64),
1349 U2H(stats[ST_F_ECON].u.u64),
1350 U2H(stats[ST_F_ERESP].u.u64),
1351 (long long)stats[ST_F_CLI_ABRT].u.u64,
1352 (long long)stats[ST_F_SRV_ABRT].u.u64,
1353 (long long)stats[ST_F_WRETR].u.u64,
1354 (long long)stats[ST_F_WREDIS].u.u64);
1355
1356 /* status, last change */
1357 chunk_appendf(out, "<td class=ac>");
1358
1359 /* FIXME!!!!
1360 * LASTCHG should contain the last change for *this* server and must be computed
1361 * properly above, as was done below, ie: this server if maint, otherwise ref server
1362 * if tracking. Note that ref is either local or remote depending on tracking.
1363 */
1364
1365
Willy Tarreau7b524852020-08-11 10:26:36 +02001366 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001367 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1368 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001369 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001370 chunk_strcat(out, "<i>no check</i>");
1371 }
1372 else {
1373 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 +02001374 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001375 if (stats[ST_F_CHECK_HEALTH].u.u32)
1376 chunk_strcat(out, " &uarr;");
1377 }
1378 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1379 chunk_strcat(out, " &darr;");
1380 }
1381
Willy Tarreau7b524852020-08-11 10:26:36 +02001382 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001383 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1384 chunk_appendf(out,
1385 "</td><td class=ac><u> %s",
1386 field_str(stats, ST_F_AGENT_STATUS));
1387
1388 if (stats[ST_F_AGENT_CODE].type)
1389 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1390
1391 if (stats[ST_F_AGENT_DURATION].type)
1392 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1393
1394 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1395
1396 if (*field_str(stats, ST_F_LAST_AGT)) {
1397 chunk_appendf(out, ": ");
1398 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1399 chunk_htmlencode(out, &src);
1400 }
1401 chunk_appendf(out, "</div></u>");
1402 }
1403 else if (stats[ST_F_CHECK_STATUS].type) {
1404 chunk_appendf(out,
1405 "</td><td class=ac><u> %s",
1406 field_str(stats, ST_F_CHECK_STATUS));
1407
1408 if (stats[ST_F_CHECK_CODE].type)
1409 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1410
1411 if (stats[ST_F_CHECK_DURATION].type)
1412 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1413
1414 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1415
1416 if (*field_str(stats, ST_F_LAST_CHK)) {
1417 chunk_appendf(out, ": ");
1418 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1419 chunk_htmlencode(out, &src);
1420 }
1421 chunk_appendf(out, "</div></u>");
1422 }
1423 else
1424 chunk_appendf(out, "</td><td>");
1425
1426 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001427 /* weight / uweight */
1428 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001429 /* act, bck */
1430 "<td class=ac>%s</td><td class=ac>%s</td>"
1431 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001432 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001433 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1434 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1435
1436 /* check failures: unique, fatal, down time */
1437 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1438 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1439 }
1440 else if (stats[ST_F_CHKFAIL].type) {
1441 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1442
1443 if (stats[ST_F_HANAFAIL].type)
1444 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1445
1446 chunk_appendf(out,
1447 "<div class=tips>Failed Health Checks%s</div></u></td>"
1448 "<td>%lld</td><td>%s</td>"
1449 "",
1450 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1451 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1452 }
1453 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1454 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1455 chunk_appendf(out,
1456 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1457 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1458 }
1459 else
1460 chunk_appendf(out, "<td colspan=3></td>");
1461
1462 /* throttle */
1463 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001464 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001465 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001466 chunk_appendf(out, "<td class=ac>-</td>");
1467
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001468 if (flags & STAT_SHMODULES) {
1469 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001470 chunk_appendf(out, "<td>");
1471
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001472 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001473 chunk_appendf(out,
1474 "<u>%s<div class=tips><table class=det>",
1475 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001476 for (j = 0; j < mod->stats_count; ++j) {
1477 chunk_appendf(out,
1478 "<tr><th>%s</th><td>%s</td></tr>",
1479 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1480 ++i;
1481 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001482 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001483 } else {
1484 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001485 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001486
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001487 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001488 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001489 }
1490
1491 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001492 }
1493 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1494 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001495 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001496 /* Column sub-heading for Enable or Disable server */
1497 chunk_appendf(out, "<td></td>");
1498 }
1499 chunk_appendf(out,
1500 "<td class=ac>"
1501 /* name */
1502 "%s<a name=\"%s/Backend\"></a>"
1503 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1504 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001505 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001506 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1507
Willy Tarreau708c4162019-10-09 10:19:16 +02001508 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001509 /* balancing */
1510 chunk_appendf(out, "<div class=tips>balancing: %s",
1511 field_str(stats, ST_F_ALGO));
1512
1513 /* cookie */
1514 if (stats[ST_F_COOKIE].type) {
1515 chunk_appendf(out, ", cookie: '");
1516 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1517 chunk_htmlencode(out, &src);
1518 chunk_appendf(out, "'");
1519 }
1520 chunk_appendf(out, "</div>");
1521 }
1522
1523 chunk_appendf(out,
1524 "%s</td>"
1525 /* queue : current, max */
1526 "<td>%s</td><td>%s</td><td></td>"
1527 /* sessions rate : current, max, limit */
1528 "<td>%s</td><td>%s</td><td></td>"
1529 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001530 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001531 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1532 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1533
1534 chunk_appendf(out,
1535 /* sessions: current, max, limit, total */
1536 "<td>%s</td><td>%s</td><td>%s</td>"
1537 "<td><u>%s<div class=tips><table class=det>"
1538 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1539 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001540 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 +01001541 U2H(stats[ST_F_STOT].u.u64),
1542 U2H(stats[ST_F_STOT].u.u64));
1543
1544 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1545 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1546 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001547 "<tr><th>New connections:</th><td>%s</td></tr>"
1548 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001549 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1550 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1551 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1552 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1553 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1554 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1555 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1556 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001557 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1558 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001559 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001560 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001561 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001562 U2H(stats[ST_F_CONNECT].u.u64),
1563 U2H(stats[ST_F_REUSE].u.u64),
1564 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1565 (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 +01001566 U2H(stats[ST_F_REQ_TOT].u.u64),
1567 U2H(stats[ST_F_HRSP_1XX].u.u64),
1568 U2H(stats[ST_F_HRSP_2XX].u.u64),
1569 U2H(stats[ST_F_COMP_RSP].u.u64),
1570 stats[ST_F_HRSP_2XX].u.u64 ?
1571 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1572 U2H(stats[ST_F_HRSP_3XX].u.u64),
1573 U2H(stats[ST_F_HRSP_4XX].u.u64),
1574 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001575 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001576 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1577 U2H(stats[ST_F_CACHE_HITS].u.u64),
1578 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1579 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001580 U2H(stats[ST_F_WREW].u.u64),
1581 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001582 }
1583
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001584 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1585 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1586 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1587 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1588 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001589 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001590 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1591 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1592 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1593 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001594
1595 chunk_appendf(out,
1596 "</table></div></u></td>"
1597 /* sessions: lbtot, last */
1598 "<td>%s</td><td>%s</td>"
1599 /* bytes: in */
1600 "<td>%s</td>"
1601 "",
1602 U2H(stats[ST_F_LBTOT].u.u64),
1603 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1604 U2H(stats[ST_F_BIN].u.u64));
1605
1606 chunk_appendf(out,
1607 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1608 "<td>%s%s<div class=tips><table class=det>"
1609 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1610 "<tr><th>Compression in:</th><td>%s</td></tr>"
1611 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1612 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1613 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1614 "</table></div>%s</td>",
1615 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1616 U2H(stats[ST_F_BOUT].u.u64),
1617 U2H(stats[ST_F_BOUT].u.u64),
1618 U2H(stats[ST_F_COMP_IN].u.u64),
1619 U2H(stats[ST_F_COMP_OUT].u.u64),
1620 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1621 U2H(stats[ST_F_COMP_BYP].u.u64),
1622 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1623 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,
1624 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1625
1626 chunk_appendf(out,
1627 /* denied: req, resp */
1628 "<td>%s</td><td>%s</td>"
1629 /* errors : request, connect */
1630 "<td></td><td>%s</td>"
1631 /* errors : response */
1632 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1633 /* warnings: retries, redispatches */
1634 "<td>%lld</td><td>%lld</td>"
1635 /* backend status: reflect backend status (up/down): we display UP
1636 * if the backend has known working servers or if it has no server at
1637 * all (eg: for stats). Then we display the total weight, number of
1638 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001639 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001640 "<td class=ac>%d</td><td class=ac>%d</td>"
1641 "",
1642 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1643 U2H(stats[ST_F_ECON].u.u64),
1644 U2H(stats[ST_F_ERESP].u.u64),
1645 (long long)stats[ST_F_CLI_ABRT].u.u64,
1646 (long long)stats[ST_F_SRV_ABRT].u.u64,
1647 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1648 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1649 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 +02001650 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001651 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1652
1653 chunk_appendf(out,
1654 /* rest of backend: nothing, down transitions, total downtime, throttle */
1655 "<td class=ac>&nbsp;</td><td>%d</td>"
1656 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001657 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001658 stats[ST_F_CHKDOWN].u.u32,
1659 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001660
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001661 if (flags & STAT_SHMODULES) {
1662 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001663 chunk_appendf(out, "<td>");
1664
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001665 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001666 chunk_appendf(out,
1667 "<u>%s<div class=tips><table class=det>",
1668 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001669 for (j = 0; j < mod->stats_count; ++j) {
1670 chunk_appendf(out,
1671 "<tr><th>%s</th><td>%s</td></tr>",
1672 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1673 ++i;
1674 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001675 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001676 } else {
1677 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001678 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001679
1680 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001681 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001682 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001683
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001684 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001685 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001686
William Lallemand74c24fb2016-11-21 17:18:36 +01001687 return 1;
1688}
1689
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001690int stats_dump_one_line(const struct field *stats, size_t stats_count,
1691 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001692{
Willy Tarreau91cefca2022-05-03 17:08:29 +02001693 struct show_stat_ctx *ctx = appctx->svcctx;
Simon Horman05ee2132017-01-04 09:37:25 +01001694 int ret;
1695
Willy Tarreau91cefca2022-05-03 17:08:29 +02001696 if (ctx->flags & STAT_FMT_HTML)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001697 ret = stats_dump_fields_html(&trash_chunk, stats, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001698 else if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001699 ret = stats_dump_fields_typed(&trash_chunk, stats, stats_count, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001700 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001701 ret = stats_dump_fields_json(&trash_chunk, stats, stats_count, ctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001702 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01001703 ret = stats_dump_fields_csv(&trash_chunk, stats, stats_count, ctx);
Simon Horman05ee2132017-01-04 09:37:25 +01001704
Simon Horman05ee2132017-01-04 09:37:25 +01001705 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001706}
1707
William Dauchy0ef54392021-01-17 18:27:45 +01001708/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001709 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1710 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1711 * this value, or if the selected field is not implemented for frontends, the
1712 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001713 */
William Dauchy0ef54392021-01-17 18:27:45 +01001714int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1715 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001716{
William Dauchy0ef54392021-01-17 18:27:45 +01001717 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001718
William Lallemand74c24fb2016-11-21 17:18:36 +01001719 if (len < ST_F_TOTAL_FIELDS)
1720 return 0;
1721
William Dauchy0ef54392021-01-17 18:27:45 +01001722 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001723 struct field metric = { 0 };
1724
William Dauchy0ef54392021-01-17 18:27:45 +01001725 switch (current_field) {
1726 case ST_F_PXNAME:
1727 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1728 break;
1729 case ST_F_SVNAME:
1730 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1731 break;
1732 case ST_F_MODE:
1733 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1734 break;
1735 case ST_F_SCUR:
1736 metric = mkf_u32(0, px->feconn);
1737 break;
1738 case ST_F_SMAX:
1739 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1740 break;
1741 case ST_F_SLIM:
1742 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1743 break;
1744 case ST_F_STOT:
1745 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1746 break;
1747 case ST_F_BIN:
1748 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1749 break;
1750 case ST_F_BOUT:
1751 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1752 break;
1753 case ST_F_DREQ:
1754 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1755 break;
1756 case ST_F_DRESP:
1757 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1758 break;
1759 case ST_F_EREQ:
1760 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1761 break;
1762 case ST_F_DCON:
1763 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1764 break;
1765 case ST_F_DSES:
1766 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1767 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001768 case ST_F_STATUS: {
1769 const char *state;
1770
1771 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1772 state = "STOP";
1773 else if (px->flags & PR_FL_PAUSED)
1774 state = "PAUSED";
1775 else
1776 state = "OPEN";
1777 metric = mkf_str(FO_STATUS, state);
William Dauchy0ef54392021-01-17 18:27:45 +01001778 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001779 }
William Dauchy0ef54392021-01-17 18:27:45 +01001780 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001781 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001782 break;
1783 case ST_F_IID:
1784 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1785 break;
1786 case ST_F_SID:
1787 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1788 break;
1789 case ST_F_TYPE:
1790 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1791 break;
1792 case ST_F_RATE:
1793 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1794 break;
1795 case ST_F_RATE_LIM:
1796 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1797 break;
1798 case ST_F_RATE_MAX:
1799 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1800 break;
1801 case ST_F_WREW:
1802 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1803 break;
1804 case ST_F_EINT:
1805 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1806 break;
1807 case ST_F_HRSP_1XX:
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[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001810 break;
1811 case ST_F_HRSP_2XX:
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[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001814 break;
1815 case ST_F_HRSP_3XX:
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[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001818 break;
1819 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001820 if (px->mode == PR_MODE_HTTP)
1821 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001822 break;
1823 case ST_F_HRSP_5XX:
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.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001826 break;
1827 case ST_F_HRSP_OTHER:
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.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001830 break;
1831 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001832 if (px->mode == PR_MODE_HTTP)
1833 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001834 break;
1835 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001836 if (px->mode == PR_MODE_HTTP)
1837 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001838 break;
1839 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001840 if (px->mode == PR_MODE_HTTP)
1841 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001842 break;
1843 case ST_F_REQ_RATE:
1844 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1845 break;
1846 case ST_F_REQ_RATE_MAX:
1847 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1848 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001849 case ST_F_REQ_TOT: {
1850 int i;
1851 uint64_t total_req;
1852 size_t nb_reqs =
1853 sizeof(px->fe_counters.p.http.cum_req) / sizeof(*px->fe_counters.p.http.cum_req);
1854
1855 total_req = 0;
1856 for (i = 0; i < nb_reqs; i++)
1857 total_req += px->fe_counters.p.http.cum_req[i];
1858 metric = mkf_u64(FN_COUNTER, total_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001859 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001860 }
William Dauchy0ef54392021-01-17 18:27:45 +01001861 case ST_F_COMP_IN:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001862 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001863 break;
1864 case ST_F_COMP_OUT:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001865 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001866 break;
1867 case ST_F_COMP_BYP:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001868 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001869 break;
1870 case ST_F_COMP_RSP:
1871 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1872 break;
1873 case ST_F_CONN_RATE:
1874 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1875 break;
1876 case ST_F_CONN_RATE_MAX:
1877 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1878 break;
1879 case ST_F_CONN_TOT:
1880 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1881 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001882 case ST_F_SESS_OTHER: {
1883 int i;
1884 uint64_t total_sess;
1885 size_t nb_sess =
1886 sizeof(px->fe_counters.cum_sess_ver) / sizeof(*px->fe_counters.cum_sess_ver);
1887
1888 total_sess = px->fe_counters.cum_sess;
1889 for (i = 0; i < nb_sess; i++)
1890 total_sess -= px->fe_counters.cum_sess_ver[i];
Frédéric Lécailled97d1d72023-02-06 09:23:56 +01001891 total_sess = (int64_t)total_sess < 0 ? 0 : total_sess;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001892 metric = mkf_u64(FN_COUNTER, total_sess);
1893 break;
1894 }
1895 case ST_F_H1SESS:
1896 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[0]);
1897 break;
1898 case ST_F_H2SESS:
1899 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[1]);
1900 break;
1901 case ST_F_H3SESS:
1902 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[2]);
1903 break;
1904 case ST_F_REQ_OTHER:
1905 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[0]);
1906 break;
1907 case ST_F_H1REQ:
1908 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[1]);
1909 break;
1910 case ST_F_H2REQ:
1911 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[2]);
1912 break;
1913 case ST_F_H3REQ:
1914 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[3]);
1915 break;
William Dauchy0ef54392021-01-17 18:27:45 +01001916 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001917 /* not used for frontends. If a specific metric
1918 * is requested, return an error. Otherwise continue.
1919 */
1920 if (selected_field != NULL)
1921 return 0;
1922 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001923 }
1924 stats[current_field] = metric;
1925 if (selected_field != NULL)
1926 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001927 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001928 return 1;
1929}
1930
Christopher Fauleta8b76842022-12-16 15:08:36 +01001931/* Dumps a frontend's line to the local trash buffer for the current proxy <px>
1932 * and uses the state from stream connector <sc>. The caller is responsible for
1933 * clearing the local trash buffer if needed. Returns non-zero if it emits
1934 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001935 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001936static int stats_dump_fe_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001937{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001938 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001939 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001940 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1941 struct stats_module *mod;
1942 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001943
1944 if (!(px->cap & PR_CAP_FE))
1945 return 0;
1946
Willy Tarreau91cefca2022-05-03 17:08:29 +02001947 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_FE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001948 return 0;
1949
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001950 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1951
William Dauchy0ef54392021-01-17 18:27:45 +01001952 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001953 return 0;
1954
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001955 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1956 void *counters;
1957
1958 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1959 stats_count += mod->stats_count;
1960 continue;
1961 }
1962
1963 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1964 mod->fill_stats(counters, stats + stats_count);
1965 stats_count += mod->stats_count;
1966 }
1967
1968 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001969}
1970
William Dauchy655e14e2021-02-14 23:22:54 +01001971/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1972 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1973 * this length is less then this value, the function returns 0, otherwise, it
1974 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1975 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001976 */
1977int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001978 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001979{
William Dauchy655e14e2021-02-14 23:22:54 +01001980 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001981 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001982
1983 if (len < ST_F_TOTAL_FIELDS)
1984 return 0;
1985
1986 if (!l->counters)
1987 return 0;
1988
1989 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001990
William Dauchy655e14e2021-02-14 23:22:54 +01001991 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1992 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001993
William Dauchy655e14e2021-02-14 23:22:54 +01001994 switch (current_field) {
1995 case ST_F_PXNAME:
1996 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1997 break;
1998 case ST_F_SVNAME:
1999 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
2000 break;
2001 case ST_F_MODE:
2002 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2003 break;
2004 case ST_F_SCUR:
2005 metric = mkf_u32(0, l->nbconn);
2006 break;
2007 case ST_F_SMAX:
2008 metric = mkf_u32(FN_MAX, l->counters->conn_max);
2009 break;
2010 case ST_F_SLIM:
Willy Tarreau758c69d2023-01-12 18:59:37 +01002011 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->bind_conf->maxconn);
William Dauchy655e14e2021-02-14 23:22:54 +01002012 break;
2013 case ST_F_STOT:
2014 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
2015 break;
2016 case ST_F_BIN:
2017 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
2018 break;
2019 case ST_F_BOUT:
2020 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
2021 break;
2022 case ST_F_DREQ:
2023 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
2024 break;
2025 case ST_F_DRESP:
2026 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
2027 break;
2028 case ST_F_EREQ:
2029 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
2030 break;
2031 case ST_F_DCON:
2032 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
2033 break;
2034 case ST_F_DSES:
2035 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
2036 break;
2037 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01002038 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01002039 break;
2040 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002041 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01002042 break;
2043 case ST_F_IID:
2044 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2045 break;
2046 case ST_F_SID:
2047 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
2048 break;
2049 case ST_F_TYPE:
2050 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
2051 break;
2052 case ST_F_WREW:
2053 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
2054 break;
2055 case ST_F_EINT:
2056 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
2057 break;
2058 case ST_F_ADDR:
2059 if (flags & STAT_SHLGNDS) {
2060 char str[INET6_ADDRSTRLEN];
2061 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01002062
William Dauchy655e14e2021-02-14 23:22:54 +01002063 port = get_host_port(&l->rx.addr);
2064 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
2065 case AF_INET:
2066 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2067 chunk_appendf(out, "%s:%d", str, port);
2068 break;
2069 case AF_INET6:
2070 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2071 chunk_appendf(out, "[%s]:%d", str, port);
2072 break;
2073 case AF_UNIX:
2074 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2075 break;
2076 case -1:
2077 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2078 chunk_strcat(out, strerror(errno));
2079 break;
2080 default: /* address family not supported */
2081 break;
2082 }
2083 }
2084 break;
Willy Tarreau4cfb0012023-05-11 14:49:43 +02002085 case ST_F_PROTO:
2086 metric = mkf_str(FO_STATUS, l->rx.proto->name);
2087 break;
William Dauchy655e14e2021-02-14 23:22:54 +01002088 default:
2089 /* not used for listen. If a specific metric
2090 * is requested, return an error. Otherwise continue.
2091 */
2092 if (selected_field != NULL)
2093 return 0;
2094 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002095 }
William Dauchy655e14e2021-02-14 23:22:54 +01002096 stats[current_field] = metric;
2097 if (selected_field != NULL)
2098 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002099 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002100 return 1;
2101}
2102
Christopher Fauleta8b76842022-12-16 15:08:36 +01002103/* Dumps a line for listener <l> and proxy <px> to the local trash buffer and
2104 * uses the state from stream connector <sc>. The caller is responsible for
2105 * clearing the local trash buffer if needed. Returns non-zero if it emits
2106 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002107 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002108static int stats_dump_li_stats(struct stconn *sc, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01002109{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002110 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002111 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002112 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2113 struct stats_module *mod;
2114 size_t stats_count = ST_F_TOTAL_FIELDS;
2115
2116 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002117
Willy Tarreau91cefca2022-05-03 17:08:29 +02002118 if (!stats_fill_li_stats(px, l, ctx->flags, stats,
William Dauchy655e14e2021-02-14 23:22:54 +01002119 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002120 return 0;
2121
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002122 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2123 void *counters;
2124
2125 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
2126 stats_count += mod->stats_count;
2127 continue;
2128 }
2129
2130 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2131 mod->fill_stats(counters, stats + stats_count);
2132 stats_count += mod->stats_count;
2133 }
2134
2135 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002136}
2137
2138enum srv_stats_state {
2139 SRV_STATS_STATE_DOWN = 0,
2140 SRV_STATS_STATE_DOWN_AGENT,
2141 SRV_STATS_STATE_GOING_UP,
2142 SRV_STATS_STATE_UP_GOING_DOWN,
2143 SRV_STATS_STATE_UP,
2144 SRV_STATS_STATE_NOLB_GOING_DOWN,
2145 SRV_STATS_STATE_NOLB,
2146 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2147 SRV_STATS_STATE_DRAIN,
2148 SRV_STATS_STATE_DRAIN_AGENT,
2149 SRV_STATS_STATE_NO_CHECK,
2150
2151 SRV_STATS_STATE_COUNT, /* Must be last */
2152};
2153
2154static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2155 [SRV_STATS_STATE_DOWN] = "DOWN",
2156 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2157 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2158 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2159 [SRV_STATS_STATE_UP] = "UP",
2160 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2161 [SRV_STATS_STATE_NOLB] = "NOLB",
2162 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2163 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2164 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2165 [SRV_STATS_STATE_NO_CHECK] = "no check"
2166};
2167
William Dauchyd3a9a492021-01-25 17:29:03 +01002168/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002169 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002170static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2171 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002172{
Emeric Brun52a91d32017-08-31 14:41:55 +02002173 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002174 if ((ref->check.state & CHK_ST_ENABLED) &&
2175 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002176 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002177 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002178 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002179 }
2180
Emeric Brun52a91d32017-08-31 14:41:55 +02002181 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002182 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002183 *state = SRV_STATS_STATE_DRAIN_AGENT;
2184 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2185 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002186 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002187 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002188 }
2189
William Dauchyd3a9a492021-01-25 17:29:03 +01002190 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2191 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002192 }
2193 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002194 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002195 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2196 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002197 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002198 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002199 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002200 }
2201 }
2202 else { /* stopped */
2203 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002204 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002205 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002206 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002207 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002208 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002209 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002210 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002211 }
2212 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002213}
William Lallemand74c24fb2016-11-21 17:18:36 +01002214
William Dauchyd3a9a492021-01-25 17:29:03 +01002215/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2216 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2217 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2218 * this value, or if the selected field is not implemented for servers, the
2219 * function returns 0, otherwise, it returns 1. <flags> can take the value
2220 * STAT_SHLGNDS.
2221 */
2222int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2223 struct field *stats, int len,
2224 enum stat_field *selected_field)
2225{
2226 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2227 struct server *via = sv->track ? sv->track : sv;
2228 struct server *ref = via;
2229 enum srv_stats_state state = 0;
2230 char str[INET6_ADDRSTRLEN];
2231 struct buffer *out = get_trash_chunk();
2232 char *fld_status;
2233 long long srv_samples_counter;
2234 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002235
William Dauchyd3a9a492021-01-25 17:29:03 +01002236 if (len < ST_F_TOTAL_FIELDS)
2237 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002238
William Dauchyd3a9a492021-01-25 17:29:03 +01002239 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002240
William Dauchyd3a9a492021-01-25 17:29:03 +01002241 /* compute state for later use */
2242 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2243 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2244 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2245 /* we have "via" which is the tracked server as described in the configuration,
2246 * and "ref" which is the checked server and the end of the chain.
2247 */
2248 while (ref->track)
2249 ref = ref->track;
2250 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002251 }
2252
William Dauchyd3a9a492021-01-25 17:29:03 +01002253 /* compue time values for later use */
2254 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2255 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2256 *selected_field == ST_F_TTIME) {
2257 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2258 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2259 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002260 }
2261
William Dauchyd3a9a492021-01-25 17:29:03 +01002262 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2263 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002264
William Dauchyd3a9a492021-01-25 17:29:03 +01002265 switch (current_field) {
2266 case ST_F_PXNAME:
2267 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2268 break;
2269 case ST_F_SVNAME:
2270 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2271 break;
2272 case ST_F_MODE:
2273 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002274 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002275 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002276 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002277 break;
2278 case ST_F_QMAX:
2279 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2280 break;
2281 case ST_F_SCUR:
2282 metric = mkf_u32(0, sv->cur_sess);
2283 break;
2284 case ST_F_SMAX:
2285 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2286 break;
2287 case ST_F_SLIM:
2288 if (sv->maxconn)
2289 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2290 break;
2291 case ST_F_SRV_ICUR:
2292 metric = mkf_u32(0, sv->curr_idle_conns);
2293 break;
2294 case ST_F_SRV_ILIM:
2295 if (sv->max_idle_conns != -1)
2296 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2297 break;
2298 case ST_F_STOT:
2299 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2300 break;
2301 case ST_F_BIN:
2302 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2303 break;
2304 case ST_F_BOUT:
2305 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2306 break;
2307 case ST_F_DRESP:
2308 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2309 break;
2310 case ST_F_ECON:
2311 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2312 break;
2313 case ST_F_ERESP:
2314 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2315 break;
2316 case ST_F_WRETR:
2317 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2318 break;
2319 case ST_F_WREDIS:
2320 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2321 break;
2322 case ST_F_WREW:
2323 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2324 break;
2325 case ST_F_EINT:
2326 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2327 break;
2328 case ST_F_CONNECT:
2329 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2330 break;
2331 case ST_F_REUSE:
2332 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2333 break;
2334 case ST_F_IDLE_CONN_CUR:
2335 metric = mkf_u32(0, sv->curr_idle_nb);
2336 break;
2337 case ST_F_SAFE_CONN_CUR:
2338 metric = mkf_u32(0, sv->curr_safe_nb);
2339 break;
2340 case ST_F_USED_CONN_CUR:
2341 metric = mkf_u32(0, sv->curr_used_conns);
2342 break;
2343 case ST_F_NEED_CONN_EST:
2344 metric = mkf_u32(0, sv->est_need_conns);
2345 break;
2346 case ST_F_STATUS:
2347 fld_status = chunk_newstr(out);
2348 if (sv->cur_admin & SRV_ADMF_RMAINT)
2349 chunk_appendf(out, "MAINT (resolution)");
2350 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2351 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2352 else if (sv->cur_admin & SRV_ADMF_MAINT)
2353 chunk_appendf(out, "MAINT");
2354 else
2355 chunk_appendf(out,
2356 srv_hlt_st[state],
2357 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2358 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002359
William Dauchyd3a9a492021-01-25 17:29:03 +01002360 metric = mkf_str(FO_STATUS, fld_status);
2361 break;
2362 case ST_F_LASTCHG:
Willy Tarreau69530f52023-04-28 09:16:15 +02002363 metric = mkf_u32(FN_AGE, ns_to_sec(now_ns) - sv->last_change);
William Dauchyd3a9a492021-01-25 17:29:03 +01002364 break;
2365 case ST_F_WEIGHT:
2366 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2367 break;
2368 case ST_F_UWEIGHT:
2369 metric = mkf_u32(FN_AVG, sv->uweight);
2370 break;
2371 case ST_F_ACT:
2372 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2373 break;
2374 case ST_F_BCK:
2375 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2376 break;
2377 case ST_F_CHKFAIL:
2378 if (sv->check.state & CHK_ST_ENABLED)
2379 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2380 break;
2381 case ST_F_CHKDOWN:
2382 if (sv->check.state & CHK_ST_ENABLED)
2383 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2384 break;
2385 case ST_F_DOWNTIME:
2386 if (sv->check.state & CHK_ST_ENABLED)
2387 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2388 break;
2389 case ST_F_QLIMIT:
2390 if (sv->maxqueue)
2391 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2392 break;
2393 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002394 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002395 break;
2396 case ST_F_IID:
2397 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2398 break;
2399 case ST_F_SID:
2400 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2401 break;
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01002402 case ST_F_SRID:
2403 metric = mkf_u32(FN_COUNTER, sv->rid);
2404 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002405 case ST_F_THROTTLE:
2406 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2407 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2408 break;
2409 case ST_F_LBTOT:
2410 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2411 break;
2412 case ST_F_TRACKED:
2413 if (sv->track) {
2414 char *fld_track = chunk_newstr(out);
2415 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2416 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2417 }
2418 break;
2419 case ST_F_TYPE:
2420 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2421 break;
2422 case ST_F_RATE:
2423 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2424 break;
2425 case ST_F_RATE_MAX:
2426 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2427 break;
2428 case ST_F_CHECK_STATUS:
2429 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2430 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002431
William Dauchyd3a9a492021-01-25 17:29:03 +01002432 fld_chksts = chunk_newstr(out);
2433 chunk_strcat(out, "* "); // for check in progress
2434 chunk_strcat(out, get_check_status_info(sv->check.status));
2435 if (!(sv->check.state & CHK_ST_INPROGRESS))
2436 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002437 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002438 }
2439 break;
2440 case ST_F_CHECK_CODE:
2441 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2442 sv->check.status >= HCHK_STATUS_L57DATA)
2443 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2444 break;
2445 case ST_F_CHECK_DURATION:
2446 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2447 sv->check.status >= HCHK_STATUS_CHECKED)
Aurelien DARRAGONb118f2f2022-12-07 14:52:10 +01002448 metric = mkf_u64(FN_DURATION, MAX(sv->check.duration, 0));
William Dauchyd3a9a492021-01-25 17:29:03 +01002449 break;
2450 case ST_F_CHECK_DESC:
2451 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2452 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2453 break;
2454 case ST_F_LAST_CHK:
2455 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2456 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2457 break;
2458 case ST_F_CHECK_RISE:
2459 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2460 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2461 break;
2462 case ST_F_CHECK_FALL:
2463 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2464 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2465 break;
2466 case ST_F_CHECK_HEALTH:
2467 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2468 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2469 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002470 case ST_F_AGENT_STATUS:
2471 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2472 const char *fld_chksts;
2473
2474 fld_chksts = chunk_newstr(out);
2475 chunk_strcat(out, "* "); // for check in progress
2476 chunk_strcat(out, get_check_status_info(sv->agent.status));
2477 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2478 fld_chksts += 2; // skip "* "
2479 metric = mkf_str(FN_OUTPUT, fld_chksts);
2480 }
2481 break;
2482 case ST_F_AGENT_CODE:
2483 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2484 (sv->agent.status >= HCHK_STATUS_L57DATA))
2485 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2486 break;
2487 case ST_F_AGENT_DURATION:
2488 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2489 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2490 break;
2491 case ST_F_AGENT_DESC:
2492 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2493 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2494 break;
2495 case ST_F_LAST_AGT:
2496 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2497 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2498 break;
2499 case ST_F_AGENT_RISE:
2500 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2501 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2502 break;
2503 case ST_F_AGENT_FALL:
2504 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2505 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2506 break;
2507 case ST_F_AGENT_HEALTH:
2508 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2509 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2510 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002511 case ST_F_REQ_TOT:
2512 if (px->mode == PR_MODE_HTTP)
2513 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2514 break;
2515 case ST_F_HRSP_1XX:
2516 if (px->mode == PR_MODE_HTTP)
2517 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2518 break;
2519 case ST_F_HRSP_2XX:
2520 if (px->mode == PR_MODE_HTTP)
2521 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2522 break;
2523 case ST_F_HRSP_3XX:
2524 if (px->mode == PR_MODE_HTTP)
2525 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2526 break;
2527 case ST_F_HRSP_4XX:
2528 if (px->mode == PR_MODE_HTTP)
2529 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2530 break;
2531 case ST_F_HRSP_5XX:
2532 if (px->mode == PR_MODE_HTTP)
2533 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2534 break;
2535 case ST_F_HRSP_OTHER:
2536 if (px->mode == PR_MODE_HTTP)
2537 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2538 break;
2539 case ST_F_HANAFAIL:
2540 if (ref->observe)
2541 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2542 break;
2543 case ST_F_CLI_ABRT:
2544 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2545 break;
2546 case ST_F_SRV_ABRT:
2547 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2548 break;
2549 case ST_F_LASTSESS:
2550 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2551 break;
2552 case ST_F_QTIME:
2553 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2554 break;
2555 case ST_F_CTIME:
2556 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2557 break;
2558 case ST_F_RTIME:
2559 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2560 break;
2561 case ST_F_TTIME:
2562 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2563 break;
2564 case ST_F_QT_MAX:
2565 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2566 break;
2567 case ST_F_CT_MAX:
2568 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2569 break;
2570 case ST_F_RT_MAX:
2571 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2572 break;
2573 case ST_F_TT_MAX:
2574 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2575 break;
2576 case ST_F_ADDR:
2577 if (flags & STAT_SHLGNDS) {
2578 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2579 case AF_INET:
2580 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2581 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2582 break;
2583 case AF_INET6:
2584 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2585 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2586 break;
2587 case AF_UNIX:
2588 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2589 break;
2590 case -1:
2591 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2592 chunk_strcat(out, strerror(errno));
2593 break;
2594 default: /* address family not supported */
2595 break;
2596 }
2597 }
2598 break;
2599 case ST_F_COOKIE:
2600 if (flags & STAT_SHLGNDS && sv->cookie)
2601 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2602 break;
2603 default:
2604 /* not used for servers. If a specific metric
2605 * is requested, return an error. Otherwise continue.
2606 */
2607 if (selected_field != NULL)
2608 return 0;
2609 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002610 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002611 stats[current_field] = metric;
2612 if (selected_field != NULL)
2613 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002614 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002615 return 1;
2616}
2617
Christopher Fauleta8b76842022-12-16 15:08:36 +01002618/* Dumps a line for server <sv> and proxy <px> to the local trash vbuffer and
2619 * uses the state from stream connector <sc>, and server state <state>. The
2620 * caller is responsible for clearing the local trash buffer if needed. Returns
2621 * non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002622 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002623static int stats_dump_sv_stats(struct stconn *sc, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002624{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002625 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002626 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002627 struct stats_module *mod;
2628 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2629 size_t stats_count = ST_F_TOTAL_FIELDS;
2630
2631 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002632
Willy Tarreau91cefca2022-05-03 17:08:29 +02002633 if (!stats_fill_sv_stats(px, sv, ctx->flags, stats,
William Dauchyd3a9a492021-01-25 17:29:03 +01002634 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002635 return 0;
2636
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002637 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2638 void *counters;
2639
2640 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2641 continue;
2642
2643 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2644 stats_count += mod->stats_count;
2645 continue;
2646 }
2647
2648 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2649 mod->fill_stats(counters, stats + stats_count);
2650 stats_count += mod->stats_count;
2651 }
2652
2653 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002654}
2655
William Dauchyda3b4662021-01-25 17:29:01 +01002656/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002657 */
William Dauchyda3b4662021-01-25 17:29:01 +01002658static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002659{
William Dauchyda3b4662021-01-25 17:29:01 +01002660 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002661 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002662
William Dauchyda3b4662021-01-25 17:29:01 +01002663 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002664 for (srv = px->srv; srv; srv = srv->next) {
2665 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002666 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002667 if (srv_currently_usable(srv) &&
2668 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002669 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002670 }
William Dauchyda3b4662021-01-25 17:29:01 +01002671 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002672 }
2673
Willy Tarreaubd715102020-10-23 22:44:30 +02002674 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2675 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002676 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002677 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2678
William Dauchyda3b4662021-01-25 17:29:01 +01002679 /* use tmp variable then assign result to make gcc happy */
2680 *nbup = nbup_tmp;
2681 *nbsrv = nbsrv_tmp;
2682 *totuw = totuw_tmp;
2683}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002684
William Dauchyda3b4662021-01-25 17:29:01 +01002685/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2686 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2687 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2688 * this value, or if the selected field is not implemented for backends, the
2689 * function returns 0, otherwise, it returns 1. <flags> can take the value
2690 * STAT_SHLGNDS.
2691 */
2692int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2693 enum stat_field *selected_field)
2694{
2695 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2696 long long be_samples_counter;
2697 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2698 struct buffer *out = get_trash_chunk();
2699 int nbup, nbsrv, totuw;
2700 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002701
William Dauchyda3b4662021-01-25 17:29:01 +01002702 if (len < ST_F_TOTAL_FIELDS)
2703 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002704
William Dauchyda3b4662021-01-25 17:29:01 +01002705 nbup = nbsrv = totuw = 0;
2706 /* some srv values compute for later if we either select all fields or
2707 * need them for one of the mentioned ones */
2708 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2709 *selected_field == ST_F_UWEIGHT)
2710 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002711
William Dauchyda3b4662021-01-25 17:29:01 +01002712 /* same here but specific to time fields */
2713 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2714 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2715 *selected_field == ST_F_TTIME) {
2716 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2717 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2718 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002719 }
2720
William Dauchyda3b4662021-01-25 17:29:01 +01002721 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2722 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002723
William Dauchyda3b4662021-01-25 17:29:01 +01002724 switch (current_field) {
2725 case ST_F_PXNAME:
2726 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2727 break;
2728 case ST_F_SVNAME:
2729 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2730 break;
2731 case ST_F_MODE:
2732 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2733 break;
2734 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002735 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002736 break;
2737 case ST_F_QMAX:
2738 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2739 break;
2740 case ST_F_SCUR:
2741 metric = mkf_u32(0, px->beconn);
2742 break;
2743 case ST_F_SMAX:
2744 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2745 break;
2746 case ST_F_SLIM:
2747 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2748 break;
2749 case ST_F_STOT:
Amaury Denoyelle1c8372b2024-04-04 18:08:46 +02002750 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_sess);
William Dauchyda3b4662021-01-25 17:29:01 +01002751 break;
2752 case ST_F_BIN:
2753 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2754 break;
2755 case ST_F_BOUT:
2756 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2757 break;
2758 case ST_F_DREQ:
2759 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2760 break;
2761 case ST_F_DRESP:
2762 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2763 break;
2764 case ST_F_ECON:
2765 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2766 break;
2767 case ST_F_ERESP:
2768 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2769 break;
2770 case ST_F_WRETR:
2771 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2772 break;
2773 case ST_F_WREDIS:
2774 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2775 break;
2776 case ST_F_WREW:
2777 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2778 break;
2779 case ST_F_EINT:
2780 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2781 break;
2782 case ST_F_CONNECT:
2783 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2784 break;
2785 case ST_F_REUSE:
2786 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2787 break;
2788 case ST_F_STATUS:
2789 fld = chunk_newstr(out);
2790 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2791 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2792 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2793 metric = mkf_str(FO_STATUS, fld);
2794 break;
Cedric Paillet7d6644e2022-12-08 09:17:00 +00002795 case ST_F_AGG_SRV_CHECK_STATUS: // DEPRECATED
2796 case ST_F_AGG_SRV_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +01002797 metric = mkf_u32(FN_GAUGE, 0);
2798 break;
Cedric Paillete06e31e2022-12-08 09:17:01 +00002799 case ST_F_AGG_CHECK_STATUS:
2800 metric = mkf_u32(FN_GAUGE, 0);
2801 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002802 case ST_F_WEIGHT:
2803 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2804 break;
2805 case ST_F_UWEIGHT:
2806 metric = mkf_u32(FN_AVG, totuw);
2807 break;
2808 case ST_F_ACT:
2809 metric = mkf_u32(0, px->srv_act);
2810 break;
2811 case ST_F_BCK:
2812 metric = mkf_u32(0, px->srv_bck);
2813 break;
2814 case ST_F_CHKDOWN:
2815 metric = mkf_u64(FN_COUNTER, px->down_trans);
2816 break;
2817 case ST_F_LASTCHG:
Willy Tarreau69530f52023-04-28 09:16:15 +02002818 metric = mkf_u32(FN_AGE, ns_to_sec(now_ns) - px->last_change);
William Dauchyda3b4662021-01-25 17:29:01 +01002819 break;
2820 case ST_F_DOWNTIME:
2821 if (px->srv)
2822 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2823 break;
2824 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002825 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002826 break;
2827 case ST_F_IID:
2828 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2829 break;
2830 case ST_F_SID:
2831 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2832 break;
2833 case ST_F_LBTOT:
2834 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2835 break;
2836 case ST_F_TYPE:
2837 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2838 break;
2839 case ST_F_RATE:
2840 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2841 break;
2842 case ST_F_RATE_MAX:
2843 metric = mkf_u32(0, px->be_counters.sps_max);
2844 break;
2845 case ST_F_COOKIE:
2846 if (flags & STAT_SHLGNDS && px->cookie_name)
2847 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2848 break;
2849 case ST_F_ALGO:
2850 if (flags & STAT_SHLGNDS)
2851 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2852 break;
2853 case ST_F_REQ_TOT:
2854 if (px->mode == PR_MODE_HTTP)
2855 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2856 break;
2857 case ST_F_HRSP_1XX:
2858 if (px->mode == PR_MODE_HTTP)
2859 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2860 break;
2861 case ST_F_HRSP_2XX:
2862 if (px->mode == PR_MODE_HTTP)
2863 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2864 break;
2865 case ST_F_HRSP_3XX:
2866 if (px->mode == PR_MODE_HTTP)
2867 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2868 break;
2869 case ST_F_HRSP_4XX:
2870 if (px->mode == PR_MODE_HTTP)
2871 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2872 break;
2873 case ST_F_HRSP_5XX:
2874 if (px->mode == PR_MODE_HTTP)
2875 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2876 break;
2877 case ST_F_HRSP_OTHER:
2878 if (px->mode == PR_MODE_HTTP)
2879 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2880 break;
2881 case ST_F_CACHE_LOOKUPS:
2882 if (px->mode == PR_MODE_HTTP)
2883 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2884 break;
2885 case ST_F_CACHE_HITS:
2886 if (px->mode == PR_MODE_HTTP)
2887 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2888 break;
2889 case ST_F_CLI_ABRT:
2890 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2891 break;
2892 case ST_F_SRV_ABRT:
2893 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2894 break;
2895 case ST_F_COMP_IN:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002896 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002897 break;
2898 case ST_F_COMP_OUT:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002899 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002900 break;
2901 case ST_F_COMP_BYP:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002902 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002903 break;
2904 case ST_F_COMP_RSP:
2905 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2906 break;
2907 case ST_F_LASTSESS:
2908 metric = mkf_s32(FN_AGE, be_lastsession(px));
2909 break;
2910 case ST_F_QTIME:
2911 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2912 break;
2913 case ST_F_CTIME:
2914 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2915 break;
2916 case ST_F_RTIME:
2917 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2918 break;
2919 case ST_F_TTIME:
2920 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2921 break;
2922 case ST_F_QT_MAX:
2923 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2924 break;
2925 case ST_F_CT_MAX:
2926 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2927 break;
2928 case ST_F_RT_MAX:
2929 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2930 break;
2931 case ST_F_TT_MAX:
2932 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2933 break;
2934 default:
2935 /* not used for backends. If a specific metric
2936 * is requested, return an error. Otherwise continue.
2937 */
2938 if (selected_field != NULL)
2939 return 0;
2940 continue;
2941 }
2942 stats[current_field] = metric;
2943 if (selected_field != NULL)
2944 break;
2945 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002946 return 1;
2947}
2948
Christopher Fauleta8b76842022-12-16 15:08:36 +01002949/* Dumps a line for backend <px> to the local trash buffer for and uses the
2950 * state from stream interface <si>. The caller is responsible for clearing the
2951 * local trash buffer if needed. Returns non-zero if it emits anything, zero
2952 * otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002953 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002954static int stats_dump_be_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002955{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002956 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002957 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002958 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2959 struct stats_module *mod;
2960 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002961
2962 if (!(px->cap & PR_CAP_BE))
2963 return 0;
2964
Willy Tarreau91cefca2022-05-03 17:08:29 +02002965 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_BE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01002966 return 0;
2967
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002968 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2969
Willy Tarreau91cefca2022-05-03 17:08:29 +02002970 if (!stats_fill_be_stats(px, ctx->flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002971 return 0;
2972
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002973 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2974 struct extra_counters *counters;
2975
2976 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2977 continue;
2978
2979 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2980 stats_count += mod->stats_count;
2981 continue;
2982 }
2983
2984 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2985 mod->fill_stats(counters, stats + stats_count);
2986 stats_count += mod->stats_count;
2987 }
2988
2989 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002990}
2991
Christopher Fauleta8b76842022-12-16 15:08:36 +01002992/* Dumps the HTML table header for proxy <px> to the local trash buffer for and
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01002993 * uses the state from stream connector <sc>. The caller is responsible for
2994 * clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01002995 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002996static void stats_dump_html_px_hdr(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002997{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002998 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002999 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01003000 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003001 struct stats_module *mod;
3002 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003003
Willy Tarreau91cefca2022-05-03 17:08:29 +02003004 if (px->cap & PR_CAP_BE && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003005 /* A form to enable/disable this proxy servers */
3006
Willy Tarreau91cefca2022-05-03 17:08:29 +02003007 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003008 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003009 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003010 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003011
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003012 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02003013 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3014 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003015 }
3016
Christopher Fauleta8b76842022-12-16 15:08:36 +01003017 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003018 "<form method=\"post\">");
3019 }
3020
3021 /* print a new table */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003022 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003023 "<table class=\"tbl\" width=\"100%%\">\n"
3024 "<tr class=\"titre\">"
3025 "<th class=\"pxname\" width=\"10%%\">");
3026
Christopher Fauleta8b76842022-12-16 15:08:36 +01003027 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003028 "<a name=\"%s\"></a>%s"
3029 "<a class=px href=\"#%s\">%s</a>",
3030 px->id,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003031 (ctx->flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01003032 px->id, px->id);
3033
Willy Tarreau91cefca2022-05-03 17:08:29 +02003034 if (ctx->flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003035 /* cap, mode, id */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003036 chunk_appendf(&trash_chunk, "<div class=tips>cap: %s, mode: %s, id: %d",
William Lallemand74c24fb2016-11-21 17:18:36 +01003037 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3038 px->uuid);
Christopher Fauleta8b76842022-12-16 15:08:36 +01003039 chunk_appendf(&trash_chunk, "</div>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003040 }
3041
Christopher Fauleta8b76842022-12-16 15:08:36 +01003042 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003043 "%s</th>"
3044 "<th class=\"%s\" width=\"90%%\">%s</th>"
3045 "</tr>\n"
3046 "</table>\n"
3047 "<table class=\"tbl\" width=\"100%%\">\n"
3048 "<tr class=\"titre\">",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003049 (ctx->flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01003050 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3051
Willy Tarreau91cefca2022-05-03 17:08:29 +02003052 if (ctx->flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003053 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01003054 if ((px->cap & PR_CAP_BE) && px->srv)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003055 chunk_appendf(&trash_chunk,
Christopher Fauletbc271ec2019-12-02 11:29:04 +01003056 "<th rowspan=2 width=1><input type=\"checkbox\" "
3057 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
3058 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
3059 px->id,
3060 px->id);
3061 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003062 chunk_appendf(&trash_chunk, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003063 }
3064
Christopher Fauleta8b76842022-12-16 15:08:36 +01003065 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003066 "<th rowspan=2></th>"
3067 "<th colspan=3>Queue</th>"
3068 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
3069 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3070 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003071 "<th colspan=9>Server</th>");
3072
Willy Tarreau91cefca2022-05-03 17:08:29 +02003073 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003074 // calculate the count of module for colspan attribute
3075 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
3076 ++stats_module_len;
3077 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003078 chunk_appendf(&trash_chunk, "<th colspan=%d>Extra modules</th>",
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003079 stats_module_len);
3080 }
3081
Christopher Fauleta8b76842022-12-16 15:08:36 +01003082 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003083 "</tr>\n"
3084 "<tr class=\"titre\">"
3085 "<th>Cur</th><th>Max</th><th>Limit</th>"
3086 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
3087 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
3088 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3089 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3090 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3091 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003092 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003093
Willy Tarreau91cefca2022-05-03 17:08:29 +02003094 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003095 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Christopher Fauleta8b76842022-12-16 15:08:36 +01003096 chunk_appendf(&trash_chunk, "<th>%s</th>", mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003097 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003098 }
3099
Christopher Fauleta8b76842022-12-16 15:08:36 +01003100 chunk_appendf(&trash_chunk, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003101}
3102
Christopher Fauleta8b76842022-12-16 15:08:36 +01003103/* Dumps the HTML table trailer for proxy <px> to the local trash buffer for and
3104 * uses the state from stream connector <sc>. The caller is responsible for
3105 * clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003106 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003107static void stats_dump_html_px_end(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01003108{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003109 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003110 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet693b23b2022-02-28 09:09:05 +01003111
Christopher Fauleta8b76842022-12-16 15:08:36 +01003112 chunk_appendf(&trash_chunk, "</table>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003113
Willy Tarreau91cefca2022-05-03 17:08:29 +02003114 if ((px->cap & PR_CAP_BE) && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003115 /* close the form used to enable/disable this proxy servers */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003116 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003117 "Choose the action to perform on the checked servers : "
3118 "<select name=action>"
3119 "<option value=\"\"></option>"
3120 "<option value=\"ready\">Set state to READY</option>"
3121 "<option value=\"drain\">Set state to DRAIN</option>"
3122 "<option value=\"maint\">Set state to MAINT</option>"
3123 "<option value=\"dhlth\">Health: disable checks</option>"
3124 "<option value=\"ehlth\">Health: enable checks</option>"
3125 "<option value=\"hrunn\">Health: force UP</option>"
3126 "<option value=\"hnolb\">Health: force NOLB</option>"
3127 "<option value=\"hdown\">Health: force DOWN</option>"
3128 "<option value=\"dagent\">Agent: disable checks</option>"
3129 "<option value=\"eagent\">Agent: enable checks</option>"
3130 "<option value=\"arunn\">Agent: force UP</option>"
3131 "<option value=\"adown\">Agent: force DOWN</option>"
3132 "<option value=\"shutdown\">Kill Sessions</option>"
3133 "</select>"
3134 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3135 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3136 "</form>",
3137 px->uuid);
3138 }
3139
Christopher Fauleta8b76842022-12-16 15:08:36 +01003140 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003141}
3142
3143/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003144 * Dumps statistics for a proxy. The output is sent to the stream connector's
William Lallemand74c24fb2016-11-21 17:18:36 +01003145 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3146 * buffer space, or non-zero if everything completed. This function is used
3147 * both by the CLI and the HTTP entry points, and is able to dump the output
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003148 * in HTML or CSV formats.
William Lallemand74c24fb2016-11-21 17:18:36 +01003149 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003150int stats_dump_proxy_to_buffer(struct stconn *sc, struct htx *htx,
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003151 struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01003152{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003153 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003154 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003155 struct channel *rep = sc_ic(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +01003156 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3157 struct listener *l;
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003158 struct uri_auth *uri = NULL;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003159 int current_field;
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003160 int px_st = ctx->px_st;
William Lallemand74c24fb2016-11-21 17:18:36 +01003161
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003162 if (ctx->http_px)
3163 uri = ctx->http_px->uri_auth;
Christopher Fauleta8b76842022-12-16 15:08:36 +01003164 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003165more:
3166 current_field = ctx->field;
William Lallemand74c24fb2016-11-21 17:18:36 +01003167
Willy Tarreau91cefca2022-05-03 17:08:29 +02003168 switch (ctx->px_st) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003169 case STAT_PX_ST_INIT:
3170 /* we are on a new proxy */
3171 if (uri && uri->scope) {
3172 /* we have a limited scope, we have to check the proxy name */
3173 struct stat_scope *scope;
3174 int len;
3175
3176 len = strlen(px->id);
3177 scope = uri->scope;
3178
3179 while (scope) {
3180 /* match exact proxy name */
3181 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3182 break;
3183
3184 /* match '.' which means 'self' proxy */
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003185 if (strcmp(scope->px_id, ".") == 0 && px == ctx->http_px)
William Lallemand74c24fb2016-11-21 17:18:36 +01003186 break;
3187 scope = scope->next;
3188 }
3189
3190 /* proxy name not found : don't dump anything */
3191 if (scope == NULL)
3192 return 1;
3193 }
3194
3195 /* if the user has requested a limited output and the proxy
3196 * name does not match, skip it.
3197 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003198 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003199 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003200
Willy Tarreau91cefca2022-05-03 17:08:29 +02003201 if (strnistr(px->id, strlen(px->id), scope_ptr, ctx->scope_len) == NULL)
Christopher Fauleted7a0662019-01-14 11:07:34 +01003202 return 1;
3203 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003204
Willy Tarreau91cefca2022-05-03 17:08:29 +02003205 if ((ctx->flags & STAT_BOUND) &&
3206 (ctx->iid != -1) &&
3207 (px->uuid != ctx->iid))
William Lallemand74c24fb2016-11-21 17:18:36 +01003208 return 1;
3209
Willy Tarreau91cefca2022-05-03 17:08:29 +02003210 ctx->px_st = STAT_PX_ST_TH;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003211 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003212
3213 case STAT_PX_ST_TH:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003214 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003215 stats_dump_html_px_hdr(sc, px);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003216 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003217 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003218 }
3219
Willy Tarreau91cefca2022-05-03 17:08:29 +02003220 ctx->px_st = STAT_PX_ST_FE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003221 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003222
3223 case STAT_PX_ST_FE:
3224 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003225 if (stats_dump_fe_stats(sc, px)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003226 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003227 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003228 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003229 if (ctx->field)
3230 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003231 }
3232
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003233 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003234 ctx->obj2 = px->conf.listeners.n;
3235 ctx->px_st = STAT_PX_ST_LI;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003236 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003237
3238 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003239 /* obj2 points to listeners list as initialized above */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003240 for (; ctx->obj2 != &px->conf.listeners; ctx->obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003241 if (htx) {
Christopher Fauleta236c582023-05-09 16:31:01 +02003242 if (htx_almost_full(htx)) {
3243 sc_need_room(sc, htx->size / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003244 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003245 }
Christopher Fauletef779222018-10-31 08:47:01 +01003246 }
3247 else {
Christopher Fauleta236c582023-05-09 16:31:01 +02003248 if (buffer_almost_full(&rep->buf)) {
3249 sc_need_room(sc, b_size(&rep->buf) / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003250 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003251 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003252 }
3253
Willy Tarreau91cefca2022-05-03 17:08:29 +02003254 l = LIST_ELEM(ctx->obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003255 if (!l->counters)
3256 continue;
3257
Willy Tarreau91cefca2022-05-03 17:08:29 +02003258 if (ctx->flags & STAT_BOUND) {
3259 if (!(ctx->type & (1 << STATS_TYPE_SO)))
William Lallemand74c24fb2016-11-21 17:18:36 +01003260 break;
3261
Willy Tarreau91cefca2022-05-03 17:08:29 +02003262 if (ctx->sid != -1 && l->luid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003263 continue;
3264 }
3265
3266 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003267 if (stats_dump_li_stats(sc, px, l)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003268 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003269 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003270 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003271 if (ctx->field)
3272 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003273 }
Aurelien DARRAGON9b07d4f2023-02-03 11:43:05 +01003274 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003275 }
3276
Willy Tarreau91cefca2022-05-03 17:08:29 +02003277 ctx->obj2 = px->srv; /* may be NULL */
3278 ctx->px_st = STAT_PX_ST_SV;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003279 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003280
3281 case STAT_PX_ST_SV:
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003282 /* check for dump resumption */
3283 if (px_st == STAT_PX_ST_SV) {
3284 struct server *cur = ctx->obj2;
3285
3286 /* re-entrant dump */
3287 BUG_ON(!cur);
3288 if (cur->flags & SRV_F_DELETED) {
3289 /* the server could have been marked as deleted
3290 * between two dumping attempts, skip it.
3291 */
3292 cur = cur->next;
3293 }
3294 srv_drop(ctx->obj2); /* drop old srv taken on last dumping attempt */
3295 ctx->obj2 = cur; /* could be NULL */
3296 /* back to normal */
3297 }
3298
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003299 /* obj2 points to servers list as initialized above.
3300 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003301 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003302 * Temporarily increment its refcount to prevent its
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003303 * anticipated cleaning. Call srv_drop() to release it.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003304 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003305 for (; ctx->obj2 != NULL;
3306 ctx->obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003307
Willy Tarreau91cefca2022-05-03 17:08:29 +02003308 sv = ctx->obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003309 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003310
Christopher Fauletef779222018-10-31 08:47:01 +01003311 if (htx) {
Christopher Fauleta236c582023-05-09 16:31:01 +02003312 if (htx_almost_full(htx)) {
3313 sc_need_room(sc, htx->size / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003314 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003315 }
Christopher Fauletef779222018-10-31 08:47:01 +01003316 }
3317 else {
Christopher Fauleta236c582023-05-09 16:31:01 +02003318 if (buffer_almost_full(&rep->buf)) {
3319 sc_need_room(sc, b_size(&rep->buf) / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003320 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003321 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003322 }
3323
Willy Tarreau91cefca2022-05-03 17:08:29 +02003324 if (ctx->flags & STAT_BOUND) {
3325 if (!(ctx->type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003326 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003327 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003328 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003329
Willy Tarreau91cefca2022-05-03 17:08:29 +02003330 if (ctx->sid != -1 && sv->puid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003331 continue;
3332 }
3333
Willy Tarreau3e320362020-10-23 17:28:57 +02003334 /* do not report disabled servers */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003335 if (ctx->flags & STAT_HIDE_MAINT &&
Willy Tarreau3e320362020-10-23 17:28:57 +02003336 sv->cur_admin & SRV_ADMF_MAINT) {
3337 continue;
3338 }
3339
William Lallemand74c24fb2016-11-21 17:18:36 +01003340 svs = sv;
3341 while (svs->track)
3342 svs = svs->track;
3343
3344 /* do not report servers which are DOWN and not changing state */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003345 if ((ctx->flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003346 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3347 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003348 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3349 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3350 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3351 continue;
3352 }
3353
Willy Tarreaucaff6312022-05-27 10:17:46 +02003354 if (stats_dump_sv_stats(sc, px, sv)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003355 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003356 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003357 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003358 if (ctx->field)
3359 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003360 }
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003361 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003362 } /* for sv */
3363
Willy Tarreau91cefca2022-05-03 17:08:29 +02003364 ctx->px_st = STAT_PX_ST_BE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003365 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003366
3367 case STAT_PX_ST_BE:
3368 /* print the backend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003369 if (stats_dump_be_stats(sc, px)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003370 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003371 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003372 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003373 if (ctx->field)
3374 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003375 }
3376
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003377 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003378 ctx->px_st = STAT_PX_ST_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003379 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003380
3381 case STAT_PX_ST_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003382 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003383 stats_dump_html_px_end(sc, px);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003384 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003385 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003386 }
3387
Willy Tarreau91cefca2022-05-03 17:08:29 +02003388 ctx->px_st = STAT_PX_ST_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003389 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003390
3391 case STAT_PX_ST_FIN:
3392 return 1;
3393
3394 default:
3395 /* unknown state, we should put an abort() here ! */
3396 return 1;
3397 }
Christopher Fauletef779222018-10-31 08:47:01 +01003398
3399 full:
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003400 /* restore previous field */
3401 ctx->field = current_field;
Christopher Fauletef779222018-10-31 08:47:01 +01003402 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003403}
3404
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003405/* Dumps the HTTP stats head block to the local trash buffer and uses the
3406 * per-uri parameters from the parent proxy. The caller is responsible for
3407 * clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003408 */
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003409static void stats_dump_html_head(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01003410{
Willy Tarreau91cefca2022-05-03 17:08:29 +02003411 struct show_stat_ctx *ctx = appctx->svcctx;
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003412 struct uri_auth *uri;
3413
3414 BUG_ON(!ctx->http_px);
3415 uri = ctx->http_px->uri_auth;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003416
William Lallemand74c24fb2016-11-21 17:18:36 +01003417 /* WARNING! This must fit in the first buffer !!! */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003418 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003419 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3420 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3421 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003422 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003423 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3424 "<style type=\"text/css\"><!--\n"
3425 "body {"
3426 " font-family: arial, helvetica, sans-serif;"
3427 " font-size: 12px;"
3428 " font-weight: normal;"
3429 " color: black;"
3430 " background: white;"
3431 "}\n"
3432 "th,td {"
3433 " font-size: 10px;"
3434 "}\n"
3435 "h1 {"
3436 " font-size: x-large;"
3437 " margin-bottom: 0.5em;"
3438 "}\n"
3439 "h2 {"
3440 " font-family: helvetica, arial;"
3441 " font-size: x-large;"
3442 " font-weight: bold;"
3443 " font-style: italic;"
3444 " color: #6020a0;"
3445 " margin-top: 0em;"
3446 " margin-bottom: 0em;"
3447 "}\n"
3448 "h3 {"
3449 " font-family: helvetica, arial;"
3450 " font-size: 16px;"
3451 " font-weight: bold;"
3452 " color: #b00040;"
3453 " background: #e8e8d0;"
3454 " margin-top: 0em;"
3455 " margin-bottom: 0em;"
3456 "}\n"
3457 "li {"
3458 " margin-top: 0.25em;"
3459 " margin-right: 2em;"
3460 "}\n"
3461 ".hr {margin-top: 0.25em;"
3462 " border-color: black;"
3463 " border-bottom-style: solid;"
3464 "}\n"
3465 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3466 ".total {background: #20D0D0;color: #ffff80;}\n"
3467 ".frontend {background: #e8e8d0;}\n"
3468 ".socket {background: #d0d0d0;}\n"
3469 ".backend {background: #e8e8d0;}\n"
3470 ".active_down {background: #ff9090;}\n"
3471 ".active_going_up {background: #ffd020;}\n"
3472 ".active_going_down {background: #ffffa0;}\n"
3473 ".active_up {background: #c0ffc0;}\n"
3474 ".active_nolb {background: #20a0ff;}\n"
3475 ".active_draining {background: #20a0FF;}\n"
3476 ".active_no_check {background: #e0e0e0;}\n"
3477 ".backup_down {background: #ff9090;}\n"
3478 ".backup_going_up {background: #ff80ff;}\n"
3479 ".backup_going_down {background: #c060ff;}\n"
3480 ".backup_up {background: #b0d0ff;}\n"
3481 ".backup_nolb {background: #90b0e0;}\n"
3482 ".backup_draining {background: #cc9900;}\n"
3483 ".backup_no_check {background: #e0e0e0;}\n"
3484 ".maintain {background: #c07820;}\n"
3485 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3486 "\n"
3487 "a.px:link {color: #ffff40; text-decoration: none;}"
3488 "a.px:visited {color: #ffff40; text-decoration: none;}"
3489 "a.px:hover {color: #ffffff; text-decoration: none;}"
3490 "a.lfsb:link {color: #000000; text-decoration: none;}"
3491 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3492 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3493 "\n"
3494 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3495 "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"
3496 "table.tbl td.ac { text-align: center;}\n"
3497 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3498 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3499 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3500 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3501 "\n"
3502 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3503 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3504 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3505 "table.det { border-collapse: collapse; border-style: none; }\n"
3506 "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"
3507 "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"
3508 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003509 "div.tips {\n"
3510 " display:block;\n"
3511 " visibility:hidden;\n"
3512 " z-index:2147483647;\n"
3513 " position:absolute;\n"
3514 " padding:2px 4px 3px;\n"
3515 " background:#f0f060; color:#000000;\n"
3516 " border:1px solid #7040c0;\n"
3517 " white-space:nowrap;\n"
3518 " font-style:normal;font-size:11px;font-weight:normal;\n"
3519 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3520 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3521 "}\n"
3522 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003523 "@media (prefers-color-scheme: dark) {\n"
3524 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3525 " h1 { color: #a265e0!important; }\n"
3526 " h2 { color: #a265e0; }\n"
3527 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3528 " a { color: #3391ff; }\n"
3529 " input { background-color: #2f3437; }\n"
3530 " .hr { border-color: #8c8273; }\n"
3531 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3532 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003533 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003534 " .backend {background: #2f3437;}\n"
3535 " .active_down {background: #760000;}\n"
3536 " .active_going_up {background: #b99200;}\n"
3537 " .active_going_down {background: #6c6c00;}\n"
3538 " .active_up {background: #165900;}\n"
3539 " .active_nolb {background: #006ab9;}\n"
3540 " .active_draining {background: #006ab9;}\n"
3541 " .active_no_check {background: #2a2d2f;}\n"
3542 " .backup_down {background: #760000;}\n"
3543 " .backup_going_up {background: #7f007f;}\n"
3544 " .backup_going_down {background: #580092;}\n"
3545 " .backup_up {background: #2e3234;}\n"
3546 " .backup_nolb {background: #1e3c6a;}\n"
3547 " .backup_draining {background: #a37a00;}\n"
3548 " .backup_no_check {background: #2a2d2f;}\n"
3549 " .maintain {background: #9a601a;}\n"
3550 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3551 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3552 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3553 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3554 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3555 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3556 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003557 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003558 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3559 " table.tbl th { border-color: #808080; }\n"
3560 " table.tbl td { border-color: #808080; }\n"
3561 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3562 " div.tips {\n"
3563 " background:#8e8e0d;\n"
3564 " color:#e8e6e3;\n"
3565 " border-color: #4e2c86;\n"
3566 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3567 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3568 " box-shadow: #60686c 2px 2px 3px;\n"
3569 " }\n"
3570 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003571 "-->\n"
3572 "</style></head>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003573 (ctx->flags & STAT_SHNODE) ? " on " : "",
3574 (ctx->flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003575 );
3576}
3577
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003578/* Dumps the HTML stats information block to the local trash buffer and uses
3579 * the state from stream connector <sc> and per-uri parameter from the parent
3580 * proxy. The caller is responsible for clearing the local trash buffer if
3581 * needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003582 */
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003583static void stats_dump_html_info(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01003584{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003585 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003586 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc05d30e2023-04-28 14:50:29 +02003587 unsigned int up = ns_to_sec(now_ns - start_time_ns);
William Lallemand74c24fb2016-11-21 17:18:36 +01003588 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02003589 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003590 struct uri_auth *uri;
Willy Tarreau6be8d092023-01-12 16:08:41 +01003591 unsigned long long bps;
3592 int thr;
3593
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003594 BUG_ON(!ctx->http_px);
3595 uri = ctx->http_px->uri_auth;
Willy Tarreau6be8d092023-01-12 16:08:41 +01003596 for (bps = thr = 0; thr < global.nbthread; thr++)
3597 bps += 32ULL * read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
Willy Tarreau1713c032019-05-23 12:23:55 +02003598
3599 /* Turn the bytes per second to bits per second and take care of the
3600 * usual ethernet overhead in order to help figure how far we are from
3601 * interface saturation since it's the only case which usually matters.
3602 * For this we count the total size of an Ethernet frame on the wire
3603 * including preamble and IFG (1538) for the largest TCP segment it
3604 * transports (1448 with TCP timestamps). This is not valid for smaller
3605 * packets (under-estimated), but it gives a reasonably accurate
3606 * estimation of how far we are from uplink saturation.
3607 */
3608 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003609
3610 /* WARNING! this has to fit the first packet too.
3611 * We are around 3.5 kB, add adding entries will
3612 * become tricky if we want to support 4kB buffers !
3613 */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003614 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003615 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3616 PRODUCT_NAME "%s</a></h1>\n"
3617 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3618 "<hr width=\"100%%\" class=\"hr\">\n"
3619 "<h3>&gt; General process information</h3>\n"
3620 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003621 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
Willy Tarreau3c4a2972023-05-11 12:02:21 +02003622 "<b>uptime = </b> %dd %dh%02dm%02ds; warnings = %u<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003623 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
Willy Tarreau96151022023-05-11 13:51:31 +02003624 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>reached = </b> %llu; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02003625 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003626 "Running tasks: %d/%d; idle = %d %%<br>\n"
3627 "</td><td align=\"center\" nowrap>\n"
3628 "<table class=\"lgd\"><tr>\n"
3629 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3630 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3631 "</tr><tr>\n"
3632 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3633 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3634 "</tr><tr>\n"
3635 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3636 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3637 "</tr><tr>\n"
3638 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3639 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3640 "</tr><tr>\n"
3641 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3642 "</tr><tr>\n"
3643 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3644 "</tr></table>\n"
3645 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3646 "</td>"
3647 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3648 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3649 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003650 (ctx->flags & STAT_HIDEVER) ? "" : (stats_version_string),
3651 pid, (ctx->flags & STAT_SHNODE) ? " on " : "",
3652 (ctx->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3653 (ctx->flags & STAT_SHDESC) ? ": " : "",
3654 (ctx->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003655 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003656 up / 86400, (up % 86400) / 3600,
3657 (up % 3600) / 60, (up % 60),
Willy Tarreau3c4a2972023-05-11 12:02:21 +02003658 HA_ATOMIC_LOAD(&tot_warnings),
William Lallemand74c24fb2016-11-21 17:18:36 +01003659 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3660 global.rlimit_memmax ? " MB" : "",
3661 global.rlimit_nofile,
Willy Tarreau96151022023-05-11 13:51:31 +02003662 global.maxsock, global.maxconn, HA_ATOMIC_LOAD(&maxconn_reached), global.maxpipes,
William Lallemand74c24fb2016-11-21 17:18:36 +01003663 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003664 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3665 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003666 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003667 );
3668
Willy Tarreau91cefca2022-05-03 17:08:29 +02003669 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3670 memcpy(scope_txt, scope_ptr, ctx->scope_len);
3671 scope_txt[ctx->scope_len] = '\0';
William Lallemand74c24fb2016-11-21 17:18:36 +01003672
Christopher Fauleta8b76842022-12-16 15:08:36 +01003673 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003674 "<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 +02003675 (ctx->scope_len > 0) ? scope_txt : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003676 STAT_SCOPE_TXT_MAXLEN);
3677
Willy Tarreau91cefca2022-05-03 17:08:29 +02003678 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003679 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003680 if (ctx->scope_len) {
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003681 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02003682 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3683 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003684 }
3685
Willy Tarreau91cefca2022-05-03 17:08:29 +02003686 if (ctx->flags & STAT_HIDE_DOWN)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003687 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003688 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3689 uri->uri_prefix,
3690 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003691 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003692 scope_txt);
3693 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003694 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003695 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3696 uri->uri_prefix,
3697 ";up",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003698 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003699 scope_txt);
3700
3701 if (uri->refresh > 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003702 if (ctx->flags & STAT_NO_REFRESH)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003703 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003704 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3705 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003706 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003707 "",
3708 scope_txt);
3709 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003710 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003711 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3712 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003713 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003714 ";norefresh",
3715 scope_txt);
3716 }
3717
Christopher Fauleta8b76842022-12-16 15:08:36 +01003718 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003719 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3720 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003721 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3722 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003723 scope_txt);
3724
Christopher Fauleta8b76842022-12-16 15:08:36 +01003725 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003726 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3727 uri->uri_prefix,
3728 (uri->refresh > 0) ? ";norefresh" : "",
3729 scope_txt);
3730
Christopher Fauleta8b76842022-12-16 15:08:36 +01003731 chunk_appendf(&trash_chunk,
Christopher Faulet6338a082019-09-09 15:50:54 +02003732 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3733 uri->uri_prefix,
3734 (uri->refresh > 0) ? ";norefresh" : "",
3735 scope_txt, uri->uri_prefix);
3736
Christopher Fauleta8b76842022-12-16 15:08:36 +01003737 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003738 "</ul></td>"
3739 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3740 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3741 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3742 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3743 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3744 "</ul>"
3745 "</td>"
3746 "</tr></table>\n"
3747 ""
3748 );
3749
Willy Tarreau91cefca2022-05-03 17:08:29 +02003750 if (ctx->st_code) {
3751 switch (ctx->st_code) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003752 case STAT_STATUS_DONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003753 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003754 "<p><div class=active_up>"
3755 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3756 "Action processed successfully."
3757 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003758 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3759 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003760 scope_txt);
3761 break;
3762 case STAT_STATUS_NONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003763 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003764 "<p><div class=active_going_down>"
3765 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3766 "Nothing has changed."
3767 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003768 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3769 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003770 scope_txt);
3771 break;
3772 case STAT_STATUS_PART:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003773 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003774 "<p><div class=active_going_down>"
3775 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3776 "Action partially processed.<br>"
3777 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3778 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003779 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3780 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003781 scope_txt);
3782 break;
3783 case STAT_STATUS_ERRP:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003784 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003785 "<p><div class=active_down>"
3786 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3787 "Action not processed because of invalid parameters."
3788 "<ul>"
3789 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003790 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003791 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3792 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3793 "</ul>"
3794 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003795 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3796 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003797 scope_txt);
3798 break;
3799 case STAT_STATUS_EXCD:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003800 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003801 "<p><div class=active_down>"
3802 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3803 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3804 "You should retry with less servers at a time.</b>"
3805 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003806 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3807 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003808 scope_txt);
3809 break;
3810 case STAT_STATUS_DENY:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003811 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003812 "<p><div class=active_down>"
3813 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3814 "<b>Action denied.</b>"
3815 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003816 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3817 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003818 scope_txt);
3819 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003820 case STAT_STATUS_IVAL:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003821 chunk_appendf(&trash_chunk,
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003822 "<p><div class=active_down>"
3823 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3824 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3825 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003826 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3827 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003828 scope_txt);
3829 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003830 default:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003831 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003832 "<p><div class=active_no_check>"
3833 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3834 "Unexpected result."
3835 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003836 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3837 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003838 scope_txt);
3839 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003840 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003841 }
3842}
3843
Christopher Fauleta8b76842022-12-16 15:08:36 +01003844/* Dumps the HTML stats trailer block to the local trash buffer. The caller is
3845 * responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003846 */
3847static void stats_dump_html_end()
3848{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003849 chunk_appendf(&trash_chunk, "</body></html>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003850}
3851
Christopher Fauleta8b76842022-12-16 15:08:36 +01003852/* Dumps the stats JSON header to the local trash buffer buffer which. The
3853 * caller is responsible for clearing it if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003854 */
3855static void stats_dump_json_header()
3856{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003857 chunk_strcat(&trash_chunk, "[");
Simon Horman05ee2132017-01-04 09:37:25 +01003858}
3859
3860
Christopher Fauleta8b76842022-12-16 15:08:36 +01003861/* Dumps the JSON stats trailer block to the local trash buffer. The caller is
3862 * responsible for clearing the local trash buffer if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003863 */
3864static void stats_dump_json_end()
3865{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003866 chunk_strcat(&trash_chunk, "]\n");
Simon Horman05ee2132017-01-04 09:37:25 +01003867}
3868
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003869/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3870 * a pointer to the current server/listener.
3871 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003872static int stats_dump_proxies(struct stconn *sc,
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003873 struct htx *htx)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003874{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003875 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003876 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003877 struct channel *rep = sc_ic(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003878 struct proxy *px;
3879
3880 /* dump proxies */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003881 while (ctx->obj1) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003882 if (htx) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003883 if (htx_almost_full(htx)) {
3884 sc_need_room(sc, htx->size / 2);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003885 goto full;
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003886 }
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003887 }
3888 else {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003889 if (buffer_almost_full(&rep->buf)) {
3890 sc_need_room(sc, b_size(&rep->buf) / 2);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003891 goto full;
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003892 }
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003893 }
3894
Willy Tarreau91cefca2022-05-03 17:08:29 +02003895 px = ctx->obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003896 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003897 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003898 * This change allows retrieving stats from "old" proxies after a reload.
3899 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003900 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003901 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003902 if (stats_dump_proxy_to_buffer(sc, htx, px) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003903 return 0;
3904 }
3905
Willy Tarreau91cefca2022-05-03 17:08:29 +02003906 ctx->obj1 = px->next;
3907 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003908 ctx->field = 0;
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003909 }
3910
3911 return 1;
3912
3913 full:
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003914 return 0;
3915}
3916
Willy Tarreau4596fe22022-05-17 19:07:51 +02003917/* This function dumps statistics onto the stream connector's read buffer in
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003918 * either CSV or HTML format. It returns 0 if it had to stop writing data and
3919 * an I/O is needed, 1 if the dump is finished and the stream must be closed,
3920 * or -1 in case of any error. This function is used by both the CLI and the
3921 * HTTP handlers.
William Lallemand74c24fb2016-11-21 17:18:36 +01003922 */
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003923static int stats_dump_stat_to_buffer(struct stconn *sc, struct htx *htx)
William Lallemand74c24fb2016-11-21 17:18:36 +01003924{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003925 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003926 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003927 enum stats_domain domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003928
Christopher Fauleta8b76842022-12-16 15:08:36 +01003929 chunk_reset(&trash_chunk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003930
Willy Tarreau41f88522022-05-03 18:39:27 +02003931 switch (ctx->state) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003932 case STAT_STATE_INIT:
3933 ctx->state = STAT_STATE_HEAD; /* let's start producing data */
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003934 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003935
Willy Tarreau6ef16482022-05-06 18:07:53 +02003936 case STAT_STATE_HEAD:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003937 if (ctx->flags & STAT_FMT_HTML)
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003938 stats_dump_html_head(appctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003939 else if (ctx->flags & STAT_JSON_SCHM)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003940 stats_dump_json_schema(&trash_chunk);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003941 else if (ctx->flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003942 stats_dump_json_header();
Willy Tarreau91cefca2022-05-03 17:08:29 +02003943 else if (!(ctx->flags & STAT_FMT_TYPED))
3944 stats_dump_csv_header(ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003945
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003946 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003947 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003948
Willy Tarreau91cefca2022-05-03 17:08:29 +02003949 if (ctx->flags & STAT_JSON_SCHM) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003950 ctx->state = STAT_STATE_FIN;
Christopher Faulet6338a082019-09-09 15:50:54 +02003951 return 1;
3952 }
Willy Tarreau6ef16482022-05-06 18:07:53 +02003953 ctx->state = STAT_STATE_INFO;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003954 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003955
Willy Tarreau6ef16482022-05-06 18:07:53 +02003956 case STAT_STATE_INFO:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003957 if (ctx->flags & STAT_FMT_HTML) {
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003958 stats_dump_html_info(sc);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003959 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003960 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003961 }
3962
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003963 if (domain == STATS_DOMAIN_PROXY)
Willy Tarreau91cefca2022-05-03 17:08:29 +02003964 ctx->obj1 = proxies_list;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003965
Willy Tarreau91cefca2022-05-03 17:08:29 +02003966 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003967 ctx->field = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003968 ctx->state = STAT_STATE_LIST;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003969 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003970
Willy Tarreau6ef16482022-05-06 18:07:53 +02003971 case STAT_STATE_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003972 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003973 case STATS_DOMAIN_RESOLVERS:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003974 if (!stats_dump_resolvers(sc, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003975 stat_count[domain],
3976 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003977 return 0;
3978 }
3979 break;
3980
3981 case STATS_DOMAIN_PROXY:
3982 default:
3983 /* dump proxies */
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01003984 if (!stats_dump_proxies(sc, htx))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003985 return 0;
3986 break;
3987 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003988
Willy Tarreau6ef16482022-05-06 18:07:53 +02003989 ctx->state = STAT_STATE_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003990 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003991
Willy Tarreau6ef16482022-05-06 18:07:53 +02003992 case STAT_STATE_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003993 if (ctx->flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3994 if (ctx->flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01003995 stats_dump_html_end();
3996 else
3997 stats_dump_json_end();
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003998 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003999 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01004000 }
4001
Willy Tarreau6ef16482022-05-06 18:07:53 +02004002 ctx->state = STAT_STATE_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01004003 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01004004
Willy Tarreau6ef16482022-05-06 18:07:53 +02004005 case STAT_STATE_FIN:
William Lallemand74c24fb2016-11-21 17:18:36 +01004006 return 1;
4007
4008 default:
4009 /* unknown state ! */
Willy Tarreau6ef16482022-05-06 18:07:53 +02004010 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01004011 return -1;
4012 }
Christopher Fauletef779222018-10-31 08:47:01 +01004013
4014 full:
Christopher Fauletef779222018-10-31 08:47:01 +01004015 return 0;
4016
William Lallemand74c24fb2016-11-21 17:18:36 +01004017}
4018
4019/* We reached the stats page through a POST request. The appctx is
4020 * expected to have already been allocated by the caller.
4021 * Parse the posted data and enable/disable servers if necessary.
4022 * Returns 1 if request was parsed or zero if it needs more data.
4023 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004024static int stats_process_http_post(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01004025{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004026 struct stream *s = __sc_strm(sc);
4027 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004028 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01004029
4030 struct proxy *px = NULL;
4031 struct server *sv = NULL;
4032
4033 char key[LINESIZE];
4034 int action = ST_ADM_ACTION_NONE;
4035 int reprocess = 0;
4036
4037 int total_servers = 0;
4038 int altered_servers = 0;
4039
4040 char *first_param, *cur_param, *next_param, *end_params;
4041 char *st_cur_param = NULL;
4042 char *st_next_param = NULL;
4043
Christopher Fauleta3618372018-12-13 21:59:56 +01004044 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01004045
Christopher Fauletb7f88902019-07-15 21:56:43 +02004046 struct htx *htx = htxbuf(&s->req.buf);
4047 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01004048
Christopher Fauletb7f88902019-07-15 21:56:43 +02004049 /* we need more data */
4050 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
4051 /* check if we can receive more */
4052 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004053 ctx->st_code = STAT_STATUS_EXCD;
Christopher Fauletb7f88902019-07-15 21:56:43 +02004054 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01004055 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004056 goto wait;
4057 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004058
Christopher Fauletb7f88902019-07-15 21:56:43 +02004059 /* The request was fully received. Copy data */
4060 blk = htx_get_head_blk(htx);
4061 while (blk) {
4062 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004063
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004064 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02004065 break;
4066 if (type == HTX_BLK_DATA) {
4067 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004068
Christopher Fauletb7f88902019-07-15 21:56:43 +02004069 if (!chunk_memcat(temp, v.ptr, v.len)) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004070 ctx->st_code = STAT_STATUS_EXCD;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004071 goto out;
4072 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004073 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004074 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01004075 }
4076
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004077 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01004078 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01004079 cur_param = next_param = end_params;
4080 *end_params = '\0';
4081
Willy Tarreau91cefca2022-05-03 17:08:29 +02004082 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004083
4084 /*
4085 * Parse the parameters in reverse order to only store the last value.
4086 * From the html form, the backend and the action are at the end.
4087 */
4088 while (cur_param > first_param) {
4089 char *value;
4090 int poffset, plen;
4091
4092 cur_param--;
4093
4094 if ((*cur_param == '&') || (cur_param == first_param)) {
4095 reprocess_servers:
4096 /* Parse the key */
4097 poffset = (cur_param != first_param ? 1 : 0);
4098 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4099 if ((plen > 0) && (plen <= sizeof(key))) {
4100 strncpy(key, cur_param + poffset, plen);
4101 key[plen - 1] = '\0';
4102 } else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004103 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004104 goto out;
4105 }
4106
4107 /* Parse the value */
4108 value = key;
4109 while (*value != '\0' && *value != '=') {
4110 value++;
4111 }
4112 if (*value == '=') {
4113 /* Ok, a value is found, we can mark the end of the key */
4114 *value++ = '\0';
4115 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02004116 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01004117 break;
4118
4119 /* Now we can check the key to see what to do */
4120 if (!px && (strcmp(key, "b") == 0)) {
4121 if ((px = proxy_be_by_name(value)) == NULL) {
4122 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004123 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004124 goto out;
4125 }
4126 }
4127 else if (!action && (strcmp(key, "action") == 0)) {
4128 if (strcmp(value, "ready") == 0) {
4129 action = ST_ADM_ACTION_READY;
4130 }
4131 else if (strcmp(value, "drain") == 0) {
4132 action = ST_ADM_ACTION_DRAIN;
4133 }
4134 else if (strcmp(value, "maint") == 0) {
4135 action = ST_ADM_ACTION_MAINT;
4136 }
4137 else if (strcmp(value, "shutdown") == 0) {
4138 action = ST_ADM_ACTION_SHUTDOWN;
4139 }
4140 else if (strcmp(value, "dhlth") == 0) {
4141 action = ST_ADM_ACTION_DHLTH;
4142 }
4143 else if (strcmp(value, "ehlth") == 0) {
4144 action = ST_ADM_ACTION_EHLTH;
4145 }
4146 else if (strcmp(value, "hrunn") == 0) {
4147 action = ST_ADM_ACTION_HRUNN;
4148 }
4149 else if (strcmp(value, "hnolb") == 0) {
4150 action = ST_ADM_ACTION_HNOLB;
4151 }
4152 else if (strcmp(value, "hdown") == 0) {
4153 action = ST_ADM_ACTION_HDOWN;
4154 }
4155 else if (strcmp(value, "dagent") == 0) {
4156 action = ST_ADM_ACTION_DAGENT;
4157 }
4158 else if (strcmp(value, "eagent") == 0) {
4159 action = ST_ADM_ACTION_EAGENT;
4160 }
4161 else if (strcmp(value, "arunn") == 0) {
4162 action = ST_ADM_ACTION_ARUNN;
4163 }
4164 else if (strcmp(value, "adown") == 0) {
4165 action = ST_ADM_ACTION_ADOWN;
4166 }
4167 /* else these are the old supported methods */
4168 else if (strcmp(value, "disable") == 0) {
4169 action = ST_ADM_ACTION_DISABLE;
4170 }
4171 else if (strcmp(value, "enable") == 0) {
4172 action = ST_ADM_ACTION_ENABLE;
4173 }
4174 else if (strcmp(value, "stop") == 0) {
4175 action = ST_ADM_ACTION_STOP;
4176 }
4177 else if (strcmp(value, "start") == 0) {
4178 action = ST_ADM_ACTION_START;
4179 }
4180 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004181 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004182 goto out;
4183 }
4184 }
4185 else if (strcmp(key, "s") == 0) {
4186 if (!(px && action)) {
4187 /*
4188 * Indicates that we'll need to reprocess the parameters
4189 * as soon as backend and action are known
4190 */
4191 if (!reprocess) {
4192 st_cur_param = cur_param;
4193 st_next_param = next_param;
4194 }
4195 reprocess = 1;
4196 }
4197 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004198 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004199 switch (action) {
4200 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004201 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004202 altered_servers++;
4203 total_servers++;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004204 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, SRV_ADM_STCHGC_STATS_DISABLE);
William Lallemand74c24fb2016-11-21 17:18:36 +01004205 }
4206 break;
4207 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004208 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004209 altered_servers++;
4210 total_servers++;
4211 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4212 }
4213 break;
4214 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004215 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004216 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, SRV_ADM_STCHGC_STATS_STOP);
William Lallemand74c24fb2016-11-21 17:18:36 +01004217 altered_servers++;
4218 total_servers++;
4219 }
4220 break;
4221 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004222 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004223 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4224 altered_servers++;
4225 total_servers++;
4226 }
4227 break;
4228 case ST_ADM_ACTION_DHLTH:
4229 if (sv->check.state & CHK_ST_CONFIGURED) {
4230 sv->check.state &= ~CHK_ST_ENABLED;
4231 altered_servers++;
4232 total_servers++;
4233 }
4234 break;
4235 case ST_ADM_ACTION_EHLTH:
4236 if (sv->check.state & CHK_ST_CONFIGURED) {
4237 sv->check.state |= CHK_ST_ENABLED;
4238 altered_servers++;
4239 total_servers++;
4240 }
4241 break;
4242 case ST_ADM_ACTION_HRUNN:
4243 if (!(sv->track)) {
4244 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004245 srv_set_running(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004246 altered_servers++;
4247 total_servers++;
4248 }
4249 break;
4250 case ST_ADM_ACTION_HNOLB:
4251 if (!(sv->track)) {
4252 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004253 srv_set_stopping(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004254 altered_servers++;
4255 total_servers++;
4256 }
4257 break;
4258 case ST_ADM_ACTION_HDOWN:
4259 if (!(sv->track)) {
4260 sv->check.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004261 srv_set_stopped(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004262 altered_servers++;
4263 total_servers++;
4264 }
4265 break;
4266 case ST_ADM_ACTION_DAGENT:
4267 if (sv->agent.state & CHK_ST_CONFIGURED) {
4268 sv->agent.state &= ~CHK_ST_ENABLED;
4269 altered_servers++;
4270 total_servers++;
4271 }
4272 break;
4273 case ST_ADM_ACTION_EAGENT:
4274 if (sv->agent.state & CHK_ST_CONFIGURED) {
4275 sv->agent.state |= CHK_ST_ENABLED;
4276 altered_servers++;
4277 total_servers++;
4278 }
4279 break;
4280 case ST_ADM_ACTION_ARUNN:
4281 if (sv->agent.state & CHK_ST_ENABLED) {
4282 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004283 srv_set_running(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004284 altered_servers++;
4285 total_servers++;
4286 }
4287 break;
4288 case ST_ADM_ACTION_ADOWN:
4289 if (sv->agent.state & CHK_ST_ENABLED) {
4290 sv->agent.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004291 srv_set_stopped(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004292 altered_servers++;
4293 total_servers++;
4294 }
4295 break;
4296 case ST_ADM_ACTION_READY:
4297 srv_adm_set_ready(sv);
4298 altered_servers++;
4299 total_servers++;
4300 break;
4301 case ST_ADM_ACTION_DRAIN:
4302 srv_adm_set_drain(sv);
4303 altered_servers++;
4304 total_servers++;
4305 break;
4306 case ST_ADM_ACTION_MAINT:
4307 srv_adm_set_maint(sv);
4308 altered_servers++;
4309 total_servers++;
4310 break;
4311 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004312 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004313 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004314 altered_servers++;
4315 total_servers++;
4316 }
4317 break;
4318 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004319 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004320 } else {
4321 /* the server name is unknown or ambiguous (duplicate names) */
4322 total_servers++;
4323 }
4324 }
4325 if (reprocess && px && action) {
4326 /* Now, we know the backend and the action chosen by the user.
4327 * We can safely restart from the first server parameter
4328 * to reprocess them
4329 */
4330 cur_param = st_cur_param;
4331 next_param = st_next_param;
4332 reprocess = 0;
4333 goto reprocess_servers;
4334 }
4335
4336 next_param = cur_param;
4337 }
4338 }
4339
4340 if (total_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004341 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004342 }
4343 else if (altered_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004344 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004345 }
4346 else if (altered_servers == total_servers) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004347 ctx->st_code = STAT_STATUS_DONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004348 }
4349 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004350 ctx->st_code = STAT_STATUS_PART;
William Lallemand74c24fb2016-11-21 17:18:36 +01004351 }
4352 out:
4353 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004354 wait:
Willy Tarreau91cefca2022-05-03 17:08:29 +02004355 ctx->st_code = STAT_STATUS_NONE;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004356 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004357}
4358
4359
Willy Tarreaucaff6312022-05-27 10:17:46 +02004360static int stats_send_http_headers(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004361{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004362 struct stream *s = __sc_strm(sc);
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01004363 struct uri_auth *uri;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004364 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004365 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004366 struct htx_sl *sl;
4367 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004368
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01004369 BUG_ON(!ctx->http_px);
4370 uri = ctx->http_px->uri_auth;
4371
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004372 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);
4373 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4374 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004375 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004376 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004377
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004378 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004379 goto full;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004380 if (ctx->flags & STAT_FMT_HTML) {
Christopher Fauletef779222018-10-31 08:47:01 +01004381 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4382 goto full;
4383 }
Willy Tarreau91cefca2022-05-03 17:08:29 +02004384 else if (ctx->flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004385 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4386 goto full;
4387 }
Christopher Fauletef779222018-10-31 08:47:01 +01004388 else {
4389 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4390 goto full;
4391 }
4392
Willy Tarreau91cefca2022-05-03 17:08:29 +02004393 if (uri->refresh > 0 && !(ctx->flags & STAT_NO_REFRESH)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004394 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004395 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004396 goto full;
4397 }
4398
Willy Tarreau91cefca2022-05-03 17:08:29 +02004399 if (ctx->flags & STAT_CHUNKED) {
Christopher Fauletef779222018-10-31 08:47:01 +01004400 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4401 goto full;
4402 }
4403
4404 if (!htx_add_endof(htx, HTX_BLK_EOH))
4405 goto full;
4406
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004407 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004408 return 1;
4409
4410 full:
4411 htx_reset(htx);
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004412 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004413 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004414}
4415
Christopher Fauletef779222018-10-31 08:47:01 +01004416
Willy Tarreaucaff6312022-05-27 10:17:46 +02004417static int stats_send_http_redirect(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004418{
4419 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02004420 struct stream *s = __sc_strm(sc);
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01004421 struct uri_auth *uri;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004422 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004423 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004424 struct htx_sl *sl;
4425 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004426
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01004427 BUG_ON(!ctx->http_px);
4428 uri = ctx->http_px->uri_auth;
4429
Willy Tarreau91cefca2022-05-03 17:08:29 +02004430 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauletef779222018-10-31 08:47:01 +01004431 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004432 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004433 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004434
Willy Tarreaufc458ec2023-04-07 18:11:39 +02004435 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02004436 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
4437 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004438 }
4439
4440 /* We don't want to land on the posted stats page because a refresh will
4441 * repost the data. We don't want this to happen on accident so we redirect
4442 * the browse to the stats page with a GET.
4443 */
4444 chunk_printf(&trash, "%s;st=%s%s%s%s",
4445 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02004446 ((ctx->st_code > STAT_STATUS_INIT) &&
4447 (ctx->st_code < STAT_STATUS_SIZE) &&
4448 stat_status_codes[ctx->st_code]) ?
4449 stat_status_codes[ctx->st_code] :
Christopher Fauletef779222018-10-31 08:47:01 +01004450 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau91cefca2022-05-03 17:08:29 +02004451 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
4452 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Fauletef779222018-10-31 08:47:01 +01004453 scope_txt);
4454
Christopher Fauletea0a9a62024-05-17 15:42:46 +02004455 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN|HTX_SL_F_BODYLESS);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004456 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4457 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004458 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004459 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004460
4461 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004462 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4463 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4464 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4465 goto full;
4466
4467 if (!htx_add_endof(htx, HTX_BLK_EOH))
4468 goto full;
4469
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004470 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004471 return 1;
4472
4473full:
4474 htx_reset(htx);
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004475 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004476 return 0;
4477}
William Lallemand74c24fb2016-11-21 17:18:36 +01004478
Simon Horman05ee2132017-01-04 09:37:25 +01004479
Willy Tarreau4596fe22022-05-17 19:07:51 +02004480/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004481 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4482 * appctx->st0 contains the operation in progress (dump, done). The handler
4483 * automatically unregisters itself once transfer is complete.
4484 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004485static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004486{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004487 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02004488 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004489 struct stream *s = __sc_strm(sc);
4490 struct channel *req = sc_oc(sc);
4491 struct channel *res = sc_ic(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004492 struct htx *req_htx, *res_htx;
4493
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004494 /* only proxy stats are available via http */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004495 ctx->domain = STATS_DOMAIN_PROXY;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004496
Christopher Fauletef779222018-10-31 08:47:01 +01004497 res_htx = htx_from_buf(&res->buf);
4498
Christopher Faulet9837bd82023-04-11 07:41:30 +02004499 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) {
4500 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004501 goto out;
Christopher Faulet9837bd82023-04-11 07:41:30 +02004502 }
Christopher Fauletef779222018-10-31 08:47:01 +01004503
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004504 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004505 if (!b_size(&res->buf)) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004506 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004507 goto out;
4508 }
4509
Christopher Fauletef779222018-10-31 08:47:01 +01004510 /* all states are processed in sequence */
4511 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004512 if (stats_send_http_headers(sc, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004513 if (s->txn->meth == HTTP_METH_HEAD)
4514 appctx->st0 = STAT_HTTP_DONE;
4515 else
4516 appctx->st0 = STAT_HTTP_DUMP;
4517 }
4518 }
4519
4520 if (appctx->st0 == STAT_HTTP_DUMP) {
Aurelien DARRAGON14656842023-02-03 08:31:42 +01004521 trash_chunk = b_make(trash.area, res->buf.size, 0, 0);
Aurelien DARRAGON5e7ecbe2023-02-02 15:03:12 +01004522 /* adjust buffer size to take htx overhead into account,
4523 * make sure to perform this call on an empty buffer
4524 */
4525 trash_chunk.size = buf_room_for_htx_data(&trash_chunk);
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01004526 if (stats_dump_stat_to_buffer(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004527 appctx->st0 = STAT_HTTP_DONE;
4528 }
4529
4530 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004531 if (stats_process_http_post(sc))
Christopher Fauletef779222018-10-31 08:47:01 +01004532 appctx->st0 = STAT_HTTP_LAST;
Christopher Fauletca5309a2023-04-17 16:17:32 +02004533 else if (s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Fauletef779222018-10-31 08:47:01 +01004534 appctx->st0 = STAT_HTTP_DONE;
4535 }
4536
4537 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004538 if (stats_send_http_redirect(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004539 appctx->st0 = STAT_HTTP_DONE;
4540 }
4541
4542 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004543 /* no more data are expected. If the response buffer is empty,
4544 * be sure to add something (EOT block in this case) to have
4545 * something to send. It is important to be sure the EOM flags
4546 * will be handled by the endpoint.
4547 */
4548 if (htx_is_empty(res_htx)) {
4549 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004550 sc_need_room(sc, sizeof(struct htx_blk) + 1);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004551 goto out;
4552 }
4553 channel_add_input(res, 1);
4554 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004555 res_htx->flags |= HTX_FL_EOM;
Willy Tarreaud869e132022-05-17 18:05:31 +02004556 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004557 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004558 }
4559
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004560 if (appctx->st0 == STAT_HTTP_END) {
Christopher Fauletdf15a5d2023-03-31 11:28:22 +02004561 se_fl_set(appctx->sedesc, SE_FL_EOS);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004562 applet_will_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004563 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004564
Christopher Fauletef779222018-10-31 08:47:01 +01004565 out:
4566 /* we have left the request in the buffer for the case where we
4567 * process a POST, and this automatically re-enables activity on
4568 * read. It's better to indicate that we want to stop reading when
4569 * we're sending, so that we know there's at most one direction
4570 * deciding to wake the applet up. It saves it from looping when
4571 * emitting large blocks into small TCP windows.
4572 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004573 htx_to_buf(res_htx, &res->buf);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004574 if (appctx->st0 == STAT_HTTP_END) {
4575 /* eat the whole request */
4576 if (co_data(req)) {
4577 req_htx = htx_from_buf(&req->buf);
4578 co_htx_skip(req, req_htx, co_data(req));
4579 htx_to_buf(req_htx, &req->buf);
4580 }
4581 }
4582 else if (!channel_is_empty(res))
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02004583 applet_wont_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004584}
4585
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004586/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004587static int stats_dump_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004588 const struct field *info,
4589 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004590{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004591 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004592 int field;
4593
4594 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4595 if (!field_format(info, field))
4596 continue;
4597
Willy Tarreaueaa55372019-10-09 07:39:11 +02004598 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004599 return 0;
4600 if (!stats_emit_raw_data_field(out, &info[field]))
4601 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004602 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4603 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004604 if (!chunk_strcat(out, "\n"))
4605 return 0;
4606 }
4607 return 1;
4608}
4609
4610/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004611static int stats_dump_typed_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004612 const struct field *info,
4613 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004614{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004615 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004616 int field;
4617
4618 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4619 if (!field_format(info, field))
4620 continue;
4621
Willy Tarreaueaa55372019-10-09 07:39:11 +02004622 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 +01004623 return 0;
4624 if (!stats_emit_field_tags(out, &info[field], ':'))
4625 return 0;
4626 if (!stats_emit_typed_data_field(out, &info[field]))
4627 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004628 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4629 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004630 if (!chunk_strcat(out, "\n"))
4631 return 0;
4632 }
4633 return 1;
4634}
4635
Willy Tarreau0b26b382021-05-08 07:43:53 +02004636/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4637 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4638 * less then this value, the function returns 0, otherwise, it returns 1. Some
4639 * fields' presence or precision may depend on some of the STAT_* flags present
4640 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004641 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004642int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004643{
Willy Tarreau83061a82018-07-13 11:56:34 +02004644 struct buffer *out = get_trash_chunk();
Willy Tarreau6be8d092023-01-12 16:08:41 +01004645 uint64_t glob_out_bytes, glob_spl_bytes, glob_out_b32;
Willy Tarreau563efe62023-04-27 14:41:37 +02004646 uint up_sec, up_usec;
4647 ullong up;
Willy Tarreau5723b382023-05-17 09:05:20 +02004648 ulong boot;
Willy Tarreau6be8d092023-01-12 16:08:41 +01004649 int thr;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004650
4651#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004652 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4653 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4654 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004655
Willy Tarreauc5977722021-05-08 08:14:04 +02004656 if (ssl_key_rate < ssl_sess_rate)
4657 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004658#endif
4659
Willy Tarreau6be8d092023-01-12 16:08:41 +01004660 /* sum certain per-thread totals (mostly byte counts) */
4661 glob_out_bytes = glob_spl_bytes = glob_out_b32 = 0;
4662 for (thr = 0; thr < global.nbthread; thr++) {
4663 glob_out_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].out_bytes);
4664 glob_spl_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].spliced_out_bytes);
4665 glob_out_b32 += read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
4666 }
4667 glob_out_b32 *= 32; // values are 32-byte units
4668
Willy Tarreauc05d30e2023-04-28 14:50:29 +02004669 up = now_ns - start_time_ns;
Willy Tarreau563efe62023-04-27 14:41:37 +02004670 up_sec = ns_to_sec(up);
4671 up_usec = (up / 1000U) % 1000000U;
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004672
Willy Tarreau5723b382023-05-17 09:05:20 +02004673 boot = tv_ms_remain(&start_date, &ready_date);
4674
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004675 if (len < INF_TOTAL_FIELDS)
4676 return 0;
4677
4678 chunk_reset(out);
4679 memset(info, 0, sizeof(*info) * len);
4680
4681 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004682 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004683 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004684 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004685
Yves Lafon95317282018-02-26 11:10:37 +01004686 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004687 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004688 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004689 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4690
4691 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreau563efe62023-04-27 14:41:37 +02004692 chunk_appendf(out, "%ud %uh%02um%02us", up_sec / 86400, (up_sec % 86400) / 3600, (up_sec % 3600) / 60, (up_sec % 60));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004693
Willy Tarreau563efe62023-04-27 14:41:37 +02004694 info[INF_UPTIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, up_sec + up_usec / 1000000.0) : mkf_u32(FN_DURATION, up_sec);
Willy Tarreau7222db72023-04-28 15:57:18 +02004695 info[INF_START_TIME_SEC] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_DURATION, start_date.tv_sec + start_date.tv_usec / 1000000.0) : mkf_u32(FN_DURATION, start_date.tv_sec);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004696 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004697 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004698 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004699 info[INF_POOL_ALLOC_BYTES] = mkf_u64(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004700 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004701 info[INF_POOL_USED_BYTES] = mkf_u64(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004702 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4703 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4704 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4705 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4706 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4707 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4708 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4709 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4710#ifdef USE_OPENSSL
4711 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004712 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4713 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004714#endif
4715 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4716 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4717 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004718 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 +01004719 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4720 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004721 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 +01004722 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4723 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4724
4725#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004726 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 +01004727 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4728 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004729 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 +01004730 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004731 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4732 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 +01004733 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4734 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4735 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4736#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004737 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));
4738 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 +01004739 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4740#ifdef USE_ZLIB
4741 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4742 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4743#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004744 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004745 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004746 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004747 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4748 if (global.desc)
4749 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004750 info[INF_STOPPING] = mkf_u32(0, stopping);
4751 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004752 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004753 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004754 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004755 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004756 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004757 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004758 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau6be8d092023-01-12 16:08:41 +01004759 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, glob_out_bytes);
4760 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, glob_spl_bytes);
4761 info[INF_BYTES_OUT_RATE] = mkf_u64(FN_RATE, glob_out_b32);
Willy Tarreau9b013702019-10-24 18:18:02 +02004762 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004763 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004764
4765 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
Willy Tarreau52fd8792023-05-17 08:43:15 +02004766 chunk_appendf(out, "%#x", get_tainted());
Willy Tarreau3c4a2972023-05-11 12:02:21 +02004767 info[INF_WARNINGS] = mkf_u32(FN_COUNTER, HA_ATOMIC_LOAD(&tot_warnings));
Willy Tarreau96151022023-05-11 13:51:31 +02004768 info[INF_MAXCONN_REACHED] = mkf_u32(FN_COUNTER, HA_ATOMIC_LOAD(&maxconn_reached));
Willy Tarreau5723b382023-05-17 09:05:20 +02004769 info[INF_BOOTTIME_MS] = mkf_u32(FN_DURATION, boot);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004770
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004771 return 1;
4772}
4773
Willy Tarreau4596fe22022-05-17 19:07:51 +02004774/* This function dumps information onto the stream connector's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004775 * It returns 0 as long as it does not complete, non-zero upon completion.
4776 * No state is used.
4777 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004778static int stats_dump_info_to_buffer(struct stconn *sc)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004779{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004780 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004781 struct show_stat_ctx *ctx = appctx->svcctx;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004782 int ret;
4783 int current_field;
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004784
Willy Tarreau91cefca2022-05-03 17:08:29 +02004785 if (!stats_fill_info(info, INF_TOTAL_FIELDS, ctx->flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004786 return 0;
4787
Christopher Fauleta8b76842022-12-16 15:08:36 +01004788 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004789more:
4790 current_field = ctx->field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004791
Willy Tarreau91cefca2022-05-03 17:08:29 +02004792 if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004793 ret = stats_dump_typed_info_fields(&trash_chunk, info, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004794 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004795 ret = stats_dump_json_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004796 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01004797 ret = stats_dump_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004798
Christopher Fauleta8b76842022-12-16 15:08:36 +01004799 if (applet_putchk(appctx, &trash_chunk) == -1) {
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004800 /* restore previous field */
4801 ctx->field = current_field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004802 return 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004803 }
4804 if (ret && ctx->field) {
4805 /* partial dump */
4806 goto more;
4807 }
4808 ctx->field = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004809 return 1;
4810}
4811
Willy Tarreau4596fe22022-05-17 19:07:51 +02004812/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004813 * It returns 0 as long as it does not complete, non-zero upon completion.
4814 * No state is used.
4815 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004816 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004817 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4818 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004819static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004820{
4821
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004822 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004823
4824 chunk_strcat(out,
4825 "{"
4826 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4827 "\"oneOf\":["
4828 "{"
4829 "\"title\":\"Info\","
4830 "\"type\":\"array\","
4831 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004832 "\"title\":\"InfoItem\","
4833 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004834 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004835 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4836 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4837 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4838 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4839 "},"
4840 "\"required\":[\"field\",\"processNum\",\"tags\","
4841 "\"value\"]"
4842 "}"
4843 "},"
4844 "{"
4845 "\"title\":\"Stat\","
4846 "\"type\":\"array\","
4847 "\"items\":{"
4848 "\"title\":\"InfoItem\","
4849 "\"type\":\"object\","
4850 "\"properties\":{"
4851 "\"objType\":{"
4852 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4853 "\"Server\",\"Unknown\"]"
4854 "},"
4855 "\"proxyId\":{"
4856 "\"type\":\"integer\","
4857 "\"minimum\":0"
4858 "},"
4859 "\"id\":{"
4860 "\"type\":\"integer\","
4861 "\"minimum\":0"
4862 "},"
4863 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4864 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4865 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4866 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4867 "},"
4868 "\"required\":[\"objType\",\"proxyId\",\"id\","
4869 "\"field\",\"processNum\",\"tags\","
4870 "\"value\"]"
4871 "}"
4872 "},"
4873 "{"
4874 "\"title\":\"Error\","
4875 "\"type\":\"object\","
4876 "\"properties\":{"
4877 "\"errorStr\":{"
4878 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004879 "}"
4880 "},"
4881 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004882 "}"
4883 "],"
4884 "\"definitions\":{"
4885 "\"field\":{"
4886 "\"type\":\"object\","
4887 "\"pos\":{"
4888 "\"type\":\"integer\","
4889 "\"minimum\":0"
4890 "},"
4891 "\"name\":{"
4892 "\"type\":\"string\""
4893 "},"
4894 "\"required\":[\"pos\",\"name\"]"
4895 "},"
4896 "\"processNum\":{"
4897 "\"type\":\"integer\","
4898 "\"minimum\":1"
4899 "},"
4900 "\"tags\":{"
4901 "\"type\":\"object\","
4902 "\"origin\":{"
4903 "\"type\":\"string\","
4904 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4905 "\"Config\",\"Product\",\"Unknown\"]"
4906 "},"
4907 "\"nature\":{"
4908 "\"type\":\"string\","
4909 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4910 "\"Rate\",\"Counter\",\"Duration\","
4911 "\"Age\",\"Time\",\"Name\",\"Output\","
4912 "\"Avg\", \"Unknown\"]"
4913 "},"
4914 "\"scope\":{"
4915 "\"type\":\"string\","
4916 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4917 "\"System\",\"Unknown\"]"
4918 "},"
4919 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4920 "},"
4921 "\"typedValue\":{"
4922 "\"type\":\"object\","
4923 "\"oneOf\":["
4924 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4925 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4926 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4927 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4928 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4929 "],"
4930 "\"definitions\":{"
4931 "\"s32Value\":{"
4932 "\"properties\":{"
4933 "\"type\":{"
4934 "\"type\":\"string\","
4935 "\"enum\":[\"s32\"]"
4936 "},"
4937 "\"value\":{"
4938 "\"type\":\"integer\","
4939 "\"minimum\":-2147483648,"
4940 "\"maximum\":2147483647"
4941 "}"
4942 "},"
4943 "\"required\":[\"type\",\"value\"]"
4944 "},"
4945 "\"s64Value\":{"
4946 "\"properties\":{"
4947 "\"type\":{"
4948 "\"type\":\"string\","
4949 "\"enum\":[\"s64\"]"
4950 "},"
4951 "\"value\":{"
4952 "\"type\":\"integer\","
4953 "\"minimum\":-9007199254740991,"
4954 "\"maximum\":9007199254740991"
4955 "}"
4956 "},"
4957 "\"required\":[\"type\",\"value\"]"
4958 "},"
4959 "\"u32Value\":{"
4960 "\"properties\":{"
4961 "\"type\":{"
4962 "\"type\":\"string\","
4963 "\"enum\":[\"u32\"]"
4964 "},"
4965 "\"value\":{"
4966 "\"type\":\"integer\","
4967 "\"minimum\":0,"
4968 "\"maximum\":4294967295"
4969 "}"
4970 "},"
4971 "\"required\":[\"type\",\"value\"]"
4972 "},"
4973 "\"u64Value\":{"
4974 "\"properties\":{"
4975 "\"type\":{"
4976 "\"type\":\"string\","
4977 "\"enum\":[\"u64\"]"
4978 "},"
4979 "\"value\":{"
4980 "\"type\":\"integer\","
4981 "\"minimum\":0,"
4982 "\"maximum\":9007199254740991"
4983 "}"
4984 "},"
4985 "\"required\":[\"type\",\"value\"]"
4986 "},"
4987 "\"strValue\":{"
4988 "\"properties\":{"
4989 "\"type\":{"
4990 "\"type\":\"string\","
4991 "\"enum\":[\"str\"]"
4992 "},"
4993 "\"value\":{\"type\":\"string\"}"
4994 "},"
4995 "\"required\":[\"type\",\"value\"]"
4996 "},"
4997 "\"unknownValue\":{"
4998 "\"properties\":{"
4999 "\"type\":{"
5000 "\"type\":\"integer\","
5001 "\"minimum\":0"
5002 "},"
5003 "\"value\":{"
5004 "\"type\":\"string\","
5005 "\"enum\":[\"unknown\"]"
5006 "}"
5007 "},"
5008 "\"required\":[\"type\",\"value\"]"
5009 "}"
5010 "}"
5011 "}"
5012 "}"
5013 "}");
5014
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005015 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01005016 chunk_reset(out);
5017 chunk_appendf(out,
5018 "{\"errorStr\":\"output buffer too short\"}");
5019 }
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02005020 chunk_appendf(out, "\n");
Simon Horman6f6bb382017-01-04 09:37:26 +01005021}
5022
Willy Tarreau4596fe22022-05-17 19:07:51 +02005023/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01005024 * It returns 0 as long as it does not complete, non-zero upon completion.
5025 * No state is used.
5026 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005027static int stats_dump_json_schema_to_buffer(struct appctx *appctx)
Simon Horman6f6bb382017-01-04 09:37:26 +01005028{
Simon Horman6f6bb382017-01-04 09:37:26 +01005029
Christopher Fauleta8b76842022-12-16 15:08:36 +01005030 chunk_reset(&trash_chunk);
Simon Horman6f6bb382017-01-04 09:37:26 +01005031
Christopher Fauleta8b76842022-12-16 15:08:36 +01005032 stats_dump_json_schema(&trash_chunk);
5033
5034 if (applet_putchk(appctx, &trash_chunk) == -1)
Simon Horman6f6bb382017-01-04 09:37:26 +01005035 return 0;
Simon Horman6f6bb382017-01-04 09:37:26 +01005036
5037 return 1;
5038}
5039
Amaury Denoyellef618dbc2024-02-22 14:13:45 +01005040static void http_stats_release(struct appctx *appctx)
5041{
5042 struct show_stat_ctx *ctx = appctx->svcctx;
5043
5044 if (ctx->px_st == STAT_PX_ST_SV)
5045 srv_drop(ctx->obj2);
5046}
5047
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005048static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01005049{
5050 struct proxy *px;
5051 struct server *sv;
5052 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005053 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005054 int clrall = 0;
5055
5056 if (strcmp(args[2], "all") == 0)
5057 clrall = 1;
5058
5059 /* check permissions */
5060 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
5061 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
5062 return 1;
5063
Olivier Houchardfbc74e82017-11-24 16:54:05 +01005064 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01005065 if (clrall) {
5066 memset(&px->be_counters, 0, sizeof(px->be_counters));
5067 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
5068 }
5069 else {
5070 px->be_counters.conn_max = 0;
5071 px->be_counters.p.http.rps_max = 0;
5072 px->be_counters.sps_max = 0;
5073 px->be_counters.cps_max = 0;
5074 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005075 px->be_counters.qtime_max = 0;
5076 px->be_counters.ctime_max = 0;
5077 px->be_counters.dtime_max = 0;
5078 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005079
5080 px->fe_counters.conn_max = 0;
5081 px->fe_counters.p.http.rps_max = 0;
5082 px->fe_counters.sps_max = 0;
5083 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005084 }
5085
5086 for (sv = px->srv; sv; sv = sv->next)
5087 if (clrall)
5088 memset(&sv->counters, 0, sizeof(sv->counters));
5089 else {
5090 sv->counters.cur_sess_max = 0;
5091 sv->counters.nbpend_max = 0;
5092 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005093 sv->counters.qtime_max = 0;
5094 sv->counters.ctime_max = 0;
5095 sv->counters.dtime_max = 0;
5096 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005097 }
5098
5099 list_for_each_entry(li, &px->conf.listeners, by_fe)
5100 if (li->counters) {
5101 if (clrall)
5102 memset(li->counters, 0, sizeof(*li->counters));
5103 else
5104 li->counters->conn_max = 0;
5105 }
5106 }
5107
5108 global.cps_max = 0;
5109 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02005110 global.ssl_max = 0;
5111 global.ssl_fe_keys_max = 0;
5112 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005113
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005114 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5115 if (!mod->clearable && !clrall)
5116 continue;
5117
5118 for (px = proxies_list; px; px = px->next) {
5119 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
5120
5121 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
5122 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
5123 mod,
5124 mod->counters,
5125 mod->counters_size);
5126 }
5127
5128 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
5129 EXTRA_COUNTERS_INIT(px->extra_counters_be,
5130 mod,
5131 mod->counters,
5132 mod->counters_size);
5133 }
5134
5135 if (mod_cap & STATS_PX_CAP_SRV) {
5136 for (sv = px->srv; sv; sv = sv->next) {
5137 EXTRA_COUNTERS_INIT(sv->extra_counters,
5138 mod,
5139 mod->counters,
5140 mod->counters_size);
5141 }
5142 }
5143
5144 if (mod_cap & STATS_PX_CAP_LI) {
5145 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5146 EXTRA_COUNTERS_INIT(li->extra_counters,
5147 mod,
5148 mod->counters,
5149 mod->counters_size);
5150 }
5151 }
5152 }
5153 }
5154
Emeric Brunf8642ee2021-10-29 17:59:18 +02005155 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005156
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005157 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01005158 return 1;
5159}
5160
5161
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005162static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005163{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005164 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005165 int arg = 2;
5166
Willy Tarreau91cefca2022-05-03 17:08:29 +02005167 ctx->scope_str = 0;
5168 ctx->scope_len = 0;
5169 ctx->flags = 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01005170 ctx->field = 0; /* explicit default value */
Willy Tarreaud25fc792016-12-16 12:33:47 +01005171
Willy Tarreau2f397382019-10-09 11:43:59 +02005172 while (*args[arg]) {
5173 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005174 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005175 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005176 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005177 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005178 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02005179 else if (strcmp(args[arg], "float") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005180 ctx->flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02005181 arg++;
5182 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005183 return 0;
5184}
5185
5186
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005187static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01005188{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005189 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005190 int arg = 2;
5191
Willy Tarreau91cefca2022-05-03 17:08:29 +02005192 ctx->scope_str = 0;
5193 ctx->scope_len = 0;
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01005194 ctx->http_px = NULL; // not under http context
Willy Tarreau91cefca2022-05-03 17:08:29 +02005195 ctx->flags = STAT_SHNODE | STAT_SHDESC;
Willy Tarreau578d6e42019-10-09 11:00:22 +02005196
Willy Tarreau0698c802022-05-11 14:09:57 +02005197 if ((strm_li(appctx_strm(appctx))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005198 ctx->flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01005199
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005200 /* proxy is the default domain */
Willy Tarreau91cefca2022-05-03 17:08:29 +02005201 ctx->domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005202 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005203 ++args;
5204
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005205 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005206 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005207 } else if (strcmp(args[arg], "resolvers") == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02005208 ctx->domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005209 ++args;
5210 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005211 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005212 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005213 }
5214
Willy Tarreau91cefca2022-05-03 17:08:29 +02005215 if (ctx->domain == STATS_DOMAIN_PROXY
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005216 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005217 struct proxy *px;
5218
Willy Tarreau2f397382019-10-09 11:43:59 +02005219 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005220 if (px)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005221 ctx->iid = px->uuid;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005222 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02005223 ctx->iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005224
Willy Tarreau91cefca2022-05-03 17:08:29 +02005225 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02005226 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005227
Willy Tarreau91cefca2022-05-03 17:08:29 +02005228 ctx->flags |= STAT_BOUND;
5229 ctx->type = atoi(args[arg+1]);
5230 ctx->sid = atoi(args[arg+2]);
Willy Tarreau2f397382019-10-09 11:43:59 +02005231 arg += 3;
5232 }
5233
5234 while (*args[arg]) {
5235 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005236 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005237 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005238 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005239 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005240 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02005241 else if (strcmp(args[arg], "no-maint") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005242 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02005243 else if (strcmp(args[arg], "up") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005244 ctx->flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02005245 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01005246 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01005247
Willy Tarreau2b812e22016-11-22 16:18:05 +01005248 return 0;
5249}
5250
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005251static int cli_io_handler_dump_info(struct appctx *appctx)
5252{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005253 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005254 return stats_dump_info_to_buffer(appctx_sc(appctx));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005255}
5256
Willy Tarreau4596fe22022-05-17 19:07:51 +02005257/* This I/O handler runs as an applet embedded in a stream connector. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01005258 * used to send raw stats over a socket.
5259 */
5260static int cli_io_handler_dump_stat(struct appctx *appctx)
5261{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005262 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Aurelien DARRAGON40f88022023-12-05 19:49:13 +01005263 return stats_dump_stat_to_buffer(appctx_sc(appctx), NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01005264}
5265
Amaury Denoyellef618dbc2024-02-22 14:13:45 +01005266static void cli_io_handler_release_stat(struct appctx *appctx)
5267{
5268 struct show_stat_ctx *ctx = appctx->svcctx;
5269
5270 if (ctx->px_st == STAT_PX_ST_SV)
5271 srv_drop(ctx->obj2);
5272}
5273
Simon Horman6f6bb382017-01-04 09:37:26 +01005274static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5275{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005276 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005277 return stats_dump_json_schema_to_buffer(appctx);
Simon Horman6f6bb382017-01-04 09:37:26 +01005278}
5279
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005280int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5281 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005282{
5283 struct stats_module *mod;
5284
5285 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5286
5287 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5288 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5289 continue;
5290
5291 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5292 }
5293
5294 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5295
5296 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5297 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5298 continue;
5299
5300 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5301 }
5302
5303 return 1;
5304
5305 alloc_failed:
5306 return 0;
5307}
5308
5309/* Initialize and allocate all extra counters for a proxy and its attached
5310 * servers/listeners with all already registered stats module
5311 */
5312int stats_allocate_proxy_counters(struct proxy *px)
5313{
5314 struct server *sv;
5315 struct listener *li;
5316
5317 if (px->cap & PR_CAP_FE) {
5318 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5319 COUNTERS_FE,
5320 STATS_PX_CAP_FE)) {
5321 return 0;
5322 }
5323 }
5324
5325 if (px->cap & PR_CAP_BE) {
5326 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5327 COUNTERS_BE,
5328 STATS_PX_CAP_BE)) {
5329 return 0;
5330 }
5331 }
5332
5333 for (sv = px->srv; sv; sv = sv->next) {
5334 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5335 COUNTERS_SV,
5336 STATS_PX_CAP_SRV)) {
5337 return 0;
5338 }
5339 }
5340
5341 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5342 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5343 COUNTERS_LI,
5344 STATS_PX_CAP_LI)) {
5345 return 0;
5346 }
5347 }
5348
5349 return 1;
5350}
5351
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005352void stats_register_module(struct stats_module *m)
5353{
5354 const uint8_t domain = stats_get_domain(m->domain_flags);
5355
Willy Tarreau2b718102021-04-21 07:32:39 +02005356 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005357 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005358}
5359
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005360static int allocate_stats_px_postcheck(void)
5361{
5362 struct stats_module *mod;
5363 size_t i = ST_F_TOTAL_FIELDS;
5364 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005365 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005366
5367 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5368
5369 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5370 if (!stat_f[STATS_DOMAIN_PROXY]) {
5371 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5372 err_code |= ERR_ALERT | ERR_FATAL;
5373 return err_code;
5374 }
5375
5376 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5377 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5378
5379 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5380 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5381 mod->stats,
5382 mod->stats_count * sizeof(struct name_desc));
5383 i += mod->stats_count;
5384 }
5385
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005386 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005387 if (!stats_allocate_proxy_counters(px)) {
5388 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5389 err_code |= ERR_ALERT | ERR_FATAL;
5390 return err_code;
5391 }
5392 }
5393
Christopher Fauletde79cd22021-01-06 07:41:56 +01005394 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005395
5396 return err_code;
5397}
5398
5399REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5400
Emeric Brunf8642ee2021-10-29 17:59:18 +02005401static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005402{
5403 struct stats_module *mod;
5404 size_t i = 0;
5405 int err_code = 0;
5406
Emeric Brunf8642ee2021-10-29 17:59:18 +02005407 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5408 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5409 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005410 err_code |= ERR_ALERT | ERR_FATAL;
5411 return err_code;
5412 }
5413
Emeric Brunf8642ee2021-10-29 17:59:18 +02005414 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5415 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005416 mod->stats,
5417 mod->stats_count * sizeof(struct name_desc));
5418 i += mod->stats_count;
5419 }
5420
Emeric Brunf8642ee2021-10-29 17:59:18 +02005421 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5422 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005423 err_code |= ERR_ALERT | ERR_FATAL;
5424 return err_code;
5425 }
5426
Christopher Fauletde79cd22021-01-06 07:41:56 +01005427 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005428
5429 return err_code;
5430}
5431
Emeric Brunf8642ee2021-10-29 17:59:18 +02005432REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005433
Christopher Fauletde79cd22021-01-06 07:41:56 +01005434static int allocate_stat_lines_per_thread(void)
5435{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005436 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005437
5438 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5439 const int domain = domains[i];
5440
5441 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5442 if (!stat_l[domain])
5443 return 0;
5444 }
5445 return 1;
5446}
5447
5448REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5449
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005450static int allocate_trash_counters(void)
5451{
5452 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005453 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005454 size_t max_counters_size = 0;
5455
5456 /* calculate the greatest counters used by any stats modules */
5457 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5458 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5459 max_counters_size = mod->counters_size > max_counters_size ?
5460 mod->counters_size : max_counters_size;
5461 }
5462 }
5463
5464 /* allocate the trash with the size of the greatest counters */
5465 if (max_counters_size) {
5466 trash_counters = malloc(max_counters_size);
5467 if (!trash_counters) {
5468 ha_alert("stats: cannot allocate trash counters for statistics\n");
5469 return 0;
5470 }
5471 }
5472
5473 return 1;
5474}
5475
5476REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5477
Christopher Fauletde79cd22021-01-06 07:41:56 +01005478static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005479{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005480 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005481
5482 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5483 const int domain = domains[i];
5484
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005485 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005486 }
5487}
5488
5489
5490REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5491
5492static void deinit_stats(void)
5493{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005494 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005495
5496 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5497 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005498
5499 if (stat_f[domain])
5500 free(stat_f[domain]);
5501 }
5502}
5503
5504REGISTER_POST_DEINIT(deinit_stats);
5505
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005506static void free_trash_counters(void)
5507{
5508 if (trash_counters)
5509 free(trash_counters);
5510}
5511
5512REGISTER_PER_THREAD_FREE(free_trash_counters);
5513
Willy Tarreau2b812e22016-11-22 16:18:05 +01005514/* register cli keywords */
5515static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005516 { { "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 +02005517 { { "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 },
Amaury Denoyellef618dbc2024-02-22 14:13:45 +01005518 { { "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, cli_io_handler_release_stat },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005519 { { "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 +01005520 {{},}
5521}};
5522
Willy Tarreau0108d902018-11-25 19:14:37 +01005523INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5524
William Lallemand74c24fb2016-11-21 17:18:36 +01005525struct applet http_stats_applet = {
5526 .obj_type = OBJ_TYPE_APPLET,
5527 .name = "<STATS>", /* used for logging */
5528 .fct = http_stats_io_handler,
Amaury Denoyellef618dbc2024-02-22 14:13:45 +01005529 .release = http_stats_release,
William Lallemand74c24fb2016-11-21 17:18:36 +01005530};
5531
William Lallemand74c24fb2016-11-21 17:18:36 +01005532/*
5533 * Local variables:
5534 * c-indent-level: 8
5535 * c-basic-offset: 8
5536 * End:
5537 */