blob: c06fce3d30ccfe60b0c8d03245df3e06b8e63977 [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 Tarreau0baac8c2016-11-22 16:36:53 +0100161};
162
Willy Tarreaueaa55372019-10-09 07:39:11 +0200163const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200164 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
165 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100166 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
167 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100168 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
169 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200170 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
171 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
172 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
173 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
174 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
175 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
176 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
177 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
178 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
179 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
180 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
181 [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 +0200182 [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 +0200183 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
184 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
185 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
186 [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" },
187 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
188 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
189 [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 +0200190 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200191 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
192 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
193 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
194 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
195 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
196 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
197 [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)" },
198 [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 +0100199 [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 +0200200 [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" },
201 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
202 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
203 [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" },
204 [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" },
205 [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" },
206 [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" },
207 [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" },
208 [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)" },
209 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
210 [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 +0100211 [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 +0200212 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
213 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
214 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
215 [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" },
216 [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" },
217 [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)" },
218 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
219 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
220 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
221 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
222 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
223 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
224 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
225 [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)" },
226 [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" },
227 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
228 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
229 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
230 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
231 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
232 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
233 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
234 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
235 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
236 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
237 [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" },
238 [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" },
239 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
240 [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" },
241 [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 +0100242 [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 +0200243 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
244 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
245 [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" },
246 [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" },
247 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
248 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
249 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
250 [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" },
251 [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" },
252 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
253 [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 +0100254 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
255 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
256 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
257 [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 +0100258 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200259 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
260 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
261 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200262 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200263 [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 +0000264 [ST_F_AGG_SRV_CHECK_STATUS] = { .name = "agg_server_check_status", .desc = "[DEPRECATED] Backend's aggregated gauge of servers' status" },
265 [ST_F_AGG_SRV_STATUS ] = { .name = "agg_server_status", .desc = "Backend's aggregated gauge of servers' status" },
Cedric Paillete06e31e2022-12-08 09:17:01 +0000266 [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 +0100267 [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 +0100268 [ST_F_SESS_OTHER] = { .name = "sess_other", .desc = "Total number of sessions other than HTTP since process started" },
269 [ST_F_H1SESS] = { .name = "h1sess", .desc = "Total number of HTTP/1 sessions since process started" },
270 [ST_F_H2SESS] = { .name = "h2sess", .desc = "Total number of HTTP/2 sessions since process started" },
271 [ST_F_H3SESS] = { .name = "h3sess", .desc = "Total number of HTTP/3 sessions since process started" },
272 [ST_F_REQ_OTHER] = { .name = "req_other", .desc = "Total number of sessions other than HTTP processed by this object since the worker process started" },
273 [ST_F_H1REQ] = { .name = "h1req", .desc = "Total number of HTTP/1 sessions processed by this object since the worker process started" },
274 [ST_F_H2REQ] = { .name = "h2req", .desc = "Total number of hTTP/2 sessions processed by this object since the worker process started" },
275 [ST_F_H3REQ] = { .name = "h3req", .desc = "Total number of HTTP/3 sessions processed by this object since the worker process started" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100276};
277
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100278/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100279THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200280
281/* description of statistics (static and dynamic) */
282static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
283static size_t stat_count[STATS_DOMAIN_COUNT];
284
285/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100286THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100287
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200288/* list of all registered stats module */
289static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
290 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Emeric Brunf8642ee2021-10-29 17:59:18 +0200291 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_RESOLVERS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200292};
293
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100294THREAD_LOCAL void *trash_counters;
Christopher Fauleta8b76842022-12-16 15:08:36 +0100295static THREAD_LOCAL struct buffer trash_chunk = BUF_NULL;
296
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100297
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200298static inline uint8_t stats_get_domain(uint32_t domain)
299{
300 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
301}
302
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200303static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
304{
305 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
306}
307
Christopher Faulet6338a082019-09-09 15:50:54 +0200308static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100309
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200310int stats_putchk(struct appctx *appctx, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +0100311{
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200312 struct stconn *sc = appctx_sc(appctx);
313 struct channel *chn = sc_ic(sc);
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +0100314 struct buffer *chk = &trash_chunk;
315
Christopher Fauletef779222018-10-31 08:47:01 +0100316 if (htx) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200317 if (chk->data >= channel_htx_recv_max(chn, htx)) {
318 sc_need_room(sc, chk->data);
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100319 return 0;
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200320 }
321 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data))) {
322 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +0100323 return 0;
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200324 }
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100325 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100326 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100327 }
328 else {
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200329 if (applet_putchk(appctx, chk) == -1)
Christopher Fauletef779222018-10-31 08:47:01 +0100330 return 0;
331 }
332 return 1;
333}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100334
Willy Tarreaucaff6312022-05-27 10:17:46 +0200335static const char *stats_scope_ptr(struct appctx *appctx, struct stconn *sc)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100336{
Willy Tarreau91cefca2022-05-03 17:08:29 +0200337 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +0200338 struct channel *req = sc_oc(sc);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200339 struct htx *htx = htxbuf(&req->buf);
340 struct htx_blk *blk;
341 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100342
Christopher Fauletb7f88902019-07-15 21:56:43 +0200343 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100344 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200345 ALREADY_CHECKED(blk);
346 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Willy Tarreau91cefca2022-05-03 17:08:29 +0200347 return uri.ptr + ctx->scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100348}
349
William Lallemand74c24fb2016-11-21 17:18:36 +0100350/*
351 * http_stats_io_handler()
352 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
353 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100354 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100355 * -> stats_dump_html_head() // emits the HTML headers
356 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
357 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
358 * -> stats_dump_html_px_hdr()
359 * -> stats_dump_fe_stats()
360 * -> stats_dump_li_stats()
361 * -> stats_dump_sv_stats()
362 * -> stats_dump_be_stats()
363 * -> stats_dump_html_px_end()
364 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100365 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100366 */
367
368
Christopher Fauleta8b76842022-12-16 15:08:36 +0100369/* Dumps the stats CSV header to the local trash buffer. The caller is
370 * responsible for clearing it if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +0100371 * NOTE: Some tools happen to rely on the field position instead of its name,
372 * so please only append new fields at the end, never in the middle.
373 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200374static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100375{
376 int field;
377
Christopher Fauleta8b76842022-12-16 15:08:36 +0100378 chunk_appendf(&trash_chunk, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200379 if (stat_f[domain]) {
380 for (field = 0; field < stat_count[domain]; ++field) {
Christopher Fauleta8b76842022-12-16 15:08:36 +0100381 chunk_appendf(&trash_chunk, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100382
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200383 /* print special delimiter on proxy stats to mark end of
384 static fields */
385 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
Christopher Fauleta8b76842022-12-16 15:08:36 +0100386 chunk_appendf(&trash_chunk, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200387 }
388 }
389
Christopher Fauleta8b76842022-12-16 15:08:36 +0100390 chunk_appendf(&trash_chunk, "\n");
William Lallemand74c24fb2016-11-21 17:18:36 +0100391}
392
William Lallemand74c24fb2016-11-21 17:18:36 +0100393/* Emits a stats field without any surrounding element and properly encoded to
394 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
395 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200396int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100397{
398 switch (field_format(f, 0)) {
399 case FF_EMPTY: return 1;
400 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
401 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
402 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
403 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200404 case FF_FLT: {
405 size_t prev_data = out->data;
406 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
407 return out->data;
408 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100409 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
410 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
411 }
412}
413
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200414const char *field_to_html_str(const struct field *f)
415{
416 switch (field_format(f, 0)) {
417 case FF_S32: return U2H(f->u.s32);
418 case FF_S64: return U2H(f->u.s64);
419 case FF_U64: return U2H(f->u.u64);
420 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200421 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200422 case FF_STR: return field_str(f, 0);
423 case FF_EMPTY:
424 default:
425 return "";
426 }
427}
428
William Lallemand74c24fb2016-11-21 17:18:36 +0100429/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
430 * output is supposed to be used on its own line. Returns non-zero on success, 0
431 * if the buffer is full.
432 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200433int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100434{
435 switch (field_format(f, 0)) {
436 case FF_EMPTY: return 1;
437 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
438 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
439 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
440 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200441 case FF_FLT: {
442 size_t prev_data = out->data;
443 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
444 return out->data;
445 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100446 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
447 default: return chunk_appendf(out, "%08x:?", f->type);
448 }
449}
450
Simon Horman05ee2132017-01-04 09:37:25 +0100451/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
452 * the recommendation for interoperable integers in section 6 of RFC 7159.
453 */
454#define JSON_INT_MAX ((1ULL << 53) - 1)
455#define JSON_INT_MIN (0 - JSON_INT_MAX)
456
457/* Emits a stats field value and its type in JSON.
458 * Returns non-zero on success, 0 on error.
459 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200460int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100461{
462 int old_len;
463 char buf[20];
464 const char *type, *value = buf, *quote = "";
465
466 switch (field_format(f, 0)) {
467 case FF_EMPTY: return 1;
468 case FF_S32: type = "\"s32\"";
469 snprintf(buf, sizeof(buf), "%d", f->u.s32);
470 break;
471 case FF_U32: type = "\"u32\"";
472 snprintf(buf, sizeof(buf), "%u", f->u.u32);
473 break;
474 case FF_S64: type = "\"s64\"";
475 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
476 return 0;
477 type = "\"s64\"";
478 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
479 break;
480 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
481 return 0;
482 type = "\"u64\"";
483 snprintf(buf, sizeof(buf), "%llu",
484 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200485 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200486 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200487 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100488 break;
489 case FF_STR: type = "\"str\"";
490 value = field_str(f, 0);
491 quote = "\"";
492 break;
493 default: snprintf(buf, sizeof(buf), "%u", f->type);
494 type = buf;
495 value = "unknown";
496 quote = "\"";
497 break;
498 }
499
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100501 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
502 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100504}
505
William Lallemand74c24fb2016-11-21 17:18:36 +0100506/* Emits an encoding of the field type on 3 characters followed by a delimiter.
507 * Returns non-zero on success, 0 if the buffer is full.
508 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200509int stats_emit_field_tags(struct buffer *out, const struct field *f,
510 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100511{
512 char origin, nature, scope;
513
514 switch (field_origin(f, 0)) {
515 case FO_METRIC: origin = 'M'; break;
516 case FO_STATUS: origin = 'S'; break;
517 case FO_KEY: origin = 'K'; break;
518 case FO_CONFIG: origin = 'C'; break;
519 case FO_PRODUCT: origin = 'P'; break;
520 default: origin = '?'; break;
521 }
522
523 switch (field_nature(f, 0)) {
524 case FN_GAUGE: nature = 'G'; break;
525 case FN_LIMIT: nature = 'L'; break;
526 case FN_MIN: nature = 'm'; break;
527 case FN_MAX: nature = 'M'; break;
528 case FN_RATE: nature = 'R'; break;
529 case FN_COUNTER: nature = 'C'; break;
530 case FN_DURATION: nature = 'D'; break;
531 case FN_AGE: nature = 'A'; break;
532 case FN_TIME: nature = 'T'; break;
533 case FN_NAME: nature = 'N'; break;
534 case FN_OUTPUT: nature = 'O'; break;
535 case FN_AVG: nature = 'a'; break;
536 default: nature = '?'; break;
537 }
538
539 switch (field_scope(f, 0)) {
540 case FS_PROCESS: scope = 'P'; break;
541 case FS_SERVICE: scope = 'S'; break;
542 case FS_SYSTEM: scope = 's'; break;
543 case FS_CLUSTER: scope = 'C'; break;
544 default: scope = '?'; break;
545 }
546
547 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
548}
549
Simon Horman05ee2132017-01-04 09:37:25 +0100550/* Emits an encoding of the field type as JSON.
551 * Returns non-zero on success, 0 if the buffer is full.
552 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200553int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100554{
555 const char *origin, *nature, *scope;
556 int old_len;
557
558 switch (field_origin(f, 0)) {
559 case FO_METRIC: origin = "Metric"; break;
560 case FO_STATUS: origin = "Status"; break;
561 case FO_KEY: origin = "Key"; break;
562 case FO_CONFIG: origin = "Config"; break;
563 case FO_PRODUCT: origin = "Product"; break;
564 default: origin = "Unknown"; break;
565 }
566
567 switch (field_nature(f, 0)) {
568 case FN_GAUGE: nature = "Gauge"; break;
569 case FN_LIMIT: nature = "Limit"; break;
570 case FN_MIN: nature = "Min"; break;
571 case FN_MAX: nature = "Max"; break;
572 case FN_RATE: nature = "Rate"; break;
573 case FN_COUNTER: nature = "Counter"; break;
574 case FN_DURATION: nature = "Duration"; break;
575 case FN_AGE: nature = "Age"; break;
576 case FN_TIME: nature = "Time"; break;
577 case FN_NAME: nature = "Name"; break;
578 case FN_OUTPUT: nature = "Output"; break;
579 case FN_AVG: nature = "Avg"; break;
580 default: nature = "Unknown"; break;
581 }
582
583 switch (field_scope(f, 0)) {
584 case FS_PROCESS: scope = "Process"; break;
585 case FS_SERVICE: scope = "Service"; break;
586 case FS_SYSTEM: scope = "System"; break;
587 case FS_CLUSTER: scope = "Cluster"; break;
588 default: scope = "Unknown"; break;
589 }
590
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200591 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100592 chunk_appendf(out, "\"tags\":{"
593 "\"origin\":\"%s\","
594 "\"nature\":\"%s\","
595 "\"scope\":\"%s\""
596 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200597 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100598}
William Lallemand74c24fb2016-11-21 17:18:36 +0100599
600/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200601static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200602 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100603 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100604{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100605 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100606 int field;
607
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200608 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100609 if (!stats_emit_raw_data_field(out, &stats[field]))
610 return 0;
611 if (!chunk_strcat(out, ","))
612 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200613
614 /* print special delimiter on proxy stats to mark end of
615 static fields */
616 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
617 if (!chunk_strcat(out, "-,"))
618 return 0;
619 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100620 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200621
William Lallemand74c24fb2016-11-21 17:18:36 +0100622 chunk_strcat(out, "\n");
623 return 1;
624}
625
626/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200627static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200628 const struct field *stats,
629 size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100630 struct show_stat_ctx * ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100631{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100632 int flags = ctx->flags;
633 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100634 int field;
635
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200636 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100637 if (!stats[field].type)
638 continue;
639
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200640 switch (domain) {
641 case STATS_DOMAIN_PROXY:
642 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
643 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
644 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
645 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
646 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
647 '?',
648 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
649 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200650 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200651 stats[ST_F_PID].u.u32);
652 break;
653
Emeric Brunf8642ee2021-10-29 17:59:18 +0200654 case STATS_DOMAIN_RESOLVERS:
655 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200656 stat_f[domain][field].name);
657 break;
658
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200659 default:
660 break;
661 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100662
663 if (!stats_emit_field_tags(out, &stats[field], ':'))
664 return 0;
665 if (!stats_emit_typed_data_field(out, &stats[field]))
666 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200667
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200668 if (flags & STAT_SHOW_FDESC &&
669 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200670 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200671 }
672
William Lallemand74c24fb2016-11-21 17:18:36 +0100673 if (!chunk_strcat(out, "\n"))
674 return 0;
675 }
676 return 1;
677}
678
Simon Horman05ee2132017-01-04 09:37:25 +0100679/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200680static int stats_dump_json_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100681 const struct field *info,
682 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100683{
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100684 int started = (ctx->field) ? 1 : 0;
685 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100686
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100687 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100688 return 0;
689
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100690 for (; ctx->field < INF_TOTAL_FIELDS; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100691 int old_len;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100692 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100693
694 if (!field_format(info, field))
695 continue;
696
697 if (started && !chunk_strcat(out, ","))
698 goto err;
699 started = 1;
700
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200701 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100702 chunk_appendf(out,
703 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
704 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200705 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100706 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200707 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100708 goto err;
709
710 if (!stats_emit_json_field_tags(out, &info[field]))
711 goto err;
712
713 if (!stats_emit_json_data_field(out, &info[field]))
714 goto err;
715
716 if (!chunk_strcat(out, "}"))
717 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100718 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100719 }
720
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200721 if (!chunk_strcat(out, "]\n"))
Simon Horman05ee2132017-01-04 09:37:25 +0100722 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100723 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100724 return 1;
725
726err:
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100727 if (!ready_data) {
728 /* not enough buffer space for a single entry.. */
729 chunk_reset(out);
730 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
731 return 0; /* hard error */
732 }
733 /* push ready data and wait for a new buffer to complete the dump */
734 out->data = ready_data;
735 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100736}
737
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200738static void stats_print_proxy_field_json(struct buffer *out,
739 const struct field *stat,
740 const char *name,
741 int pos,
742 uint32_t field_type,
743 uint32_t iid,
744 uint32_t sid,
745 uint32_t pid)
746{
747 const char *obj_type;
748 switch (field_type) {
749 case STATS_TYPE_FE: obj_type = "Frontend"; break;
750 case STATS_TYPE_BE: obj_type = "Backend"; break;
751 case STATS_TYPE_SO: obj_type = "Listener"; break;
752 case STATS_TYPE_SV: obj_type = "Server"; break;
753 default: obj_type = "Unknown"; break;
754 }
755
756 chunk_appendf(out,
757 "{"
758 "\"objType\":\"%s\","
759 "\"proxyId\":%u,"
760 "\"id\":%u,"
761 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
762 "\"processNum\":%u,",
763 obj_type, iid, sid, pos, name, pid);
764}
765
Emeric Brunf8642ee2021-10-29 17:59:18 +0200766static void stats_print_rslv_field_json(struct buffer *out,
767 const struct field *stat,
768 const char *name,
769 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200770{
771 chunk_appendf(out,
772 "{"
773 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
774 pos, name);
775}
776
777
Simon Horman05ee2132017-01-04 09:37:25 +0100778/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200779static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200780 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100781 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100782{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100783 int flags = ctx->flags;
784 int domain = ctx->domain;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100785 int started = (ctx->field) ? 1 : 0;
786 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100787
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100788 if (!started && (flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100789 return 0;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100790 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100791 return 0;
792
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100793 for (; ctx->field < stats_count; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100794 int old_len;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100795 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100796
797 if (!stats[field].type)
798 continue;
799
800 if (started && !chunk_strcat(out, ","))
801 goto err;
802 started = 1;
803
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200804 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200805 if (domain == STATS_DOMAIN_PROXY) {
806 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200807 stat_f[domain][field].name,
808 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200809 stats[ST_F_TYPE].u.u32,
810 stats[ST_F_IID].u.u32,
811 stats[ST_F_SID].u.u32,
812 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200813 } else if (domain == STATS_DOMAIN_RESOLVERS) {
814 stats_print_rslv_field_json(out, &stats[field],
815 stat_f[domain][field].name,
816 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200817 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200818
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200819 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100820 goto err;
821
822 if (!stats_emit_json_field_tags(out, &stats[field]))
823 goto err;
824
825 if (!stats_emit_json_data_field(out, &stats[field]))
826 goto err;
827
828 if (!chunk_strcat(out, "}"))
829 goto err;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100830 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100831 }
832
833 if (!chunk_strcat(out, "]"))
834 goto err;
835
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100836 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100837 return 1;
838
839err:
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100840 if (!ready_data) {
841 /* not enough buffer space for a single entry.. */
842 chunk_reset(out);
843 if (ctx->flags & STAT_STARTED)
844 chunk_strcat(out, ",");
845 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
846 return 0; /* hard error */
847 }
848 /* push ready data and wait for a new buffer to complete the dump */
849 out->data = ready_data;
850 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100851}
852
William Lallemand74c24fb2016-11-21 17:18:36 +0100853/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200854 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200855 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
856 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
857 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100858 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200859static int stats_dump_fields_html(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100860 const struct field *stats,
861 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100862{
Willy Tarreau83061a82018-07-13 11:56:34 +0200863 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200864 struct stats_module *mod;
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100865 int flags = ctx->flags;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200866 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100867
868 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
869 chunk_appendf(out,
870 /* name, queue */
871 "<tr class=\"frontend\">");
872
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200873 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100874 /* Column sub-heading for Enable or Disable server */
875 chunk_appendf(out, "<td></td>");
876 }
877
878 chunk_appendf(out,
879 "<td class=ac>"
880 "<a name=\"%s/Frontend\"></a>"
881 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
882 "<td colspan=3></td>"
883 "",
884 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
885
886 chunk_appendf(out,
887 /* sessions rate : current */
888 "<td><u>%s<div class=tips><table class=det>"
889 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
890 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
891 "",
892 U2H(stats[ST_F_RATE].u.u32),
893 U2H(stats[ST_F_CONN_RATE].u.u32),
894 U2H(stats[ST_F_RATE].u.u32));
895
896 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
897 chunk_appendf(out,
898 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
899 U2H(stats[ST_F_REQ_RATE].u.u32));
900
901 chunk_appendf(out,
902 "</table></div></u></td>"
903 /* sessions rate : max */
904 "<td><u>%s<div class=tips><table class=det>"
905 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
906 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
907 "",
908 U2H(stats[ST_F_RATE_MAX].u.u32),
909 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
910 U2H(stats[ST_F_RATE_MAX].u.u32));
911
912 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
913 chunk_appendf(out,
914 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
915 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
916
917 chunk_appendf(out,
918 "</table></div></u></td>"
919 /* sessions rate : limit */
920 "<td>%s</td>",
921 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
922
923 chunk_appendf(out,
924 /* sessions: current, max, limit, total */
925 "<td>%s</td><td>%s</td><td>%s</td>"
926 "<td><u>%s<div class=tips><table class=det>"
927 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
928 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
929 "",
930 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
931 U2H(stats[ST_F_STOT].u.u64),
932 U2H(stats[ST_F_CONN_TOT].u.u64),
933 U2H(stats[ST_F_STOT].u.u64));
934
935 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
936 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
937 chunk_appendf(out,
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100938 "<tr><th>- HTTP/1 sessions:</th><td>%s</td></tr>"
939 "<tr><th>- HTTP/2 sessions:</th><td>%s</td></tr>"
940 "<tr><th>- HTTP/3 sessions:</th><td>%s</td></tr>"
941 "<tr><th>- other sessions:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100942 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100943 "<tr><th>- HTTP/1 requests:</th><td>%s</td></tr>"
944 "<tr><th>- HTTP/2 requests:</th><td>%s</td></tr>"
945 "<tr><th>- HTTP/3 requests:</th><td>%s</td></tr>"
946 "<tr><th>- other requests:</th><td>%s</td></tr>"
947 "",
948 U2H(stats[ST_F_H1SESS].u.u64),
949 U2H(stats[ST_F_H2SESS].u.u64),
950 U2H(stats[ST_F_H3SESS].u.u64),
951 U2H(stats[ST_F_SESS_OTHER].u.u64),
952 U2H(stats[ST_F_REQ_TOT].u.u64),
953 U2H(stats[ST_F_H1REQ].u.u64),
954 U2H(stats[ST_F_H2REQ].u.u64),
955 U2H(stats[ST_F_H3REQ].u.u64),
956 U2H(stats[ST_F_REQ_OTHER].u.u64));
957
958 chunk_appendf(out,
William Lallemand74c24fb2016-11-21 17:18:36 +0100959 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
960 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
961 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
962 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
963 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
964 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
965 "<tr><th>- other responses:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100966 "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100967 U2H(stats[ST_F_HRSP_1XX].u.u64),
968 U2H(stats[ST_F_HRSP_2XX].u.u64),
969 U2H(stats[ST_F_COMP_RSP].u.u64),
970 stats[ST_F_HRSP_2XX].u.u64 ?
971 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
972 U2H(stats[ST_F_HRSP_3XX].u.u64),
973 U2H(stats[ST_F_HRSP_4XX].u.u64),
974 U2H(stats[ST_F_HRSP_5XX].u.u64),
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100975 U2H(stats[ST_F_HRSP_OTHER].u.u64));
976
977 chunk_appendf(out,
978 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
979 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
980 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
981 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
982 "<tr><th>Internal errors:</th><td>%s</td></tr>"
983 "",
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200984 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100985 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
986 U2H(stats[ST_F_CACHE_HITS].u.u64),
987 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
988 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100989 U2H(stats[ST_F_WREW].u.u64),
990 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100991 }
992
993 chunk_appendf(out,
994 "</table></div></u></td>"
995 /* sessions: lbtot, lastsess */
996 "<td></td><td></td>"
997 /* bytes : in */
998 "<td>%s</td>"
999 "",
1000 U2H(stats[ST_F_BIN].u.u64));
1001
1002 chunk_appendf(out,
1003 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1004 "<td>%s%s<div class=tips><table class=det>"
1005 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1006 "<tr><th>Compression in:</th><td>%s</td></tr>"
1007 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1008 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1009 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1010 "</table></div>%s</td>",
1011 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1012 U2H(stats[ST_F_BOUT].u.u64),
1013 U2H(stats[ST_F_BOUT].u.u64),
1014 U2H(stats[ST_F_COMP_IN].u.u64),
1015 U2H(stats[ST_F_COMP_OUT].u.u64),
1016 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1017 U2H(stats[ST_F_COMP_BYP].u.u64),
1018 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1019 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,
1020 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1021
1022 chunk_appendf(out,
1023 /* denied: req, resp */
1024 "<td>%s</td><td>%s</td>"
1025 /* errors : request, connect, response */
1026 "<td>%s</td><td></td><td></td>"
1027 /* warnings: retries, redispatches */
1028 "<td></td><td></td>"
1029 /* server status : reflect frontend status */
1030 "<td class=ac>%s</td>"
1031 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001032 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001033 "",
1034 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1035 U2H(stats[ST_F_EREQ].u.u64),
1036 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001037
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001038 if (flags & STAT_SHMODULES) {
1039 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001040 chunk_appendf(out, "<td>");
1041
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001042 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001043 chunk_appendf(out,
1044 "<u>%s<div class=tips><table class=det>",
1045 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001046 for (j = 0; j < mod->stats_count; ++j) {
1047 chunk_appendf(out,
1048 "<tr><th>%s</th><td>%s</td></tr>",
1049 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1050 ++i;
1051 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001052 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001053 } else {
1054 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001055 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001056
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001057 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001058 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001059 }
1060
1061 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001062 }
1063 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
1064 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001065 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001066 /* Column sub-heading for Enable or Disable server */
1067 chunk_appendf(out, "<td></td>");
1068 }
1069
1070 chunk_appendf(out,
1071 /* frontend name, listener name */
1072 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1073 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1074 "",
1075 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001076 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001077 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1078
Willy Tarreau708c4162019-10-09 10:19:16 +02001079 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001080 chunk_appendf(out, "<div class=tips>");
1081
Willy Tarreau90807112020-02-25 08:16:33 +01001082 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001083 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1084 else if (*field_str(stats, ST_F_ADDR) == '[')
1085 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1086 else if (*field_str(stats, ST_F_ADDR))
1087 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1088
Willy Tarreau4cfb0012023-05-11 14:49:43 +02001089 chunk_appendf(out, "proto=%s, ", field_str(stats, ST_F_PROTO));
1090
William Lallemand74c24fb2016-11-21 17:18:36 +01001091 /* id */
1092 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1093 }
1094
1095 chunk_appendf(out,
1096 /* queue */
1097 "%s</td><td colspan=3></td>"
1098 /* sessions rate: current, max, limit */
1099 "<td colspan=3>&nbsp;</td>"
1100 /* sessions: current, max, limit, total, lbtot, lastsess */
1101 "<td>%s</td><td>%s</td><td>%s</td>"
1102 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1103 /* bytes: in, out */
1104 "<td>%s</td><td>%s</td>"
1105 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001106 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001107 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1108 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1109
1110 chunk_appendf(out,
1111 /* denied: req, resp */
1112 "<td>%s</td><td>%s</td>"
1113 /* errors: request, connect, response */
1114 "<td>%s</td><td></td><td></td>"
1115 /* warnings: retries, redispatches */
1116 "<td></td><td></td>"
1117 /* server status: reflect listener status */
1118 "<td class=ac>%s</td>"
1119 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001120 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001121 "",
1122 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1123 U2H(stats[ST_F_EREQ].u.u64),
1124 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001125
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001126 if (flags & STAT_SHMODULES) {
1127 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001128 chunk_appendf(out, "<td>");
1129
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001130 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001131 chunk_appendf(out,
1132 "<u>%s<div class=tips><table class=det>",
1133 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001134 for (j = 0; j < mod->stats_count; ++j) {
1135 chunk_appendf(out,
1136 "<tr><th>%s</th><td>%s</td></tr>",
1137 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1138 ++i;
1139 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001140 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001141 } else {
1142 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001143 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001144
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001145 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001146 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001147 }
1148
1149 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001150 }
1151 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1152 const char *style;
1153
1154 /* determine the style to use depending on the server's state,
1155 * its health and weight. There isn't a 1-to-1 mapping between
1156 * state and styles for the cases where the server is (still)
1157 * up. The reason is that we don't want to report nolb and
1158 * drain with the same color.
1159 */
1160
1161 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1162 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1163 style = "down";
1164 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001165 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001166 style = "going_up";
1167 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001168 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1169 style = "draining";
1170 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001171 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001172 style = "going_down";
1173 }
1174 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1175 style = "nolb";
1176 }
1177 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1178 style = "no_check";
1179 }
1180 else if (!stats[ST_F_CHKFAIL].type ||
1181 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1182 /* no check or max health = UP */
1183 if (stats[ST_F_WEIGHT].u.u32)
1184 style = "up";
1185 else
1186 style = "draining";
1187 }
1188 else {
1189 style = "going_down";
1190 }
1191
Willy Tarreau7b524852020-08-11 10:26:36 +02001192 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001193 chunk_appendf(out, "<tr class=\"maintain\">");
1194 else
1195 chunk_appendf(out,
1196 "<tr class=\"%s_%s\">",
1197 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1198
1199
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001200 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001201 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001202 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1203 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001204 field_str(stats, ST_F_SVNAME));
1205
1206 chunk_appendf(out,
1207 "<td class=ac><a name=\"%s/%s\"></a>%s"
1208 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1209 "",
1210 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001211 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001212 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1213
Willy Tarreau708c4162019-10-09 10:19:16 +02001214 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001215 chunk_appendf(out, "<div class=tips>");
1216
Willy Tarreau90807112020-02-25 08:16:33 +01001217 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001218 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1219 else if (*field_str(stats, ST_F_ADDR) == '[')
1220 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1221 else if (*field_str(stats, ST_F_ADDR))
1222 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1223
1224 /* id */
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01001225 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 +01001226
1227 /* cookie */
1228 if (stats[ST_F_COOKIE].type) {
1229 chunk_appendf(out, ", cookie: '");
1230 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1231 chunk_htmlencode(out, &src);
1232 chunk_appendf(out, "'");
1233 }
1234
1235 chunk_appendf(out, "</div>");
1236 }
1237
1238 chunk_appendf(out,
1239 /* queue : current, max, limit */
1240 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1241 /* sessions rate : current, max, limit */
1242 "<td>%s</td><td>%s</td><td></td>"
1243 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001244 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001245 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1246 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1247
1248 chunk_appendf(out,
1249 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001250 "<td><u>%s<div class=tips>"
1251 "<table class=det>"
1252 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001253 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001254 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001255 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1256 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001257 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001258 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1259 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1260 "</table></div></u>"
1261 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001262 "<td><u>%s<div class=tips><table class=det>"
1263 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1264 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001265 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001266 U2H(stats[ST_F_SCUR].u.u32),
1267 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1268 U2H(stats[ST_F_SRV_ICUR].u.u32),
1269 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1270 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001271 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001272
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001273 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1274 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1275 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001276 U2H(stats[ST_F_STOT].u.u64),
1277 U2H(stats[ST_F_STOT].u.u64));
1278
1279 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1280 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001281 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001282 "<tr><th>New connections:</th><td>%s</td></tr>"
1283 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001284 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001285 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1286 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1287 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1288 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1289 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1290 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001291 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001292 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001293 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001294 U2H(stats[ST_F_CONNECT].u.u64),
1295 U2H(stats[ST_F_REUSE].u.u64),
1296 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1297 (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 +02001298 U2H(stats[ST_F_REQ_TOT].u.u64),
1299 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1300 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1301 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1302 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1303 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1304 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1305 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1306 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1307 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1308 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1309 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1310 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001311 U2H(stats[ST_F_WREW].u.u64),
1312 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001313 }
1314
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001315 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1316 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1317 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1318 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1319 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001320 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001321 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1322 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1323 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1324 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001325
1326 chunk_appendf(out,
1327 "</table></div></u></td>"
1328 /* sessions: lbtot, last */
1329 "<td>%s</td><td>%s</td>",
1330 U2H(stats[ST_F_LBTOT].u.u64),
1331 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1332
1333 chunk_appendf(out,
1334 /* bytes : in, out */
1335 "<td>%s</td><td>%s</td>"
1336 /* denied: req, resp */
1337 "<td></td><td>%s</td>"
1338 /* errors : request, connect */
1339 "<td></td><td>%s</td>"
1340 /* errors : response */
1341 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1342 /* warnings: retries, redispatches */
1343 "<td>%lld</td><td>%lld</td>"
1344 "",
1345 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1346 U2H(stats[ST_F_DRESP].u.u64),
1347 U2H(stats[ST_F_ECON].u.u64),
1348 U2H(stats[ST_F_ERESP].u.u64),
1349 (long long)stats[ST_F_CLI_ABRT].u.u64,
1350 (long long)stats[ST_F_SRV_ABRT].u.u64,
1351 (long long)stats[ST_F_WRETR].u.u64,
1352 (long long)stats[ST_F_WREDIS].u.u64);
1353
1354 /* status, last change */
1355 chunk_appendf(out, "<td class=ac>");
1356
1357 /* FIXME!!!!
1358 * LASTCHG should contain the last change for *this* server and must be computed
1359 * properly above, as was done below, ie: this server if maint, otherwise ref server
1360 * if tracking. Note that ref is either local or remote depending on tracking.
1361 */
1362
1363
Willy Tarreau7b524852020-08-11 10:26:36 +02001364 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001365 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1366 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001367 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001368 chunk_strcat(out, "<i>no check</i>");
1369 }
1370 else {
1371 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 +02001372 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001373 if (stats[ST_F_CHECK_HEALTH].u.u32)
1374 chunk_strcat(out, " &uarr;");
1375 }
1376 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1377 chunk_strcat(out, " &darr;");
1378 }
1379
Willy Tarreau7b524852020-08-11 10:26:36 +02001380 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001381 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1382 chunk_appendf(out,
1383 "</td><td class=ac><u> %s",
1384 field_str(stats, ST_F_AGENT_STATUS));
1385
1386 if (stats[ST_F_AGENT_CODE].type)
1387 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1388
1389 if (stats[ST_F_AGENT_DURATION].type)
1390 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1391
1392 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1393
1394 if (*field_str(stats, ST_F_LAST_AGT)) {
1395 chunk_appendf(out, ": ");
1396 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1397 chunk_htmlencode(out, &src);
1398 }
1399 chunk_appendf(out, "</div></u>");
1400 }
1401 else if (stats[ST_F_CHECK_STATUS].type) {
1402 chunk_appendf(out,
1403 "</td><td class=ac><u> %s",
1404 field_str(stats, ST_F_CHECK_STATUS));
1405
1406 if (stats[ST_F_CHECK_CODE].type)
1407 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1408
1409 if (stats[ST_F_CHECK_DURATION].type)
1410 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1411
1412 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1413
1414 if (*field_str(stats, ST_F_LAST_CHK)) {
1415 chunk_appendf(out, ": ");
1416 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1417 chunk_htmlencode(out, &src);
1418 }
1419 chunk_appendf(out, "</div></u>");
1420 }
1421 else
1422 chunk_appendf(out, "</td><td>");
1423
1424 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001425 /* weight / uweight */
1426 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001427 /* act, bck */
1428 "<td class=ac>%s</td><td class=ac>%s</td>"
1429 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001430 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001431 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1432 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1433
1434 /* check failures: unique, fatal, down time */
1435 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1436 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1437 }
1438 else if (stats[ST_F_CHKFAIL].type) {
1439 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1440
1441 if (stats[ST_F_HANAFAIL].type)
1442 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1443
1444 chunk_appendf(out,
1445 "<div class=tips>Failed Health Checks%s</div></u></td>"
1446 "<td>%lld</td><td>%s</td>"
1447 "",
1448 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1449 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1450 }
1451 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1452 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1453 chunk_appendf(out,
1454 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1455 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1456 }
1457 else
1458 chunk_appendf(out, "<td colspan=3></td>");
1459
1460 /* throttle */
1461 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001462 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001463 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001464 chunk_appendf(out, "<td class=ac>-</td>");
1465
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001466 if (flags & STAT_SHMODULES) {
1467 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001468 chunk_appendf(out, "<td>");
1469
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001470 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001471 chunk_appendf(out,
1472 "<u>%s<div class=tips><table class=det>",
1473 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001474 for (j = 0; j < mod->stats_count; ++j) {
1475 chunk_appendf(out,
1476 "<tr><th>%s</th><td>%s</td></tr>",
1477 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1478 ++i;
1479 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001480 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001481 } else {
1482 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001483 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001484
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001485 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001486 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001487 }
1488
1489 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001490 }
1491 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1492 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001493 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001494 /* Column sub-heading for Enable or Disable server */
1495 chunk_appendf(out, "<td></td>");
1496 }
1497 chunk_appendf(out,
1498 "<td class=ac>"
1499 /* name */
1500 "%s<a name=\"%s/Backend\"></a>"
1501 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1502 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001503 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001504 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1505
Willy Tarreau708c4162019-10-09 10:19:16 +02001506 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001507 /* balancing */
1508 chunk_appendf(out, "<div class=tips>balancing: %s",
1509 field_str(stats, ST_F_ALGO));
1510
1511 /* cookie */
1512 if (stats[ST_F_COOKIE].type) {
1513 chunk_appendf(out, ", cookie: '");
1514 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1515 chunk_htmlencode(out, &src);
1516 chunk_appendf(out, "'");
1517 }
1518 chunk_appendf(out, "</div>");
1519 }
1520
1521 chunk_appendf(out,
1522 "%s</td>"
1523 /* queue : current, max */
1524 "<td>%s</td><td>%s</td><td></td>"
1525 /* sessions rate : current, max, limit */
1526 "<td>%s</td><td>%s</td><td></td>"
1527 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001528 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001529 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1530 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1531
1532 chunk_appendf(out,
1533 /* sessions: current, max, limit, total */
1534 "<td>%s</td><td>%s</td><td>%s</td>"
1535 "<td><u>%s<div class=tips><table class=det>"
1536 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1537 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001538 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 +01001539 U2H(stats[ST_F_STOT].u.u64),
1540 U2H(stats[ST_F_STOT].u.u64));
1541
1542 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1543 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1544 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001545 "<tr><th>New connections:</th><td>%s</td></tr>"
1546 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001547 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1548 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1549 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1550 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1551 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1552 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1553 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1554 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001555 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1556 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001557 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001558 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001559 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001560 U2H(stats[ST_F_CONNECT].u.u64),
1561 U2H(stats[ST_F_REUSE].u.u64),
1562 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1563 (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 +01001564 U2H(stats[ST_F_REQ_TOT].u.u64),
1565 U2H(stats[ST_F_HRSP_1XX].u.u64),
1566 U2H(stats[ST_F_HRSP_2XX].u.u64),
1567 U2H(stats[ST_F_COMP_RSP].u.u64),
1568 stats[ST_F_HRSP_2XX].u.u64 ?
1569 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1570 U2H(stats[ST_F_HRSP_3XX].u.u64),
1571 U2H(stats[ST_F_HRSP_4XX].u.u64),
1572 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001573 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001574 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1575 U2H(stats[ST_F_CACHE_HITS].u.u64),
1576 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1577 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001578 U2H(stats[ST_F_WREW].u.u64),
1579 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001580 }
1581
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001582 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1583 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1584 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1585 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1586 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001587 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001588 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1589 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1590 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1591 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001592
1593 chunk_appendf(out,
1594 "</table></div></u></td>"
1595 /* sessions: lbtot, last */
1596 "<td>%s</td><td>%s</td>"
1597 /* bytes: in */
1598 "<td>%s</td>"
1599 "",
1600 U2H(stats[ST_F_LBTOT].u.u64),
1601 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1602 U2H(stats[ST_F_BIN].u.u64));
1603
1604 chunk_appendf(out,
1605 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1606 "<td>%s%s<div class=tips><table class=det>"
1607 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1608 "<tr><th>Compression in:</th><td>%s</td></tr>"
1609 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1610 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1611 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1612 "</table></div>%s</td>",
1613 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1614 U2H(stats[ST_F_BOUT].u.u64),
1615 U2H(stats[ST_F_BOUT].u.u64),
1616 U2H(stats[ST_F_COMP_IN].u.u64),
1617 U2H(stats[ST_F_COMP_OUT].u.u64),
1618 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1619 U2H(stats[ST_F_COMP_BYP].u.u64),
1620 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1621 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,
1622 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1623
1624 chunk_appendf(out,
1625 /* denied: req, resp */
1626 "<td>%s</td><td>%s</td>"
1627 /* errors : request, connect */
1628 "<td></td><td>%s</td>"
1629 /* errors : response */
1630 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1631 /* warnings: retries, redispatches */
1632 "<td>%lld</td><td>%lld</td>"
1633 /* backend status: reflect backend status (up/down): we display UP
1634 * if the backend has known working servers or if it has no server at
1635 * all (eg: for stats). Then we display the total weight, number of
1636 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001637 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001638 "<td class=ac>%d</td><td class=ac>%d</td>"
1639 "",
1640 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1641 U2H(stats[ST_F_ECON].u.u64),
1642 U2H(stats[ST_F_ERESP].u.u64),
1643 (long long)stats[ST_F_CLI_ABRT].u.u64,
1644 (long long)stats[ST_F_SRV_ABRT].u.u64,
1645 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1646 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1647 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 +02001648 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001649 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1650
1651 chunk_appendf(out,
1652 /* rest of backend: nothing, down transitions, total downtime, throttle */
1653 "<td class=ac>&nbsp;</td><td>%d</td>"
1654 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001655 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001656 stats[ST_F_CHKDOWN].u.u32,
1657 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001658
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001659 if (flags & STAT_SHMODULES) {
1660 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001661 chunk_appendf(out, "<td>");
1662
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001663 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001664 chunk_appendf(out,
1665 "<u>%s<div class=tips><table class=det>",
1666 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001667 for (j = 0; j < mod->stats_count; ++j) {
1668 chunk_appendf(out,
1669 "<tr><th>%s</th><td>%s</td></tr>",
1670 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1671 ++i;
1672 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001673 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001674 } else {
1675 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001676 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001677
1678 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001679 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001680 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001681
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001682 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001683 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001684
William Lallemand74c24fb2016-11-21 17:18:36 +01001685 return 1;
1686}
1687
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001688int stats_dump_one_line(const struct field *stats, size_t stats_count,
1689 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001690{
Willy Tarreau91cefca2022-05-03 17:08:29 +02001691 struct show_stat_ctx *ctx = appctx->svcctx;
Simon Horman05ee2132017-01-04 09:37:25 +01001692 int ret;
1693
Willy Tarreau91cefca2022-05-03 17:08:29 +02001694 if (ctx->flags & STAT_FMT_HTML)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001695 ret = stats_dump_fields_html(&trash_chunk, stats, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001696 else if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001697 ret = stats_dump_fields_typed(&trash_chunk, stats, stats_count, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001698 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001699 ret = stats_dump_fields_json(&trash_chunk, stats, stats_count, ctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001700 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01001701 ret = stats_dump_fields_csv(&trash_chunk, stats, stats_count, ctx);
Simon Horman05ee2132017-01-04 09:37:25 +01001702
Simon Horman05ee2132017-01-04 09:37:25 +01001703 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001704}
1705
William Dauchy0ef54392021-01-17 18:27:45 +01001706/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001707 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1708 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1709 * this value, or if the selected field is not implemented for frontends, the
1710 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001711 */
William Dauchy0ef54392021-01-17 18:27:45 +01001712int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1713 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001714{
William Dauchy0ef54392021-01-17 18:27:45 +01001715 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001716
William Lallemand74c24fb2016-11-21 17:18:36 +01001717 if (len < ST_F_TOTAL_FIELDS)
1718 return 0;
1719
William Dauchy0ef54392021-01-17 18:27:45 +01001720 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001721 struct field metric = { 0 };
1722
William Dauchy0ef54392021-01-17 18:27:45 +01001723 switch (current_field) {
1724 case ST_F_PXNAME:
1725 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1726 break;
1727 case ST_F_SVNAME:
1728 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1729 break;
1730 case ST_F_MODE:
1731 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1732 break;
1733 case ST_F_SCUR:
1734 metric = mkf_u32(0, px->feconn);
1735 break;
1736 case ST_F_SMAX:
1737 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1738 break;
1739 case ST_F_SLIM:
1740 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1741 break;
1742 case ST_F_STOT:
1743 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1744 break;
1745 case ST_F_BIN:
1746 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1747 break;
1748 case ST_F_BOUT:
1749 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1750 break;
1751 case ST_F_DREQ:
1752 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1753 break;
1754 case ST_F_DRESP:
1755 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1756 break;
1757 case ST_F_EREQ:
1758 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1759 break;
1760 case ST_F_DCON:
1761 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1762 break;
1763 case ST_F_DSES:
1764 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1765 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001766 case ST_F_STATUS: {
1767 const char *state;
1768
1769 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1770 state = "STOP";
1771 else if (px->flags & PR_FL_PAUSED)
1772 state = "PAUSED";
1773 else
1774 state = "OPEN";
1775 metric = mkf_str(FO_STATUS, state);
William Dauchy0ef54392021-01-17 18:27:45 +01001776 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001777 }
William Dauchy0ef54392021-01-17 18:27:45 +01001778 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001779 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001780 break;
1781 case ST_F_IID:
1782 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1783 break;
1784 case ST_F_SID:
1785 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1786 break;
1787 case ST_F_TYPE:
1788 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1789 break;
1790 case ST_F_RATE:
1791 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1792 break;
1793 case ST_F_RATE_LIM:
1794 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1795 break;
1796 case ST_F_RATE_MAX:
1797 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1798 break;
1799 case ST_F_WREW:
1800 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1801 break;
1802 case ST_F_EINT:
1803 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1804 break;
1805 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001806 if (px->mode == PR_MODE_HTTP)
1807 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001808 break;
1809 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001810 if (px->mode == PR_MODE_HTTP)
1811 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001812 break;
1813 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001814 if (px->mode == PR_MODE_HTTP)
1815 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001816 break;
1817 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001818 if (px->mode == PR_MODE_HTTP)
1819 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001820 break;
1821 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001822 if (px->mode == PR_MODE_HTTP)
1823 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001824 break;
1825 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001826 if (px->mode == PR_MODE_HTTP)
1827 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001828 break;
1829 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001830 if (px->mode == PR_MODE_HTTP)
1831 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001832 break;
1833 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001834 if (px->mode == PR_MODE_HTTP)
1835 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001836 break;
1837 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001838 if (px->mode == PR_MODE_HTTP)
1839 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001840 break;
1841 case ST_F_REQ_RATE:
1842 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1843 break;
1844 case ST_F_REQ_RATE_MAX:
1845 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1846 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001847 case ST_F_REQ_TOT: {
1848 int i;
1849 uint64_t total_req;
1850 size_t nb_reqs =
1851 sizeof(px->fe_counters.p.http.cum_req) / sizeof(*px->fe_counters.p.http.cum_req);
1852
1853 total_req = 0;
1854 for (i = 0; i < nb_reqs; i++)
1855 total_req += px->fe_counters.p.http.cum_req[i];
1856 metric = mkf_u64(FN_COUNTER, total_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001857 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001858 }
William Dauchy0ef54392021-01-17 18:27:45 +01001859 case ST_F_COMP_IN:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001860 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001861 break;
1862 case ST_F_COMP_OUT:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001863 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001864 break;
1865 case ST_F_COMP_BYP:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001866 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001867 break;
1868 case ST_F_COMP_RSP:
1869 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1870 break;
1871 case ST_F_CONN_RATE:
1872 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1873 break;
1874 case ST_F_CONN_RATE_MAX:
1875 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1876 break;
1877 case ST_F_CONN_TOT:
1878 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1879 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001880 case ST_F_SESS_OTHER: {
1881 int i;
1882 uint64_t total_sess;
1883 size_t nb_sess =
1884 sizeof(px->fe_counters.cum_sess_ver) / sizeof(*px->fe_counters.cum_sess_ver);
1885
1886 total_sess = px->fe_counters.cum_sess;
1887 for (i = 0; i < nb_sess; i++)
1888 total_sess -= px->fe_counters.cum_sess_ver[i];
Frédéric Lécailled97d1d72023-02-06 09:23:56 +01001889 total_sess = (int64_t)total_sess < 0 ? 0 : total_sess;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001890 metric = mkf_u64(FN_COUNTER, total_sess);
1891 break;
1892 }
1893 case ST_F_H1SESS:
1894 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[0]);
1895 break;
1896 case ST_F_H2SESS:
1897 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[1]);
1898 break;
1899 case ST_F_H3SESS:
1900 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[2]);
1901 break;
1902 case ST_F_REQ_OTHER:
1903 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[0]);
1904 break;
1905 case ST_F_H1REQ:
1906 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[1]);
1907 break;
1908 case ST_F_H2REQ:
1909 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[2]);
1910 break;
1911 case ST_F_H3REQ:
1912 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[3]);
1913 break;
William Dauchy0ef54392021-01-17 18:27:45 +01001914 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001915 /* not used for frontends. If a specific metric
1916 * is requested, return an error. Otherwise continue.
1917 */
1918 if (selected_field != NULL)
1919 return 0;
1920 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001921 }
1922 stats[current_field] = metric;
1923 if (selected_field != NULL)
1924 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001925 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001926 return 1;
1927}
1928
Christopher Fauleta8b76842022-12-16 15:08:36 +01001929/* Dumps a frontend's line to the local trash buffer for the current proxy <px>
1930 * and uses the state from stream connector <sc>. The caller is responsible for
1931 * clearing the local trash buffer if needed. Returns non-zero if it emits
1932 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001933 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001934static int stats_dump_fe_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001935{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001936 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001937 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001938 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1939 struct stats_module *mod;
1940 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001941
1942 if (!(px->cap & PR_CAP_FE))
1943 return 0;
1944
Willy Tarreau91cefca2022-05-03 17:08:29 +02001945 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_FE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001946 return 0;
1947
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001948 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1949
William Dauchy0ef54392021-01-17 18:27:45 +01001950 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001951 return 0;
1952
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001953 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1954 void *counters;
1955
1956 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1957 stats_count += mod->stats_count;
1958 continue;
1959 }
1960
1961 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1962 mod->fill_stats(counters, stats + stats_count);
1963 stats_count += mod->stats_count;
1964 }
1965
1966 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001967}
1968
William Dauchy655e14e2021-02-14 23:22:54 +01001969/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1970 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1971 * this length is less then this value, the function returns 0, otherwise, it
1972 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1973 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001974 */
1975int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001976 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001977{
William Dauchy655e14e2021-02-14 23:22:54 +01001978 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001979 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001980
1981 if (len < ST_F_TOTAL_FIELDS)
1982 return 0;
1983
1984 if (!l->counters)
1985 return 0;
1986
1987 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001988
William Dauchy655e14e2021-02-14 23:22:54 +01001989 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1990 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001991
William Dauchy655e14e2021-02-14 23:22:54 +01001992 switch (current_field) {
1993 case ST_F_PXNAME:
1994 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1995 break;
1996 case ST_F_SVNAME:
1997 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1998 break;
1999 case ST_F_MODE:
2000 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2001 break;
2002 case ST_F_SCUR:
2003 metric = mkf_u32(0, l->nbconn);
2004 break;
2005 case ST_F_SMAX:
2006 metric = mkf_u32(FN_MAX, l->counters->conn_max);
2007 break;
2008 case ST_F_SLIM:
Willy Tarreau758c69d2023-01-12 18:59:37 +01002009 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->bind_conf->maxconn);
William Dauchy655e14e2021-02-14 23:22:54 +01002010 break;
2011 case ST_F_STOT:
2012 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
2013 break;
2014 case ST_F_BIN:
2015 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
2016 break;
2017 case ST_F_BOUT:
2018 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
2019 break;
2020 case ST_F_DREQ:
2021 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
2022 break;
2023 case ST_F_DRESP:
2024 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
2025 break;
2026 case ST_F_EREQ:
2027 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
2028 break;
2029 case ST_F_DCON:
2030 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
2031 break;
2032 case ST_F_DSES:
2033 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
2034 break;
2035 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01002036 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01002037 break;
2038 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002039 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01002040 break;
2041 case ST_F_IID:
2042 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2043 break;
2044 case ST_F_SID:
2045 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
2046 break;
2047 case ST_F_TYPE:
2048 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
2049 break;
2050 case ST_F_WREW:
2051 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
2052 break;
2053 case ST_F_EINT:
2054 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
2055 break;
2056 case ST_F_ADDR:
2057 if (flags & STAT_SHLGNDS) {
2058 char str[INET6_ADDRSTRLEN];
2059 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01002060
William Dauchy655e14e2021-02-14 23:22:54 +01002061 port = get_host_port(&l->rx.addr);
2062 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
2063 case AF_INET:
2064 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2065 chunk_appendf(out, "%s:%d", str, port);
2066 break;
2067 case AF_INET6:
2068 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2069 chunk_appendf(out, "[%s]:%d", str, port);
2070 break;
2071 case AF_UNIX:
2072 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2073 break;
2074 case -1:
2075 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2076 chunk_strcat(out, strerror(errno));
2077 break;
2078 default: /* address family not supported */
2079 break;
2080 }
2081 }
2082 break;
Willy Tarreau4cfb0012023-05-11 14:49:43 +02002083 case ST_F_PROTO:
2084 metric = mkf_str(FO_STATUS, l->rx.proto->name);
2085 break;
William Dauchy655e14e2021-02-14 23:22:54 +01002086 default:
2087 /* not used for listen. If a specific metric
2088 * is requested, return an error. Otherwise continue.
2089 */
2090 if (selected_field != NULL)
2091 return 0;
2092 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002093 }
William Dauchy655e14e2021-02-14 23:22:54 +01002094 stats[current_field] = metric;
2095 if (selected_field != NULL)
2096 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002097 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002098 return 1;
2099}
2100
Christopher Fauleta8b76842022-12-16 15:08:36 +01002101/* Dumps a line for listener <l> and proxy <px> to the local trash buffer and
2102 * uses the state from stream connector <sc>. The caller is responsible for
2103 * clearing the local trash buffer if needed. Returns non-zero if it emits
2104 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002105 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002106static int stats_dump_li_stats(struct stconn *sc, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01002107{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002108 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002109 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002110 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2111 struct stats_module *mod;
2112 size_t stats_count = ST_F_TOTAL_FIELDS;
2113
2114 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002115
Willy Tarreau91cefca2022-05-03 17:08:29 +02002116 if (!stats_fill_li_stats(px, l, ctx->flags, stats,
William Dauchy655e14e2021-02-14 23:22:54 +01002117 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002118 return 0;
2119
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002120 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2121 void *counters;
2122
2123 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
2124 stats_count += mod->stats_count;
2125 continue;
2126 }
2127
2128 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2129 mod->fill_stats(counters, stats + stats_count);
2130 stats_count += mod->stats_count;
2131 }
2132
2133 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002134}
2135
2136enum srv_stats_state {
2137 SRV_STATS_STATE_DOWN = 0,
2138 SRV_STATS_STATE_DOWN_AGENT,
2139 SRV_STATS_STATE_GOING_UP,
2140 SRV_STATS_STATE_UP_GOING_DOWN,
2141 SRV_STATS_STATE_UP,
2142 SRV_STATS_STATE_NOLB_GOING_DOWN,
2143 SRV_STATS_STATE_NOLB,
2144 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2145 SRV_STATS_STATE_DRAIN,
2146 SRV_STATS_STATE_DRAIN_AGENT,
2147 SRV_STATS_STATE_NO_CHECK,
2148
2149 SRV_STATS_STATE_COUNT, /* Must be last */
2150};
2151
2152static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2153 [SRV_STATS_STATE_DOWN] = "DOWN",
2154 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2155 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2156 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2157 [SRV_STATS_STATE_UP] = "UP",
2158 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2159 [SRV_STATS_STATE_NOLB] = "NOLB",
2160 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2161 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2162 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2163 [SRV_STATS_STATE_NO_CHECK] = "no check"
2164};
2165
William Dauchyd3a9a492021-01-25 17:29:03 +01002166/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002167 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002168static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2169 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002170{
Emeric Brun52a91d32017-08-31 14:41:55 +02002171 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002172 if ((ref->check.state & CHK_ST_ENABLED) &&
2173 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002174 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002175 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002176 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002177 }
2178
Emeric Brun52a91d32017-08-31 14:41:55 +02002179 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002180 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002181 *state = SRV_STATS_STATE_DRAIN_AGENT;
2182 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2183 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002184 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002185 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002186 }
2187
William Dauchyd3a9a492021-01-25 17:29:03 +01002188 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2189 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002190 }
2191 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002192 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002193 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2194 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002195 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002196 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002197 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002198 }
2199 }
2200 else { /* stopped */
2201 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002202 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002203 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002204 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002205 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002206 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002207 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002208 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002209 }
2210 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002211}
William Lallemand74c24fb2016-11-21 17:18:36 +01002212
William Dauchyd3a9a492021-01-25 17:29:03 +01002213/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2214 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2215 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2216 * this value, or if the selected field is not implemented for servers, the
2217 * function returns 0, otherwise, it returns 1. <flags> can take the value
2218 * STAT_SHLGNDS.
2219 */
2220int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2221 struct field *stats, int len,
2222 enum stat_field *selected_field)
2223{
2224 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2225 struct server *via = sv->track ? sv->track : sv;
2226 struct server *ref = via;
2227 enum srv_stats_state state = 0;
2228 char str[INET6_ADDRSTRLEN];
2229 struct buffer *out = get_trash_chunk();
2230 char *fld_status;
2231 long long srv_samples_counter;
2232 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002233
William Dauchyd3a9a492021-01-25 17:29:03 +01002234 if (len < ST_F_TOTAL_FIELDS)
2235 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002236
William Dauchyd3a9a492021-01-25 17:29:03 +01002237 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002238
William Dauchyd3a9a492021-01-25 17:29:03 +01002239 /* compute state for later use */
2240 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2241 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2242 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2243 /* we have "via" which is the tracked server as described in the configuration,
2244 * and "ref" which is the checked server and the end of the chain.
2245 */
2246 while (ref->track)
2247 ref = ref->track;
2248 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002249 }
2250
William Dauchyd3a9a492021-01-25 17:29:03 +01002251 /* compue time values for later use */
2252 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2253 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2254 *selected_field == ST_F_TTIME) {
2255 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2256 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2257 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002258 }
2259
William Dauchyd3a9a492021-01-25 17:29:03 +01002260 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2261 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002262
William Dauchyd3a9a492021-01-25 17:29:03 +01002263 switch (current_field) {
2264 case ST_F_PXNAME:
2265 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2266 break;
2267 case ST_F_SVNAME:
2268 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2269 break;
2270 case ST_F_MODE:
2271 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002272 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002273 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002274 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002275 break;
2276 case ST_F_QMAX:
2277 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2278 break;
2279 case ST_F_SCUR:
2280 metric = mkf_u32(0, sv->cur_sess);
2281 break;
2282 case ST_F_SMAX:
2283 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2284 break;
2285 case ST_F_SLIM:
2286 if (sv->maxconn)
2287 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2288 break;
2289 case ST_F_SRV_ICUR:
2290 metric = mkf_u32(0, sv->curr_idle_conns);
2291 break;
2292 case ST_F_SRV_ILIM:
2293 if (sv->max_idle_conns != -1)
2294 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2295 break;
2296 case ST_F_STOT:
2297 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2298 break;
2299 case ST_F_BIN:
2300 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2301 break;
2302 case ST_F_BOUT:
2303 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2304 break;
2305 case ST_F_DRESP:
2306 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2307 break;
2308 case ST_F_ECON:
2309 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2310 break;
2311 case ST_F_ERESP:
2312 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2313 break;
2314 case ST_F_WRETR:
2315 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2316 break;
2317 case ST_F_WREDIS:
2318 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2319 break;
2320 case ST_F_WREW:
2321 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2322 break;
2323 case ST_F_EINT:
2324 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2325 break;
2326 case ST_F_CONNECT:
2327 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2328 break;
2329 case ST_F_REUSE:
2330 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2331 break;
2332 case ST_F_IDLE_CONN_CUR:
2333 metric = mkf_u32(0, sv->curr_idle_nb);
2334 break;
2335 case ST_F_SAFE_CONN_CUR:
2336 metric = mkf_u32(0, sv->curr_safe_nb);
2337 break;
2338 case ST_F_USED_CONN_CUR:
2339 metric = mkf_u32(0, sv->curr_used_conns);
2340 break;
2341 case ST_F_NEED_CONN_EST:
2342 metric = mkf_u32(0, sv->est_need_conns);
2343 break;
2344 case ST_F_STATUS:
2345 fld_status = chunk_newstr(out);
2346 if (sv->cur_admin & SRV_ADMF_RMAINT)
2347 chunk_appendf(out, "MAINT (resolution)");
2348 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2349 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2350 else if (sv->cur_admin & SRV_ADMF_MAINT)
2351 chunk_appendf(out, "MAINT");
2352 else
2353 chunk_appendf(out,
2354 srv_hlt_st[state],
2355 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2356 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002357
William Dauchyd3a9a492021-01-25 17:29:03 +01002358 metric = mkf_str(FO_STATUS, fld_status);
2359 break;
2360 case ST_F_LASTCHG:
Willy Tarreau69530f52023-04-28 09:16:15 +02002361 metric = mkf_u32(FN_AGE, ns_to_sec(now_ns) - sv->last_change);
William Dauchyd3a9a492021-01-25 17:29:03 +01002362 break;
2363 case ST_F_WEIGHT:
2364 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2365 break;
2366 case ST_F_UWEIGHT:
2367 metric = mkf_u32(FN_AVG, sv->uweight);
2368 break;
2369 case ST_F_ACT:
2370 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2371 break;
2372 case ST_F_BCK:
2373 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2374 break;
2375 case ST_F_CHKFAIL:
2376 if (sv->check.state & CHK_ST_ENABLED)
2377 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2378 break;
2379 case ST_F_CHKDOWN:
2380 if (sv->check.state & CHK_ST_ENABLED)
2381 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2382 break;
2383 case ST_F_DOWNTIME:
2384 if (sv->check.state & CHK_ST_ENABLED)
2385 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2386 break;
2387 case ST_F_QLIMIT:
2388 if (sv->maxqueue)
2389 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2390 break;
2391 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002392 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002393 break;
2394 case ST_F_IID:
2395 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2396 break;
2397 case ST_F_SID:
2398 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2399 break;
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01002400 case ST_F_SRID:
2401 metric = mkf_u32(FN_COUNTER, sv->rid);
2402 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002403 case ST_F_THROTTLE:
2404 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2405 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2406 break;
2407 case ST_F_LBTOT:
2408 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2409 break;
2410 case ST_F_TRACKED:
2411 if (sv->track) {
2412 char *fld_track = chunk_newstr(out);
2413 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2414 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2415 }
2416 break;
2417 case ST_F_TYPE:
2418 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2419 break;
2420 case ST_F_RATE:
2421 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2422 break;
2423 case ST_F_RATE_MAX:
2424 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2425 break;
2426 case ST_F_CHECK_STATUS:
2427 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2428 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002429
William Dauchyd3a9a492021-01-25 17:29:03 +01002430 fld_chksts = chunk_newstr(out);
2431 chunk_strcat(out, "* "); // for check in progress
2432 chunk_strcat(out, get_check_status_info(sv->check.status));
2433 if (!(sv->check.state & CHK_ST_INPROGRESS))
2434 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002435 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002436 }
2437 break;
2438 case ST_F_CHECK_CODE:
2439 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2440 sv->check.status >= HCHK_STATUS_L57DATA)
2441 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2442 break;
2443 case ST_F_CHECK_DURATION:
2444 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2445 sv->check.status >= HCHK_STATUS_CHECKED)
Aurelien DARRAGONb118f2f2022-12-07 14:52:10 +01002446 metric = mkf_u64(FN_DURATION, MAX(sv->check.duration, 0));
William Dauchyd3a9a492021-01-25 17:29:03 +01002447 break;
2448 case ST_F_CHECK_DESC:
2449 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2450 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2451 break;
2452 case ST_F_LAST_CHK:
2453 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2454 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2455 break;
2456 case ST_F_CHECK_RISE:
2457 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2458 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2459 break;
2460 case ST_F_CHECK_FALL:
2461 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2462 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2463 break;
2464 case ST_F_CHECK_HEALTH:
2465 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2466 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2467 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002468 case ST_F_AGENT_STATUS:
2469 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2470 const char *fld_chksts;
2471
2472 fld_chksts = chunk_newstr(out);
2473 chunk_strcat(out, "* "); // for check in progress
2474 chunk_strcat(out, get_check_status_info(sv->agent.status));
2475 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2476 fld_chksts += 2; // skip "* "
2477 metric = mkf_str(FN_OUTPUT, fld_chksts);
2478 }
2479 break;
2480 case ST_F_AGENT_CODE:
2481 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2482 (sv->agent.status >= HCHK_STATUS_L57DATA))
2483 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2484 break;
2485 case ST_F_AGENT_DURATION:
2486 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2487 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2488 break;
2489 case ST_F_AGENT_DESC:
2490 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2491 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2492 break;
2493 case ST_F_LAST_AGT:
2494 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2495 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2496 break;
2497 case ST_F_AGENT_RISE:
2498 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2499 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2500 break;
2501 case ST_F_AGENT_FALL:
2502 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2503 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2504 break;
2505 case ST_F_AGENT_HEALTH:
2506 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2507 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2508 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002509 case ST_F_REQ_TOT:
2510 if (px->mode == PR_MODE_HTTP)
2511 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2512 break;
2513 case ST_F_HRSP_1XX:
2514 if (px->mode == PR_MODE_HTTP)
2515 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2516 break;
2517 case ST_F_HRSP_2XX:
2518 if (px->mode == PR_MODE_HTTP)
2519 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2520 break;
2521 case ST_F_HRSP_3XX:
2522 if (px->mode == PR_MODE_HTTP)
2523 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2524 break;
2525 case ST_F_HRSP_4XX:
2526 if (px->mode == PR_MODE_HTTP)
2527 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2528 break;
2529 case ST_F_HRSP_5XX:
2530 if (px->mode == PR_MODE_HTTP)
2531 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2532 break;
2533 case ST_F_HRSP_OTHER:
2534 if (px->mode == PR_MODE_HTTP)
2535 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2536 break;
2537 case ST_F_HANAFAIL:
2538 if (ref->observe)
2539 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2540 break;
2541 case ST_F_CLI_ABRT:
2542 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2543 break;
2544 case ST_F_SRV_ABRT:
2545 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2546 break;
2547 case ST_F_LASTSESS:
2548 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2549 break;
2550 case ST_F_QTIME:
2551 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2552 break;
2553 case ST_F_CTIME:
2554 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2555 break;
2556 case ST_F_RTIME:
2557 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2558 break;
2559 case ST_F_TTIME:
2560 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2561 break;
2562 case ST_F_QT_MAX:
2563 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2564 break;
2565 case ST_F_CT_MAX:
2566 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2567 break;
2568 case ST_F_RT_MAX:
2569 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2570 break;
2571 case ST_F_TT_MAX:
2572 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2573 break;
2574 case ST_F_ADDR:
2575 if (flags & STAT_SHLGNDS) {
2576 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2577 case AF_INET:
2578 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2579 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2580 break;
2581 case AF_INET6:
2582 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2583 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2584 break;
2585 case AF_UNIX:
2586 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2587 break;
2588 case -1:
2589 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2590 chunk_strcat(out, strerror(errno));
2591 break;
2592 default: /* address family not supported */
2593 break;
2594 }
2595 }
2596 break;
2597 case ST_F_COOKIE:
2598 if (flags & STAT_SHLGNDS && sv->cookie)
2599 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2600 break;
2601 default:
2602 /* not used for servers. If a specific metric
2603 * is requested, return an error. Otherwise continue.
2604 */
2605 if (selected_field != NULL)
2606 return 0;
2607 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002608 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002609 stats[current_field] = metric;
2610 if (selected_field != NULL)
2611 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002612 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002613 return 1;
2614}
2615
Christopher Fauleta8b76842022-12-16 15:08:36 +01002616/* Dumps a line for server <sv> and proxy <px> to the local trash vbuffer and
2617 * uses the state from stream connector <sc>, and server state <state>. The
2618 * caller is responsible for clearing the local trash buffer if needed. Returns
2619 * non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002620 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002621static int stats_dump_sv_stats(struct stconn *sc, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002622{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002623 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002624 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002625 struct stats_module *mod;
2626 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2627 size_t stats_count = ST_F_TOTAL_FIELDS;
2628
2629 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002630
Willy Tarreau91cefca2022-05-03 17:08:29 +02002631 if (!stats_fill_sv_stats(px, sv, ctx->flags, stats,
William Dauchyd3a9a492021-01-25 17:29:03 +01002632 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002633 return 0;
2634
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002635 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2636 void *counters;
2637
2638 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2639 continue;
2640
2641 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2642 stats_count += mod->stats_count;
2643 continue;
2644 }
2645
2646 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2647 mod->fill_stats(counters, stats + stats_count);
2648 stats_count += mod->stats_count;
2649 }
2650
2651 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002652}
2653
William Dauchyda3b4662021-01-25 17:29:01 +01002654/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002655 */
William Dauchyda3b4662021-01-25 17:29:01 +01002656static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002657{
William Dauchyda3b4662021-01-25 17:29:01 +01002658 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002659 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002660
William Dauchyda3b4662021-01-25 17:29:01 +01002661 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002662 for (srv = px->srv; srv; srv = srv->next) {
2663 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002664 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002665 if (srv_currently_usable(srv) &&
2666 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002667 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002668 }
William Dauchyda3b4662021-01-25 17:29:01 +01002669 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002670 }
2671
Willy Tarreaubd715102020-10-23 22:44:30 +02002672 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2673 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002674 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002675 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2676
William Dauchyda3b4662021-01-25 17:29:01 +01002677 /* use tmp variable then assign result to make gcc happy */
2678 *nbup = nbup_tmp;
2679 *nbsrv = nbsrv_tmp;
2680 *totuw = totuw_tmp;
2681}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002682
William Dauchyda3b4662021-01-25 17:29:01 +01002683/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2684 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2685 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2686 * this value, or if the selected field is not implemented for backends, the
2687 * function returns 0, otherwise, it returns 1. <flags> can take the value
2688 * STAT_SHLGNDS.
2689 */
2690int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2691 enum stat_field *selected_field)
2692{
2693 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2694 long long be_samples_counter;
2695 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2696 struct buffer *out = get_trash_chunk();
2697 int nbup, nbsrv, totuw;
2698 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002699
William Dauchyda3b4662021-01-25 17:29:01 +01002700 if (len < ST_F_TOTAL_FIELDS)
2701 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002702
William Dauchyda3b4662021-01-25 17:29:01 +01002703 nbup = nbsrv = totuw = 0;
2704 /* some srv values compute for later if we either select all fields or
2705 * need them for one of the mentioned ones */
2706 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2707 *selected_field == ST_F_UWEIGHT)
2708 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002709
William Dauchyda3b4662021-01-25 17:29:01 +01002710 /* same here but specific to time fields */
2711 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2712 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2713 *selected_field == ST_F_TTIME) {
2714 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2715 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2716 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002717 }
2718
William Dauchyda3b4662021-01-25 17:29:01 +01002719 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2720 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002721
William Dauchyda3b4662021-01-25 17:29:01 +01002722 switch (current_field) {
2723 case ST_F_PXNAME:
2724 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2725 break;
2726 case ST_F_SVNAME:
2727 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2728 break;
2729 case ST_F_MODE:
2730 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2731 break;
2732 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002733 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002734 break;
2735 case ST_F_QMAX:
2736 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2737 break;
2738 case ST_F_SCUR:
2739 metric = mkf_u32(0, px->beconn);
2740 break;
2741 case ST_F_SMAX:
2742 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2743 break;
2744 case ST_F_SLIM:
2745 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2746 break;
2747 case ST_F_STOT:
2748 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2749 break;
2750 case ST_F_BIN:
2751 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2752 break;
2753 case ST_F_BOUT:
2754 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2755 break;
2756 case ST_F_DREQ:
2757 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2758 break;
2759 case ST_F_DRESP:
2760 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2761 break;
2762 case ST_F_ECON:
2763 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2764 break;
2765 case ST_F_ERESP:
2766 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2767 break;
2768 case ST_F_WRETR:
2769 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2770 break;
2771 case ST_F_WREDIS:
2772 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2773 break;
2774 case ST_F_WREW:
2775 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2776 break;
2777 case ST_F_EINT:
2778 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2779 break;
2780 case ST_F_CONNECT:
2781 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2782 break;
2783 case ST_F_REUSE:
2784 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2785 break;
2786 case ST_F_STATUS:
2787 fld = chunk_newstr(out);
2788 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2789 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2790 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2791 metric = mkf_str(FO_STATUS, fld);
2792 break;
Cedric Paillet7d6644e2022-12-08 09:17:00 +00002793 case ST_F_AGG_SRV_CHECK_STATUS: // DEPRECATED
2794 case ST_F_AGG_SRV_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +01002795 metric = mkf_u32(FN_GAUGE, 0);
2796 break;
Cedric Paillete06e31e2022-12-08 09:17:01 +00002797 case ST_F_AGG_CHECK_STATUS:
2798 metric = mkf_u32(FN_GAUGE, 0);
2799 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002800 case ST_F_WEIGHT:
2801 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2802 break;
2803 case ST_F_UWEIGHT:
2804 metric = mkf_u32(FN_AVG, totuw);
2805 break;
2806 case ST_F_ACT:
2807 metric = mkf_u32(0, px->srv_act);
2808 break;
2809 case ST_F_BCK:
2810 metric = mkf_u32(0, px->srv_bck);
2811 break;
2812 case ST_F_CHKDOWN:
2813 metric = mkf_u64(FN_COUNTER, px->down_trans);
2814 break;
2815 case ST_F_LASTCHG:
Willy Tarreau69530f52023-04-28 09:16:15 +02002816 metric = mkf_u32(FN_AGE, ns_to_sec(now_ns) - px->last_change);
William Dauchyda3b4662021-01-25 17:29:01 +01002817 break;
2818 case ST_F_DOWNTIME:
2819 if (px->srv)
2820 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2821 break;
2822 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002823 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002824 break;
2825 case ST_F_IID:
2826 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2827 break;
2828 case ST_F_SID:
2829 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2830 break;
2831 case ST_F_LBTOT:
2832 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2833 break;
2834 case ST_F_TYPE:
2835 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2836 break;
2837 case ST_F_RATE:
2838 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2839 break;
2840 case ST_F_RATE_MAX:
2841 metric = mkf_u32(0, px->be_counters.sps_max);
2842 break;
2843 case ST_F_COOKIE:
2844 if (flags & STAT_SHLGNDS && px->cookie_name)
2845 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2846 break;
2847 case ST_F_ALGO:
2848 if (flags & STAT_SHLGNDS)
2849 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2850 break;
2851 case ST_F_REQ_TOT:
2852 if (px->mode == PR_MODE_HTTP)
2853 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2854 break;
2855 case ST_F_HRSP_1XX:
2856 if (px->mode == PR_MODE_HTTP)
2857 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2858 break;
2859 case ST_F_HRSP_2XX:
2860 if (px->mode == PR_MODE_HTTP)
2861 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2862 break;
2863 case ST_F_HRSP_3XX:
2864 if (px->mode == PR_MODE_HTTP)
2865 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2866 break;
2867 case ST_F_HRSP_4XX:
2868 if (px->mode == PR_MODE_HTTP)
2869 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2870 break;
2871 case ST_F_HRSP_5XX:
2872 if (px->mode == PR_MODE_HTTP)
2873 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2874 break;
2875 case ST_F_HRSP_OTHER:
2876 if (px->mode == PR_MODE_HTTP)
2877 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2878 break;
2879 case ST_F_CACHE_LOOKUPS:
2880 if (px->mode == PR_MODE_HTTP)
2881 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2882 break;
2883 case ST_F_CACHE_HITS:
2884 if (px->mode == PR_MODE_HTTP)
2885 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2886 break;
2887 case ST_F_CLI_ABRT:
2888 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2889 break;
2890 case ST_F_SRV_ABRT:
2891 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2892 break;
2893 case ST_F_COMP_IN:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002894 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002895 break;
2896 case ST_F_COMP_OUT:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002897 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002898 break;
2899 case ST_F_COMP_BYP:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002900 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002901 break;
2902 case ST_F_COMP_RSP:
2903 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2904 break;
2905 case ST_F_LASTSESS:
2906 metric = mkf_s32(FN_AGE, be_lastsession(px));
2907 break;
2908 case ST_F_QTIME:
2909 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2910 break;
2911 case ST_F_CTIME:
2912 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2913 break;
2914 case ST_F_RTIME:
2915 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2916 break;
2917 case ST_F_TTIME:
2918 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2919 break;
2920 case ST_F_QT_MAX:
2921 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2922 break;
2923 case ST_F_CT_MAX:
2924 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2925 break;
2926 case ST_F_RT_MAX:
2927 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2928 break;
2929 case ST_F_TT_MAX:
2930 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2931 break;
2932 default:
2933 /* not used for backends. If a specific metric
2934 * is requested, return an error. Otherwise continue.
2935 */
2936 if (selected_field != NULL)
2937 return 0;
2938 continue;
2939 }
2940 stats[current_field] = metric;
2941 if (selected_field != NULL)
2942 break;
2943 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002944 return 1;
2945}
2946
Christopher Fauleta8b76842022-12-16 15:08:36 +01002947/* Dumps a line for backend <px> to the local trash buffer for and uses the
2948 * state from stream interface <si>. The caller is responsible for clearing the
2949 * local trash buffer if needed. Returns non-zero if it emits anything, zero
2950 * otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002951 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002952static int stats_dump_be_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002953{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002954 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002955 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002956 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2957 struct stats_module *mod;
2958 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002959
2960 if (!(px->cap & PR_CAP_BE))
2961 return 0;
2962
Willy Tarreau91cefca2022-05-03 17:08:29 +02002963 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_BE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01002964 return 0;
2965
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002966 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2967
Willy Tarreau91cefca2022-05-03 17:08:29 +02002968 if (!stats_fill_be_stats(px, ctx->flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002969 return 0;
2970
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002971 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2972 struct extra_counters *counters;
2973
2974 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2975 continue;
2976
2977 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2978 stats_count += mod->stats_count;
2979 continue;
2980 }
2981
2982 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2983 mod->fill_stats(counters, stats + stats_count);
2984 stats_count += mod->stats_count;
2985 }
2986
2987 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002988}
2989
Christopher Fauleta8b76842022-12-16 15:08:36 +01002990/* Dumps the HTML table header for proxy <px> to the local trash buffer for and
2991 * uses the state from stream connector <sc> and per-uri parameters <uri>. The
2992 * caller is responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01002993 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002994static void stats_dump_html_px_hdr(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002995{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002996 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002997 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01002998 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02002999 struct stats_module *mod;
3000 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003001
Willy Tarreau91cefca2022-05-03 17:08:29 +02003002 if (px->cap & PR_CAP_BE && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003003 /* A form to enable/disable this proxy servers */
3004
Willy Tarreau91cefca2022-05-03 17:08:29 +02003005 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003006 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003007 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003008 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003009
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003010 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02003011 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3012 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003013 }
3014
Christopher Fauleta8b76842022-12-16 15:08:36 +01003015 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003016 "<form method=\"post\">");
3017 }
3018
3019 /* print a new table */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003020 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003021 "<table class=\"tbl\" width=\"100%%\">\n"
3022 "<tr class=\"titre\">"
3023 "<th class=\"pxname\" width=\"10%%\">");
3024
Christopher Fauleta8b76842022-12-16 15:08:36 +01003025 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003026 "<a name=\"%s\"></a>%s"
3027 "<a class=px href=\"#%s\">%s</a>",
3028 px->id,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003029 (ctx->flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01003030 px->id, px->id);
3031
Willy Tarreau91cefca2022-05-03 17:08:29 +02003032 if (ctx->flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003033 /* cap, mode, id */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003034 chunk_appendf(&trash_chunk, "<div class=tips>cap: %s, mode: %s, id: %d",
William Lallemand74c24fb2016-11-21 17:18:36 +01003035 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3036 px->uuid);
Christopher Fauleta8b76842022-12-16 15:08:36 +01003037 chunk_appendf(&trash_chunk, "</div>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003038 }
3039
Christopher Fauleta8b76842022-12-16 15:08:36 +01003040 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003041 "%s</th>"
3042 "<th class=\"%s\" width=\"90%%\">%s</th>"
3043 "</tr>\n"
3044 "</table>\n"
3045 "<table class=\"tbl\" width=\"100%%\">\n"
3046 "<tr class=\"titre\">",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003047 (ctx->flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01003048 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3049
Willy Tarreau91cefca2022-05-03 17:08:29 +02003050 if (ctx->flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003051 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01003052 if ((px->cap & PR_CAP_BE) && px->srv)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003053 chunk_appendf(&trash_chunk,
Christopher Fauletbc271ec2019-12-02 11:29:04 +01003054 "<th rowspan=2 width=1><input type=\"checkbox\" "
3055 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
3056 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
3057 px->id,
3058 px->id);
3059 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003060 chunk_appendf(&trash_chunk, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003061 }
3062
Christopher Fauleta8b76842022-12-16 15:08:36 +01003063 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003064 "<th rowspan=2></th>"
3065 "<th colspan=3>Queue</th>"
3066 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
3067 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3068 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003069 "<th colspan=9>Server</th>");
3070
Willy Tarreau91cefca2022-05-03 17:08:29 +02003071 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003072 // calculate the count of module for colspan attribute
3073 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
3074 ++stats_module_len;
3075 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003076 chunk_appendf(&trash_chunk, "<th colspan=%d>Extra modules</th>",
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003077 stats_module_len);
3078 }
3079
Christopher Fauleta8b76842022-12-16 15:08:36 +01003080 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003081 "</tr>\n"
3082 "<tr class=\"titre\">"
3083 "<th>Cur</th><th>Max</th><th>Limit</th>"
3084 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
3085 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
3086 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3087 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3088 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3089 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003090 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003091
Willy Tarreau91cefca2022-05-03 17:08:29 +02003092 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003093 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Christopher Fauleta8b76842022-12-16 15:08:36 +01003094 chunk_appendf(&trash_chunk, "<th>%s</th>", mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003095 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003096 }
3097
Christopher Fauleta8b76842022-12-16 15:08:36 +01003098 chunk_appendf(&trash_chunk, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003099}
3100
Christopher Fauleta8b76842022-12-16 15:08:36 +01003101/* Dumps the HTML table trailer for proxy <px> to the local trash buffer for and
3102 * uses the state from stream connector <sc>. The caller is responsible for
3103 * clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003104 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003105static void stats_dump_html_px_end(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01003106{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003107 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003108 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet693b23b2022-02-28 09:09:05 +01003109
Christopher Fauleta8b76842022-12-16 15:08:36 +01003110 chunk_appendf(&trash_chunk, "</table>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003111
Willy Tarreau91cefca2022-05-03 17:08:29 +02003112 if ((px->cap & PR_CAP_BE) && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003113 /* close the form used to enable/disable this proxy servers */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003114 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003115 "Choose the action to perform on the checked servers : "
3116 "<select name=action>"
3117 "<option value=\"\"></option>"
3118 "<option value=\"ready\">Set state to READY</option>"
3119 "<option value=\"drain\">Set state to DRAIN</option>"
3120 "<option value=\"maint\">Set state to MAINT</option>"
3121 "<option value=\"dhlth\">Health: disable checks</option>"
3122 "<option value=\"ehlth\">Health: enable checks</option>"
3123 "<option value=\"hrunn\">Health: force UP</option>"
3124 "<option value=\"hnolb\">Health: force NOLB</option>"
3125 "<option value=\"hdown\">Health: force DOWN</option>"
3126 "<option value=\"dagent\">Agent: disable checks</option>"
3127 "<option value=\"eagent\">Agent: enable checks</option>"
3128 "<option value=\"arunn\">Agent: force UP</option>"
3129 "<option value=\"adown\">Agent: force DOWN</option>"
3130 "<option value=\"shutdown\">Kill Sessions</option>"
3131 "</select>"
3132 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3133 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3134 "</form>",
3135 px->uuid);
3136 }
3137
Christopher Fauleta8b76842022-12-16 15:08:36 +01003138 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003139}
3140
3141/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003142 * Dumps statistics for a proxy. The output is sent to the stream connector's
William Lallemand74c24fb2016-11-21 17:18:36 +01003143 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3144 * buffer space, or non-zero if everything completed. This function is used
3145 * both by the CLI and the HTTP entry points, and is able to dump the output
3146 * in HTML or CSV formats. If the later, <uri> must be NULL.
3147 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003148int stats_dump_proxy_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003149 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003150{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003151 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003152 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003153 struct stream *s = __sc_strm(sc);
3154 struct channel *rep = sc_ic(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +01003155 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3156 struct listener *l;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003157 int current_field;
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003158 int px_st = ctx->px_st;
William Lallemand74c24fb2016-11-21 17:18:36 +01003159
Christopher Fauleta8b76842022-12-16 15:08:36 +01003160 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003161more:
3162 current_field = ctx->field;
William Lallemand74c24fb2016-11-21 17:18:36 +01003163
Willy Tarreau91cefca2022-05-03 17:08:29 +02003164 switch (ctx->px_st) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003165 case STAT_PX_ST_INIT:
3166 /* we are on a new proxy */
3167 if (uri && uri->scope) {
3168 /* we have a limited scope, we have to check the proxy name */
3169 struct stat_scope *scope;
3170 int len;
3171
3172 len = strlen(px->id);
3173 scope = uri->scope;
3174
3175 while (scope) {
3176 /* match exact proxy name */
3177 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3178 break;
3179
3180 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003181 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003182 break;
3183 scope = scope->next;
3184 }
3185
3186 /* proxy name not found : don't dump anything */
3187 if (scope == NULL)
3188 return 1;
3189 }
3190
3191 /* if the user has requested a limited output and the proxy
3192 * name does not match, skip it.
3193 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003194 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003195 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003196
Willy Tarreau91cefca2022-05-03 17:08:29 +02003197 if (strnistr(px->id, strlen(px->id), scope_ptr, ctx->scope_len) == NULL)
Christopher Fauleted7a0662019-01-14 11:07:34 +01003198 return 1;
3199 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003200
Willy Tarreau91cefca2022-05-03 17:08:29 +02003201 if ((ctx->flags & STAT_BOUND) &&
3202 (ctx->iid != -1) &&
3203 (px->uuid != ctx->iid))
William Lallemand74c24fb2016-11-21 17:18:36 +01003204 return 1;
3205
Willy Tarreau91cefca2022-05-03 17:08:29 +02003206 ctx->px_st = STAT_PX_ST_TH;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003207 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003208
3209 case STAT_PX_ST_TH:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003210 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003211 stats_dump_html_px_hdr(sc, px);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003212 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003213 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003214 }
3215
Willy Tarreau91cefca2022-05-03 17:08:29 +02003216 ctx->px_st = STAT_PX_ST_FE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003217 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003218
3219 case STAT_PX_ST_FE:
3220 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003221 if (stats_dump_fe_stats(sc, px)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003222 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003223 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003224 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003225 if (ctx->field)
3226 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003227 }
3228
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003229 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003230 ctx->obj2 = px->conf.listeners.n;
3231 ctx->px_st = STAT_PX_ST_LI;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003232 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003233
3234 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003235 /* obj2 points to listeners list as initialized above */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003236 for (; ctx->obj2 != &px->conf.listeners; ctx->obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003237 if (htx) {
Christopher Fauleta236c582023-05-09 16:31:01 +02003238 if (htx_almost_full(htx)) {
3239 sc_need_room(sc, htx->size / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003240 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003241 }
Christopher Fauletef779222018-10-31 08:47:01 +01003242 }
3243 else {
Christopher Fauleta236c582023-05-09 16:31:01 +02003244 if (buffer_almost_full(&rep->buf)) {
3245 sc_need_room(sc, b_size(&rep->buf) / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003246 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003247 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003248 }
3249
Willy Tarreau91cefca2022-05-03 17:08:29 +02003250 l = LIST_ELEM(ctx->obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003251 if (!l->counters)
3252 continue;
3253
Willy Tarreau91cefca2022-05-03 17:08:29 +02003254 if (ctx->flags & STAT_BOUND) {
3255 if (!(ctx->type & (1 << STATS_TYPE_SO)))
William Lallemand74c24fb2016-11-21 17:18:36 +01003256 break;
3257
Willy Tarreau91cefca2022-05-03 17:08:29 +02003258 if (ctx->sid != -1 && l->luid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003259 continue;
3260 }
3261
3262 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003263 if (stats_dump_li_stats(sc, px, l)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003264 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003265 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003266 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003267 if (ctx->field)
3268 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003269 }
Aurelien DARRAGON9b07d4f2023-02-03 11:43:05 +01003270 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003271 }
3272
Willy Tarreau91cefca2022-05-03 17:08:29 +02003273 ctx->obj2 = px->srv; /* may be NULL */
3274 ctx->px_st = STAT_PX_ST_SV;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003275 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003276
3277 case STAT_PX_ST_SV:
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003278 /* check for dump resumption */
3279 if (px_st == STAT_PX_ST_SV) {
3280 struct server *cur = ctx->obj2;
3281
3282 /* re-entrant dump */
3283 BUG_ON(!cur);
3284 if (cur->flags & SRV_F_DELETED) {
3285 /* the server could have been marked as deleted
3286 * between two dumping attempts, skip it.
3287 */
3288 cur = cur->next;
3289 }
3290 srv_drop(ctx->obj2); /* drop old srv taken on last dumping attempt */
3291 ctx->obj2 = cur; /* could be NULL */
3292 /* back to normal */
3293 }
3294
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003295 /* obj2 points to servers list as initialized above.
3296 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003297 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003298 * Temporarily increment its refcount to prevent its
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003299 * anticipated cleaning. Call srv_drop() to release it.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003300 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003301 for (; ctx->obj2 != NULL;
3302 ctx->obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003303
Willy Tarreau91cefca2022-05-03 17:08:29 +02003304 sv = ctx->obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003305 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003306
Christopher Fauletef779222018-10-31 08:47:01 +01003307 if (htx) {
Christopher Fauleta236c582023-05-09 16:31:01 +02003308 if (htx_almost_full(htx)) {
3309 sc_need_room(sc, htx->size / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003310 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003311 }
Christopher Fauletef779222018-10-31 08:47:01 +01003312 }
3313 else {
Christopher Fauleta236c582023-05-09 16:31:01 +02003314 if (buffer_almost_full(&rep->buf)) {
3315 sc_need_room(sc, b_size(&rep->buf) / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003316 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003317 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003318 }
3319
Willy Tarreau91cefca2022-05-03 17:08:29 +02003320 if (ctx->flags & STAT_BOUND) {
3321 if (!(ctx->type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003322 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003323 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003324 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003325
Willy Tarreau91cefca2022-05-03 17:08:29 +02003326 if (ctx->sid != -1 && sv->puid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003327 continue;
3328 }
3329
Willy Tarreau3e320362020-10-23 17:28:57 +02003330 /* do not report disabled servers */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003331 if (ctx->flags & STAT_HIDE_MAINT &&
Willy Tarreau3e320362020-10-23 17:28:57 +02003332 sv->cur_admin & SRV_ADMF_MAINT) {
3333 continue;
3334 }
3335
William Lallemand74c24fb2016-11-21 17:18:36 +01003336 svs = sv;
3337 while (svs->track)
3338 svs = svs->track;
3339
3340 /* do not report servers which are DOWN and not changing state */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003341 if ((ctx->flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003342 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3343 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003344 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3345 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3346 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3347 continue;
3348 }
3349
Willy Tarreaucaff6312022-05-27 10:17:46 +02003350 if (stats_dump_sv_stats(sc, px, sv)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003351 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003352 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003353 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003354 if (ctx->field)
3355 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003356 }
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003357 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003358 } /* for sv */
3359
Willy Tarreau91cefca2022-05-03 17:08:29 +02003360 ctx->px_st = STAT_PX_ST_BE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003361 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003362
3363 case STAT_PX_ST_BE:
3364 /* print the backend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003365 if (stats_dump_be_stats(sc, px)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003366 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003367 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003368 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003369 if (ctx->field)
3370 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003371 }
3372
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003373 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003374 ctx->px_st = STAT_PX_ST_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003375 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003376
3377 case STAT_PX_ST_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003378 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003379 stats_dump_html_px_end(sc, px);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003380 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003381 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003382 }
3383
Willy Tarreau91cefca2022-05-03 17:08:29 +02003384 ctx->px_st = STAT_PX_ST_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003385 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003386
3387 case STAT_PX_ST_FIN:
3388 return 1;
3389
3390 default:
3391 /* unknown state, we should put an abort() here ! */
3392 return 1;
3393 }
Christopher Fauletef779222018-10-31 08:47:01 +01003394
3395 full:
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003396 /* restore previous field */
3397 ctx->field = current_field;
Christopher Fauletef779222018-10-31 08:47:01 +01003398 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003399}
3400
Christopher Fauleta8b76842022-12-16 15:08:36 +01003401/* Dumps the HTTP stats head block to the local trash buffer for and uses the
3402 * per-uri parameters <uri>. The caller is responsible for clearing the local
3403 * trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003404 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003405static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003406{
Willy Tarreau91cefca2022-05-03 17:08:29 +02003407 struct show_stat_ctx *ctx = appctx->svcctx;
3408
William Lallemand74c24fb2016-11-21 17:18:36 +01003409 /* WARNING! This must fit in the first buffer !!! */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003410 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003411 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3412 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3413 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003414 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003415 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3416 "<style type=\"text/css\"><!--\n"
3417 "body {"
3418 " font-family: arial, helvetica, sans-serif;"
3419 " font-size: 12px;"
3420 " font-weight: normal;"
3421 " color: black;"
3422 " background: white;"
3423 "}\n"
3424 "th,td {"
3425 " font-size: 10px;"
3426 "}\n"
3427 "h1 {"
3428 " font-size: x-large;"
3429 " margin-bottom: 0.5em;"
3430 "}\n"
3431 "h2 {"
3432 " font-family: helvetica, arial;"
3433 " font-size: x-large;"
3434 " font-weight: bold;"
3435 " font-style: italic;"
3436 " color: #6020a0;"
3437 " margin-top: 0em;"
3438 " margin-bottom: 0em;"
3439 "}\n"
3440 "h3 {"
3441 " font-family: helvetica, arial;"
3442 " font-size: 16px;"
3443 " font-weight: bold;"
3444 " color: #b00040;"
3445 " background: #e8e8d0;"
3446 " margin-top: 0em;"
3447 " margin-bottom: 0em;"
3448 "}\n"
3449 "li {"
3450 " margin-top: 0.25em;"
3451 " margin-right: 2em;"
3452 "}\n"
3453 ".hr {margin-top: 0.25em;"
3454 " border-color: black;"
3455 " border-bottom-style: solid;"
3456 "}\n"
3457 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3458 ".total {background: #20D0D0;color: #ffff80;}\n"
3459 ".frontend {background: #e8e8d0;}\n"
3460 ".socket {background: #d0d0d0;}\n"
3461 ".backend {background: #e8e8d0;}\n"
3462 ".active_down {background: #ff9090;}\n"
3463 ".active_going_up {background: #ffd020;}\n"
3464 ".active_going_down {background: #ffffa0;}\n"
3465 ".active_up {background: #c0ffc0;}\n"
3466 ".active_nolb {background: #20a0ff;}\n"
3467 ".active_draining {background: #20a0FF;}\n"
3468 ".active_no_check {background: #e0e0e0;}\n"
3469 ".backup_down {background: #ff9090;}\n"
3470 ".backup_going_up {background: #ff80ff;}\n"
3471 ".backup_going_down {background: #c060ff;}\n"
3472 ".backup_up {background: #b0d0ff;}\n"
3473 ".backup_nolb {background: #90b0e0;}\n"
3474 ".backup_draining {background: #cc9900;}\n"
3475 ".backup_no_check {background: #e0e0e0;}\n"
3476 ".maintain {background: #c07820;}\n"
3477 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3478 "\n"
3479 "a.px:link {color: #ffff40; text-decoration: none;}"
3480 "a.px:visited {color: #ffff40; text-decoration: none;}"
3481 "a.px:hover {color: #ffffff; text-decoration: none;}"
3482 "a.lfsb:link {color: #000000; text-decoration: none;}"
3483 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3484 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3485 "\n"
3486 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3487 "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"
3488 "table.tbl td.ac { text-align: center;}\n"
3489 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3490 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3491 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3492 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3493 "\n"
3494 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3495 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3496 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3497 "table.det { border-collapse: collapse; border-style: none; }\n"
3498 "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"
3499 "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"
3500 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003501 "div.tips {\n"
3502 " display:block;\n"
3503 " visibility:hidden;\n"
3504 " z-index:2147483647;\n"
3505 " position:absolute;\n"
3506 " padding:2px 4px 3px;\n"
3507 " background:#f0f060; color:#000000;\n"
3508 " border:1px solid #7040c0;\n"
3509 " white-space:nowrap;\n"
3510 " font-style:normal;font-size:11px;font-weight:normal;\n"
3511 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3512 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3513 "}\n"
3514 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003515 "@media (prefers-color-scheme: dark) {\n"
3516 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3517 " h1 { color: #a265e0!important; }\n"
3518 " h2 { color: #a265e0; }\n"
3519 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3520 " a { color: #3391ff; }\n"
3521 " input { background-color: #2f3437; }\n"
3522 " .hr { border-color: #8c8273; }\n"
3523 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3524 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003525 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003526 " .backend {background: #2f3437;}\n"
3527 " .active_down {background: #760000;}\n"
3528 " .active_going_up {background: #b99200;}\n"
3529 " .active_going_down {background: #6c6c00;}\n"
3530 " .active_up {background: #165900;}\n"
3531 " .active_nolb {background: #006ab9;}\n"
3532 " .active_draining {background: #006ab9;}\n"
3533 " .active_no_check {background: #2a2d2f;}\n"
3534 " .backup_down {background: #760000;}\n"
3535 " .backup_going_up {background: #7f007f;}\n"
3536 " .backup_going_down {background: #580092;}\n"
3537 " .backup_up {background: #2e3234;}\n"
3538 " .backup_nolb {background: #1e3c6a;}\n"
3539 " .backup_draining {background: #a37a00;}\n"
3540 " .backup_no_check {background: #2a2d2f;}\n"
3541 " .maintain {background: #9a601a;}\n"
3542 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3543 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3544 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3545 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3546 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3547 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3548 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003549 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003550 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3551 " table.tbl th { border-color: #808080; }\n"
3552 " table.tbl td { border-color: #808080; }\n"
3553 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3554 " div.tips {\n"
3555 " background:#8e8e0d;\n"
3556 " color:#e8e6e3;\n"
3557 " border-color: #4e2c86;\n"
3558 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3559 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3560 " box-shadow: #60686c 2px 2px 3px;\n"
3561 " }\n"
3562 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003563 "-->\n"
3564 "</style></head>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003565 (ctx->flags & STAT_SHNODE) ? " on " : "",
3566 (ctx->flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003567 );
3568}
3569
Christopher Fauleta8b76842022-12-16 15:08:36 +01003570/* Dumps the HTML stats information block to the local trash buffer for and uses
3571 * the state from stream connector <sc> and per-uri parameters <uri>. The caller
3572 * is responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003573 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003574static void stats_dump_html_info(struct stconn *sc, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003575{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003576 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003577 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc05d30e2023-04-28 14:50:29 +02003578 unsigned int up = ns_to_sec(now_ns - start_time_ns);
William Lallemand74c24fb2016-11-21 17:18:36 +01003579 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02003580 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Willy Tarreau6be8d092023-01-12 16:08:41 +01003581 unsigned long long bps;
3582 int thr;
3583
3584 for (bps = thr = 0; thr < global.nbthread; thr++)
3585 bps += 32ULL * read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
Willy Tarreau1713c032019-05-23 12:23:55 +02003586
3587 /* Turn the bytes per second to bits per second and take care of the
3588 * usual ethernet overhead in order to help figure how far we are from
3589 * interface saturation since it's the only case which usually matters.
3590 * For this we count the total size of an Ethernet frame on the wire
3591 * including preamble and IFG (1538) for the largest TCP segment it
3592 * transports (1448 with TCP timestamps). This is not valid for smaller
3593 * packets (under-estimated), but it gives a reasonably accurate
3594 * estimation of how far we are from uplink saturation.
3595 */
3596 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003597
3598 /* WARNING! this has to fit the first packet too.
3599 * We are around 3.5 kB, add adding entries will
3600 * become tricky if we want to support 4kB buffers !
3601 */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003602 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003603 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3604 PRODUCT_NAME "%s</a></h1>\n"
3605 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3606 "<hr width=\"100%%\" class=\"hr\">\n"
3607 "<h3>&gt; General process information</h3>\n"
3608 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003609 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
Willy Tarreau3c4a2972023-05-11 12:02:21 +02003610 "<b>uptime = </b> %dd %dh%02dm%02ds; warnings = %u<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003611 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
Willy Tarreau96151022023-05-11 13:51:31 +02003612 "<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 +02003613 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003614 "Running tasks: %d/%d; idle = %d %%<br>\n"
3615 "</td><td align=\"center\" nowrap>\n"
3616 "<table class=\"lgd\"><tr>\n"
3617 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3618 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3619 "</tr><tr>\n"
3620 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3621 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3622 "</tr><tr>\n"
3623 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3624 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3625 "</tr><tr>\n"
3626 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3627 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3628 "</tr><tr>\n"
3629 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3630 "</tr><tr>\n"
3631 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3632 "</tr></table>\n"
3633 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3634 "</td>"
3635 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3636 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3637 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003638 (ctx->flags & STAT_HIDEVER) ? "" : (stats_version_string),
3639 pid, (ctx->flags & STAT_SHNODE) ? " on " : "",
3640 (ctx->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3641 (ctx->flags & STAT_SHDESC) ? ": " : "",
3642 (ctx->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003643 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003644 up / 86400, (up % 86400) / 3600,
3645 (up % 3600) / 60, (up % 60),
Willy Tarreau3c4a2972023-05-11 12:02:21 +02003646 HA_ATOMIC_LOAD(&tot_warnings),
William Lallemand74c24fb2016-11-21 17:18:36 +01003647 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3648 global.rlimit_memmax ? " MB" : "",
3649 global.rlimit_nofile,
Willy Tarreau96151022023-05-11 13:51:31 +02003650 global.maxsock, global.maxconn, HA_ATOMIC_LOAD(&maxconn_reached), global.maxpipes,
William Lallemand74c24fb2016-11-21 17:18:36 +01003651 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003652 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3653 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003654 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003655 );
3656
Willy Tarreau91cefca2022-05-03 17:08:29 +02003657 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3658 memcpy(scope_txt, scope_ptr, ctx->scope_len);
3659 scope_txt[ctx->scope_len] = '\0';
William Lallemand74c24fb2016-11-21 17:18:36 +01003660
Christopher Fauleta8b76842022-12-16 15:08:36 +01003661 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003662 "<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 +02003663 (ctx->scope_len > 0) ? scope_txt : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003664 STAT_SCOPE_TXT_MAXLEN);
3665
Willy Tarreau91cefca2022-05-03 17:08:29 +02003666 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003667 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003668 if (ctx->scope_len) {
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003669 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02003670 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3671 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003672 }
3673
Willy Tarreau91cefca2022-05-03 17:08:29 +02003674 if (ctx->flags & STAT_HIDE_DOWN)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003675 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003676 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3677 uri->uri_prefix,
3678 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003679 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003680 scope_txt);
3681 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003682 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003683 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3684 uri->uri_prefix,
3685 ";up",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003686 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003687 scope_txt);
3688
3689 if (uri->refresh > 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003690 if (ctx->flags & STAT_NO_REFRESH)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003691 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003692 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3693 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003694 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003695 "",
3696 scope_txt);
3697 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003698 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003699 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3700 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003701 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003702 ";norefresh",
3703 scope_txt);
3704 }
3705
Christopher Fauleta8b76842022-12-16 15:08:36 +01003706 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003707 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3708 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003709 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3710 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003711 scope_txt);
3712
Christopher Fauleta8b76842022-12-16 15:08:36 +01003713 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003714 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3715 uri->uri_prefix,
3716 (uri->refresh > 0) ? ";norefresh" : "",
3717 scope_txt);
3718
Christopher Fauleta8b76842022-12-16 15:08:36 +01003719 chunk_appendf(&trash_chunk,
Christopher Faulet6338a082019-09-09 15:50:54 +02003720 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3721 uri->uri_prefix,
3722 (uri->refresh > 0) ? ";norefresh" : "",
3723 scope_txt, uri->uri_prefix);
3724
Christopher Fauleta8b76842022-12-16 15:08:36 +01003725 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003726 "</ul></td>"
3727 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3728 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3729 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3730 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3731 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3732 "</ul>"
3733 "</td>"
3734 "</tr></table>\n"
3735 ""
3736 );
3737
Willy Tarreau91cefca2022-05-03 17:08:29 +02003738 if (ctx->st_code) {
3739 switch (ctx->st_code) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003740 case STAT_STATUS_DONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003741 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003742 "<p><div class=active_up>"
3743 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3744 "Action processed successfully."
3745 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003746 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3747 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003748 scope_txt);
3749 break;
3750 case STAT_STATUS_NONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003751 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003752 "<p><div class=active_going_down>"
3753 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3754 "Nothing has changed."
3755 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003756 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3757 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003758 scope_txt);
3759 break;
3760 case STAT_STATUS_PART:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003761 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003762 "<p><div class=active_going_down>"
3763 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3764 "Action partially processed.<br>"
3765 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3766 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003767 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3768 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003769 scope_txt);
3770 break;
3771 case STAT_STATUS_ERRP:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003772 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003773 "<p><div class=active_down>"
3774 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3775 "Action not processed because of invalid parameters."
3776 "<ul>"
3777 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003778 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003779 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3780 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3781 "</ul>"
3782 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003783 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3784 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003785 scope_txt);
3786 break;
3787 case STAT_STATUS_EXCD:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003788 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003789 "<p><div class=active_down>"
3790 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3791 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3792 "You should retry with less servers at a time.</b>"
3793 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003794 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3795 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003796 scope_txt);
3797 break;
3798 case STAT_STATUS_DENY:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003799 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003800 "<p><div class=active_down>"
3801 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3802 "<b>Action denied.</b>"
3803 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003804 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3805 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003806 scope_txt);
3807 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003808 case STAT_STATUS_IVAL:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003809 chunk_appendf(&trash_chunk,
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003810 "<p><div class=active_down>"
3811 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3812 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3813 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003814 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3815 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003816 scope_txt);
3817 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003818 default:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003819 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003820 "<p><div class=active_no_check>"
3821 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3822 "Unexpected result."
3823 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003824 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3825 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003826 scope_txt);
3827 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003828 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003829 }
3830}
3831
Christopher Fauleta8b76842022-12-16 15:08:36 +01003832/* Dumps the HTML stats trailer block to the local trash buffer. The caller is
3833 * responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003834 */
3835static void stats_dump_html_end()
3836{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003837 chunk_appendf(&trash_chunk, "</body></html>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003838}
3839
Christopher Fauleta8b76842022-12-16 15:08:36 +01003840/* Dumps the stats JSON header to the local trash buffer buffer which. The
3841 * caller is responsible for clearing it if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003842 */
3843static void stats_dump_json_header()
3844{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003845 chunk_strcat(&trash_chunk, "[");
Simon Horman05ee2132017-01-04 09:37:25 +01003846}
3847
3848
Christopher Fauleta8b76842022-12-16 15:08:36 +01003849/* Dumps the JSON stats trailer block to the local trash buffer. The caller is
3850 * responsible for clearing the local trash buffer if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003851 */
3852static void stats_dump_json_end()
3853{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003854 chunk_strcat(&trash_chunk, "]\n");
Simon Horman05ee2132017-01-04 09:37:25 +01003855}
3856
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003857/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3858 * a pointer to the current server/listener.
3859 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003860static int stats_dump_proxies(struct stconn *sc,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003861 struct htx *htx,
3862 struct uri_auth *uri)
3863{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003864 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003865 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003866 struct channel *rep = sc_ic(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003867 struct proxy *px;
3868
3869 /* dump proxies */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003870 while (ctx->obj1) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003871 if (htx) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003872 if (htx_almost_full(htx)) {
3873 sc_need_room(sc, htx->size / 2);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003874 goto full;
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003875 }
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003876 }
3877 else {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003878 if (buffer_almost_full(&rep->buf)) {
3879 sc_need_room(sc, b_size(&rep->buf) / 2);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003880 goto full;
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003881 }
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003882 }
3883
Willy Tarreau91cefca2022-05-03 17:08:29 +02003884 px = ctx->obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003885 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003886 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003887 * This change allows retrieving stats from "old" proxies after a reload.
3888 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003889 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003890 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003891 if (stats_dump_proxy_to_buffer(sc, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003892 return 0;
3893 }
3894
Willy Tarreau91cefca2022-05-03 17:08:29 +02003895 ctx->obj1 = px->next;
3896 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003897 ctx->field = 0;
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003898 }
3899
3900 return 1;
3901
3902 full:
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003903 return 0;
3904}
3905
Willy Tarreau4596fe22022-05-17 19:07:51 +02003906/* This function dumps statistics onto the stream connector's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003907 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3908 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3909 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3910 * and the stream must be closed, or -1 in case of any error. This function is
3911 * used by both the CLI and the HTTP handlers.
3912 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003913static int stats_dump_stat_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003914 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003915{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003916 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003917 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003918 enum stats_domain domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003919
Christopher Fauleta8b76842022-12-16 15:08:36 +01003920 chunk_reset(&trash_chunk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003921
Willy Tarreau41f88522022-05-03 18:39:27 +02003922 switch (ctx->state) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003923 case STAT_STATE_INIT:
3924 ctx->state = STAT_STATE_HEAD; /* let's start producing data */
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003925 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003926
Willy Tarreau6ef16482022-05-06 18:07:53 +02003927 case STAT_STATE_HEAD:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003928 if (ctx->flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003929 stats_dump_html_head(appctx, uri);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003930 else if (ctx->flags & STAT_JSON_SCHM)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003931 stats_dump_json_schema(&trash_chunk);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003932 else if (ctx->flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003933 stats_dump_json_header();
Willy Tarreau91cefca2022-05-03 17:08:29 +02003934 else if (!(ctx->flags & STAT_FMT_TYPED))
3935 stats_dump_csv_header(ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003936
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003937 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003938 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003939
Willy Tarreau91cefca2022-05-03 17:08:29 +02003940 if (ctx->flags & STAT_JSON_SCHM) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003941 ctx->state = STAT_STATE_FIN;
Christopher Faulet6338a082019-09-09 15:50:54 +02003942 return 1;
3943 }
Willy Tarreau6ef16482022-05-06 18:07:53 +02003944 ctx->state = STAT_STATE_INFO;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003945 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003946
Willy Tarreau6ef16482022-05-06 18:07:53 +02003947 case STAT_STATE_INFO:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003948 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003949 stats_dump_html_info(sc, uri);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003950 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003951 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003952 }
3953
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003954 if (domain == STATS_DOMAIN_PROXY)
Willy Tarreau91cefca2022-05-03 17:08:29 +02003955 ctx->obj1 = proxies_list;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003956
Willy Tarreau91cefca2022-05-03 17:08:29 +02003957 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003958 ctx->field = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003959 ctx->state = STAT_STATE_LIST;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003960 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003961
Willy Tarreau6ef16482022-05-06 18:07:53 +02003962 case STAT_STATE_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003963 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003964 case STATS_DOMAIN_RESOLVERS:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003965 if (!stats_dump_resolvers(sc, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003966 stat_count[domain],
3967 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003968 return 0;
3969 }
3970 break;
3971
3972 case STATS_DOMAIN_PROXY:
3973 default:
3974 /* dump proxies */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003975 if (!stats_dump_proxies(sc, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003976 return 0;
3977 break;
3978 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003979
Willy Tarreau6ef16482022-05-06 18:07:53 +02003980 ctx->state = STAT_STATE_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003981 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003982
Willy Tarreau6ef16482022-05-06 18:07:53 +02003983 case STAT_STATE_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003984 if (ctx->flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3985 if (ctx->flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01003986 stats_dump_html_end();
3987 else
3988 stats_dump_json_end();
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003989 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003990 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003991 }
3992
Willy Tarreau6ef16482022-05-06 18:07:53 +02003993 ctx->state = STAT_STATE_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003994 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003995
Willy Tarreau6ef16482022-05-06 18:07:53 +02003996 case STAT_STATE_FIN:
William Lallemand74c24fb2016-11-21 17:18:36 +01003997 return 1;
3998
3999 default:
4000 /* unknown state ! */
Willy Tarreau6ef16482022-05-06 18:07:53 +02004001 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01004002 return -1;
4003 }
Christopher Fauletef779222018-10-31 08:47:01 +01004004
4005 full:
Christopher Fauletef779222018-10-31 08:47:01 +01004006 return 0;
4007
William Lallemand74c24fb2016-11-21 17:18:36 +01004008}
4009
4010/* We reached the stats page through a POST request. The appctx is
4011 * expected to have already been allocated by the caller.
4012 * Parse the posted data and enable/disable servers if necessary.
4013 * Returns 1 if request was parsed or zero if it needs more data.
4014 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004015static int stats_process_http_post(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01004016{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004017 struct stream *s = __sc_strm(sc);
4018 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004019 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01004020
4021 struct proxy *px = NULL;
4022 struct server *sv = NULL;
4023
4024 char key[LINESIZE];
4025 int action = ST_ADM_ACTION_NONE;
4026 int reprocess = 0;
4027
4028 int total_servers = 0;
4029 int altered_servers = 0;
4030
4031 char *first_param, *cur_param, *next_param, *end_params;
4032 char *st_cur_param = NULL;
4033 char *st_next_param = NULL;
4034
Christopher Fauleta3618372018-12-13 21:59:56 +01004035 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01004036
Christopher Fauletb7f88902019-07-15 21:56:43 +02004037 struct htx *htx = htxbuf(&s->req.buf);
4038 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01004039
Christopher Fauletb7f88902019-07-15 21:56:43 +02004040 /* we need more data */
4041 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
4042 /* check if we can receive more */
4043 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004044 ctx->st_code = STAT_STATUS_EXCD;
Christopher Fauletb7f88902019-07-15 21:56:43 +02004045 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01004046 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004047 goto wait;
4048 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004049
Christopher Fauletb7f88902019-07-15 21:56:43 +02004050 /* The request was fully received. Copy data */
4051 blk = htx_get_head_blk(htx);
4052 while (blk) {
4053 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004054
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004055 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02004056 break;
4057 if (type == HTX_BLK_DATA) {
4058 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004059
Christopher Fauletb7f88902019-07-15 21:56:43 +02004060 if (!chunk_memcat(temp, v.ptr, v.len)) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004061 ctx->st_code = STAT_STATUS_EXCD;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004062 goto out;
4063 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004064 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004065 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01004066 }
4067
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004068 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01004069 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01004070 cur_param = next_param = end_params;
4071 *end_params = '\0';
4072
Willy Tarreau91cefca2022-05-03 17:08:29 +02004073 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004074
4075 /*
4076 * Parse the parameters in reverse order to only store the last value.
4077 * From the html form, the backend and the action are at the end.
4078 */
4079 while (cur_param > first_param) {
4080 char *value;
4081 int poffset, plen;
4082
4083 cur_param--;
4084
4085 if ((*cur_param == '&') || (cur_param == first_param)) {
4086 reprocess_servers:
4087 /* Parse the key */
4088 poffset = (cur_param != first_param ? 1 : 0);
4089 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4090 if ((plen > 0) && (plen <= sizeof(key))) {
4091 strncpy(key, cur_param + poffset, plen);
4092 key[plen - 1] = '\0';
4093 } else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004094 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004095 goto out;
4096 }
4097
4098 /* Parse the value */
4099 value = key;
4100 while (*value != '\0' && *value != '=') {
4101 value++;
4102 }
4103 if (*value == '=') {
4104 /* Ok, a value is found, we can mark the end of the key */
4105 *value++ = '\0';
4106 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02004107 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01004108 break;
4109
4110 /* Now we can check the key to see what to do */
4111 if (!px && (strcmp(key, "b") == 0)) {
4112 if ((px = proxy_be_by_name(value)) == NULL) {
4113 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004114 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004115 goto out;
4116 }
4117 }
4118 else if (!action && (strcmp(key, "action") == 0)) {
4119 if (strcmp(value, "ready") == 0) {
4120 action = ST_ADM_ACTION_READY;
4121 }
4122 else if (strcmp(value, "drain") == 0) {
4123 action = ST_ADM_ACTION_DRAIN;
4124 }
4125 else if (strcmp(value, "maint") == 0) {
4126 action = ST_ADM_ACTION_MAINT;
4127 }
4128 else if (strcmp(value, "shutdown") == 0) {
4129 action = ST_ADM_ACTION_SHUTDOWN;
4130 }
4131 else if (strcmp(value, "dhlth") == 0) {
4132 action = ST_ADM_ACTION_DHLTH;
4133 }
4134 else if (strcmp(value, "ehlth") == 0) {
4135 action = ST_ADM_ACTION_EHLTH;
4136 }
4137 else if (strcmp(value, "hrunn") == 0) {
4138 action = ST_ADM_ACTION_HRUNN;
4139 }
4140 else if (strcmp(value, "hnolb") == 0) {
4141 action = ST_ADM_ACTION_HNOLB;
4142 }
4143 else if (strcmp(value, "hdown") == 0) {
4144 action = ST_ADM_ACTION_HDOWN;
4145 }
4146 else if (strcmp(value, "dagent") == 0) {
4147 action = ST_ADM_ACTION_DAGENT;
4148 }
4149 else if (strcmp(value, "eagent") == 0) {
4150 action = ST_ADM_ACTION_EAGENT;
4151 }
4152 else if (strcmp(value, "arunn") == 0) {
4153 action = ST_ADM_ACTION_ARUNN;
4154 }
4155 else if (strcmp(value, "adown") == 0) {
4156 action = ST_ADM_ACTION_ADOWN;
4157 }
4158 /* else these are the old supported methods */
4159 else if (strcmp(value, "disable") == 0) {
4160 action = ST_ADM_ACTION_DISABLE;
4161 }
4162 else if (strcmp(value, "enable") == 0) {
4163 action = ST_ADM_ACTION_ENABLE;
4164 }
4165 else if (strcmp(value, "stop") == 0) {
4166 action = ST_ADM_ACTION_STOP;
4167 }
4168 else if (strcmp(value, "start") == 0) {
4169 action = ST_ADM_ACTION_START;
4170 }
4171 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004172 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004173 goto out;
4174 }
4175 }
4176 else if (strcmp(key, "s") == 0) {
4177 if (!(px && action)) {
4178 /*
4179 * Indicates that we'll need to reprocess the parameters
4180 * as soon as backend and action are known
4181 */
4182 if (!reprocess) {
4183 st_cur_param = cur_param;
4184 st_next_param = next_param;
4185 }
4186 reprocess = 1;
4187 }
4188 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004189 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004190 switch (action) {
4191 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004192 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004193 altered_servers++;
4194 total_servers++;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004195 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, SRV_ADM_STCHGC_STATS_DISABLE);
William Lallemand74c24fb2016-11-21 17:18:36 +01004196 }
4197 break;
4198 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004199 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004200 altered_servers++;
4201 total_servers++;
4202 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4203 }
4204 break;
4205 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004206 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004207 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, SRV_ADM_STCHGC_STATS_STOP);
William Lallemand74c24fb2016-11-21 17:18:36 +01004208 altered_servers++;
4209 total_servers++;
4210 }
4211 break;
4212 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004213 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004214 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4215 altered_servers++;
4216 total_servers++;
4217 }
4218 break;
4219 case ST_ADM_ACTION_DHLTH:
4220 if (sv->check.state & CHK_ST_CONFIGURED) {
4221 sv->check.state &= ~CHK_ST_ENABLED;
4222 altered_servers++;
4223 total_servers++;
4224 }
4225 break;
4226 case ST_ADM_ACTION_EHLTH:
4227 if (sv->check.state & CHK_ST_CONFIGURED) {
4228 sv->check.state |= CHK_ST_ENABLED;
4229 altered_servers++;
4230 total_servers++;
4231 }
4232 break;
4233 case ST_ADM_ACTION_HRUNN:
4234 if (!(sv->track)) {
4235 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004236 srv_set_running(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004237 altered_servers++;
4238 total_servers++;
4239 }
4240 break;
4241 case ST_ADM_ACTION_HNOLB:
4242 if (!(sv->track)) {
4243 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004244 srv_set_stopping(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004245 altered_servers++;
4246 total_servers++;
4247 }
4248 break;
4249 case ST_ADM_ACTION_HDOWN:
4250 if (!(sv->track)) {
4251 sv->check.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004252 srv_set_stopped(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004253 altered_servers++;
4254 total_servers++;
4255 }
4256 break;
4257 case ST_ADM_ACTION_DAGENT:
4258 if (sv->agent.state & CHK_ST_CONFIGURED) {
4259 sv->agent.state &= ~CHK_ST_ENABLED;
4260 altered_servers++;
4261 total_servers++;
4262 }
4263 break;
4264 case ST_ADM_ACTION_EAGENT:
4265 if (sv->agent.state & CHK_ST_CONFIGURED) {
4266 sv->agent.state |= CHK_ST_ENABLED;
4267 altered_servers++;
4268 total_servers++;
4269 }
4270 break;
4271 case ST_ADM_ACTION_ARUNN:
4272 if (sv->agent.state & CHK_ST_ENABLED) {
4273 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004274 srv_set_running(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004275 altered_servers++;
4276 total_servers++;
4277 }
4278 break;
4279 case ST_ADM_ACTION_ADOWN:
4280 if (sv->agent.state & CHK_ST_ENABLED) {
4281 sv->agent.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004282 srv_set_stopped(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004283 altered_servers++;
4284 total_servers++;
4285 }
4286 break;
4287 case ST_ADM_ACTION_READY:
4288 srv_adm_set_ready(sv);
4289 altered_servers++;
4290 total_servers++;
4291 break;
4292 case ST_ADM_ACTION_DRAIN:
4293 srv_adm_set_drain(sv);
4294 altered_servers++;
4295 total_servers++;
4296 break;
4297 case ST_ADM_ACTION_MAINT:
4298 srv_adm_set_maint(sv);
4299 altered_servers++;
4300 total_servers++;
4301 break;
4302 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004303 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004304 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004305 altered_servers++;
4306 total_servers++;
4307 }
4308 break;
4309 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004310 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004311 } else {
4312 /* the server name is unknown or ambiguous (duplicate names) */
4313 total_servers++;
4314 }
4315 }
4316 if (reprocess && px && action) {
4317 /* Now, we know the backend and the action chosen by the user.
4318 * We can safely restart from the first server parameter
4319 * to reprocess them
4320 */
4321 cur_param = st_cur_param;
4322 next_param = st_next_param;
4323 reprocess = 0;
4324 goto reprocess_servers;
4325 }
4326
4327 next_param = cur_param;
4328 }
4329 }
4330
4331 if (total_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004332 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004333 }
4334 else if (altered_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004335 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004336 }
4337 else if (altered_servers == total_servers) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004338 ctx->st_code = STAT_STATUS_DONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004339 }
4340 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004341 ctx->st_code = STAT_STATUS_PART;
William Lallemand74c24fb2016-11-21 17:18:36 +01004342 }
4343 out:
4344 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004345 wait:
Willy Tarreau91cefca2022-05-03 17:08:29 +02004346 ctx->st_code = STAT_STATUS_NONE;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004347 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004348}
4349
4350
Willy Tarreaucaff6312022-05-27 10:17:46 +02004351static int stats_send_http_headers(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004352{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004353 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004354 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004355 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004356 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004357 struct htx_sl *sl;
4358 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004359
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004360 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);
4361 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4362 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004363 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004364 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004365
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004366 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004367 goto full;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004368 if (ctx->flags & STAT_FMT_HTML) {
Christopher Fauletef779222018-10-31 08:47:01 +01004369 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4370 goto full;
4371 }
Willy Tarreau91cefca2022-05-03 17:08:29 +02004372 else if (ctx->flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004373 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4374 goto full;
4375 }
Christopher Fauletef779222018-10-31 08:47:01 +01004376 else {
4377 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4378 goto full;
4379 }
4380
Willy Tarreau91cefca2022-05-03 17:08:29 +02004381 if (uri->refresh > 0 && !(ctx->flags & STAT_NO_REFRESH)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004382 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004383 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004384 goto full;
4385 }
4386
Willy Tarreau91cefca2022-05-03 17:08:29 +02004387 if (ctx->flags & STAT_CHUNKED) {
Christopher Fauletef779222018-10-31 08:47:01 +01004388 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4389 goto full;
4390 }
4391
4392 if (!htx_add_endof(htx, HTX_BLK_EOH))
4393 goto full;
4394
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004395 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004396 return 1;
4397
4398 full:
4399 htx_reset(htx);
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004400 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004401 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004402}
4403
Christopher Fauletef779222018-10-31 08:47:01 +01004404
Willy Tarreaucaff6312022-05-27 10:17:46 +02004405static int stats_send_http_redirect(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004406{
4407 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02004408 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004409 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004410 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004411 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004412 struct htx_sl *sl;
4413 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004414
Willy Tarreau91cefca2022-05-03 17:08:29 +02004415 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauletef779222018-10-31 08:47:01 +01004416 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004417 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004418 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004419
Willy Tarreaufc458ec2023-04-07 18:11:39 +02004420 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02004421 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
4422 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004423 }
4424
4425 /* We don't want to land on the posted stats page because a refresh will
4426 * repost the data. We don't want this to happen on accident so we redirect
4427 * the browse to the stats page with a GET.
4428 */
4429 chunk_printf(&trash, "%s;st=%s%s%s%s",
4430 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02004431 ((ctx->st_code > STAT_STATUS_INIT) &&
4432 (ctx->st_code < STAT_STATUS_SIZE) &&
4433 stat_status_codes[ctx->st_code]) ?
4434 stat_status_codes[ctx->st_code] :
Christopher Fauletef779222018-10-31 08:47:01 +01004435 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau91cefca2022-05-03 17:08:29 +02004436 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
4437 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Fauletef779222018-10-31 08:47:01 +01004438 scope_txt);
4439
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004440 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4441 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4442 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004443 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004444 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004445
4446 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004447 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4448 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4449 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4450 goto full;
4451
4452 if (!htx_add_endof(htx, HTX_BLK_EOH))
4453 goto full;
4454
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004455 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004456 return 1;
4457
4458full:
4459 htx_reset(htx);
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004460 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004461 return 0;
4462}
William Lallemand74c24fb2016-11-21 17:18:36 +01004463
Simon Horman05ee2132017-01-04 09:37:25 +01004464
Willy Tarreau4596fe22022-05-17 19:07:51 +02004465/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004466 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4467 * appctx->st0 contains the operation in progress (dump, done). The handler
4468 * automatically unregisters itself once transfer is complete.
4469 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004470static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004471{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004472 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02004473 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004474 struct stream *s = __sc_strm(sc);
4475 struct channel *req = sc_oc(sc);
4476 struct channel *res = sc_ic(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004477 struct htx *req_htx, *res_htx;
4478
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004479 /* only proxy stats are available via http */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004480 ctx->domain = STATS_DOMAIN_PROXY;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004481
Christopher Fauletef779222018-10-31 08:47:01 +01004482 res_htx = htx_from_buf(&res->buf);
4483
Christopher Faulet9837bd82023-04-11 07:41:30 +02004484 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) {
4485 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004486 goto out;
Christopher Faulet9837bd82023-04-11 07:41:30 +02004487 }
Christopher Fauletef779222018-10-31 08:47:01 +01004488
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004489 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004490 if (!b_size(&res->buf)) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004491 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004492 goto out;
4493 }
4494
Christopher Fauletef779222018-10-31 08:47:01 +01004495 /* all states are processed in sequence */
4496 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004497 if (stats_send_http_headers(sc, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004498 if (s->txn->meth == HTTP_METH_HEAD)
4499 appctx->st0 = STAT_HTTP_DONE;
4500 else
4501 appctx->st0 = STAT_HTTP_DUMP;
4502 }
4503 }
4504
4505 if (appctx->st0 == STAT_HTTP_DUMP) {
Aurelien DARRAGON14656842023-02-03 08:31:42 +01004506 trash_chunk = b_make(trash.area, res->buf.size, 0, 0);
Aurelien DARRAGON5e7ecbe2023-02-02 15:03:12 +01004507 /* adjust buffer size to take htx overhead into account,
4508 * make sure to perform this call on an empty buffer
4509 */
4510 trash_chunk.size = buf_room_for_htx_data(&trash_chunk);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004511 if (stats_dump_stat_to_buffer(sc, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004512 appctx->st0 = STAT_HTTP_DONE;
4513 }
4514
4515 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004516 if (stats_process_http_post(sc))
Christopher Fauletef779222018-10-31 08:47:01 +01004517 appctx->st0 = STAT_HTTP_LAST;
Christopher Fauletca5309a2023-04-17 16:17:32 +02004518 else if (s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Fauletef779222018-10-31 08:47:01 +01004519 appctx->st0 = STAT_HTTP_DONE;
4520 }
4521
4522 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004523 if (stats_send_http_redirect(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004524 appctx->st0 = STAT_HTTP_DONE;
4525 }
4526
4527 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004528 /* no more data are expected. If the response buffer is empty,
4529 * be sure to add something (EOT block in this case) to have
4530 * something to send. It is important to be sure the EOM flags
4531 * will be handled by the endpoint.
4532 */
4533 if (htx_is_empty(res_htx)) {
4534 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004535 sc_need_room(sc, sizeof(struct htx_blk) + 1);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004536 goto out;
4537 }
4538 channel_add_input(res, 1);
4539 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004540 res_htx->flags |= HTX_FL_EOM;
Willy Tarreaud869e132022-05-17 18:05:31 +02004541 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004542 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004543 }
4544
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004545 if (appctx->st0 == STAT_HTTP_END) {
Christopher Fauletdf15a5d2023-03-31 11:28:22 +02004546 se_fl_set(appctx->sedesc, SE_FL_EOS);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004547 applet_will_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004548 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004549
Christopher Fauletef779222018-10-31 08:47:01 +01004550 out:
4551 /* we have left the request in the buffer for the case where we
4552 * process a POST, and this automatically re-enables activity on
4553 * read. It's better to indicate that we want to stop reading when
4554 * we're sending, so that we know there's at most one direction
4555 * deciding to wake the applet up. It saves it from looping when
4556 * emitting large blocks into small TCP windows.
4557 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004558 htx_to_buf(res_htx, &res->buf);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004559 if (appctx->st0 == STAT_HTTP_END) {
4560 /* eat the whole request */
4561 if (co_data(req)) {
4562 req_htx = htx_from_buf(&req->buf);
4563 co_htx_skip(req, req_htx, co_data(req));
4564 htx_to_buf(req_htx, &req->buf);
4565 }
4566 }
4567 else if (!channel_is_empty(res))
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02004568 applet_wont_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004569}
4570
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004571/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004572static int stats_dump_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004573 const struct field *info,
4574 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004575{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004576 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004577 int field;
4578
4579 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4580 if (!field_format(info, field))
4581 continue;
4582
Willy Tarreaueaa55372019-10-09 07:39:11 +02004583 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004584 return 0;
4585 if (!stats_emit_raw_data_field(out, &info[field]))
4586 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004587 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4588 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004589 if (!chunk_strcat(out, "\n"))
4590 return 0;
4591 }
4592 return 1;
4593}
4594
4595/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004596static int stats_dump_typed_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004597 const struct field *info,
4598 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004599{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004600 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004601 int field;
4602
4603 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4604 if (!field_format(info, field))
4605 continue;
4606
Willy Tarreaueaa55372019-10-09 07:39:11 +02004607 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 +01004608 return 0;
4609 if (!stats_emit_field_tags(out, &info[field], ':'))
4610 return 0;
4611 if (!stats_emit_typed_data_field(out, &info[field]))
4612 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004613 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4614 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004615 if (!chunk_strcat(out, "\n"))
4616 return 0;
4617 }
4618 return 1;
4619}
4620
Willy Tarreau0b26b382021-05-08 07:43:53 +02004621/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4622 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4623 * less then this value, the function returns 0, otherwise, it returns 1. Some
4624 * fields' presence or precision may depend on some of the STAT_* flags present
4625 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004626 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004627int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004628{
Willy Tarreau83061a82018-07-13 11:56:34 +02004629 struct buffer *out = get_trash_chunk();
Willy Tarreau6be8d092023-01-12 16:08:41 +01004630 uint64_t glob_out_bytes, glob_spl_bytes, glob_out_b32;
Willy Tarreau563efe62023-04-27 14:41:37 +02004631 uint up_sec, up_usec;
4632 ullong up;
Willy Tarreau6be8d092023-01-12 16:08:41 +01004633 int thr;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004634
4635#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004636 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4637 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4638 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004639
Willy Tarreauc5977722021-05-08 08:14:04 +02004640 if (ssl_key_rate < ssl_sess_rate)
4641 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004642#endif
4643
Willy Tarreau6be8d092023-01-12 16:08:41 +01004644 /* sum certain per-thread totals (mostly byte counts) */
4645 glob_out_bytes = glob_spl_bytes = glob_out_b32 = 0;
4646 for (thr = 0; thr < global.nbthread; thr++) {
4647 glob_out_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].out_bytes);
4648 glob_spl_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].spliced_out_bytes);
4649 glob_out_b32 += read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
4650 }
4651 glob_out_b32 *= 32; // values are 32-byte units
4652
Willy Tarreauc05d30e2023-04-28 14:50:29 +02004653 up = now_ns - start_time_ns;
Willy Tarreau563efe62023-04-27 14:41:37 +02004654 up_sec = ns_to_sec(up);
4655 up_usec = (up / 1000U) % 1000000U;
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004656
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004657 if (len < INF_TOTAL_FIELDS)
4658 return 0;
4659
4660 chunk_reset(out);
4661 memset(info, 0, sizeof(*info) * len);
4662
4663 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004664 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004665 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004666 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004667
Yves Lafon95317282018-02-26 11:10:37 +01004668 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004669 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004670 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004671 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4672
4673 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreau563efe62023-04-27 14:41:37 +02004674 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 +01004675
Willy Tarreau563efe62023-04-27 14:41:37 +02004676 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 +02004677 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 +01004678 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004679 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004680 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004681 info[INF_POOL_ALLOC_BYTES] = mkf_u64(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004682 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004683 info[INF_POOL_USED_BYTES] = mkf_u64(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004684 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4685 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4686 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4687 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4688 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4689 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4690 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4691 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4692#ifdef USE_OPENSSL
4693 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004694 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4695 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004696#endif
4697 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4698 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4699 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004700 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 +01004701 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4702 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004703 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 +01004704 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4705 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4706
4707#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004708 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 +01004709 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4710 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004711 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 +01004712 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004713 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4714 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 +01004715 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4716 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4717 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4718#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004719 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));
4720 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 +01004721 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4722#ifdef USE_ZLIB
4723 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4724 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4725#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004726 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004727 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004728 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004729 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4730 if (global.desc)
4731 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004732 info[INF_STOPPING] = mkf_u32(0, stopping);
4733 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004734 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004735 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004736 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004737 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004738 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004739 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004740 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau6be8d092023-01-12 16:08:41 +01004741 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, glob_out_bytes);
4742 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, glob_spl_bytes);
4743 info[INF_BYTES_OUT_RATE] = mkf_u64(FN_RATE, glob_out_b32);
Willy Tarreau9b013702019-10-24 18:18:02 +02004744 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004745 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004746
4747 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
Willy Tarreau3c4a2972023-05-11 12:02:21 +02004748 info[INF_WARNINGS] = mkf_u32(FN_COUNTER, HA_ATOMIC_LOAD(&tot_warnings));
Willy Tarreau96151022023-05-11 13:51:31 +02004749 info[INF_MAXCONN_REACHED] = mkf_u32(FN_COUNTER, HA_ATOMIC_LOAD(&maxconn_reached));
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004750 chunk_appendf(out, "%#x", get_tainted());
4751
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004752 return 1;
4753}
4754
Willy Tarreau4596fe22022-05-17 19:07:51 +02004755/* This function dumps information onto the stream connector's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004756 * It returns 0 as long as it does not complete, non-zero upon completion.
4757 * No state is used.
4758 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004759static int stats_dump_info_to_buffer(struct stconn *sc)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004760{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004761 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004762 struct show_stat_ctx *ctx = appctx->svcctx;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004763 int ret;
4764 int current_field;
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004765
Willy Tarreau91cefca2022-05-03 17:08:29 +02004766 if (!stats_fill_info(info, INF_TOTAL_FIELDS, ctx->flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004767 return 0;
4768
Christopher Fauleta8b76842022-12-16 15:08:36 +01004769 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004770more:
4771 current_field = ctx->field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004772
Willy Tarreau91cefca2022-05-03 17:08:29 +02004773 if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004774 ret = stats_dump_typed_info_fields(&trash_chunk, info, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004775 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004776 ret = stats_dump_json_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004777 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01004778 ret = stats_dump_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004779
Christopher Fauleta8b76842022-12-16 15:08:36 +01004780 if (applet_putchk(appctx, &trash_chunk) == -1) {
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004781 /* restore previous field */
4782 ctx->field = current_field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004783 return 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004784 }
4785 if (ret && ctx->field) {
4786 /* partial dump */
4787 goto more;
4788 }
4789 ctx->field = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004790 return 1;
4791}
4792
Willy Tarreau4596fe22022-05-17 19:07:51 +02004793/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004794 * It returns 0 as long as it does not complete, non-zero upon completion.
4795 * No state is used.
4796 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004797 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004798 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4799 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004800static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004801{
4802
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004803 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004804
4805 chunk_strcat(out,
4806 "{"
4807 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4808 "\"oneOf\":["
4809 "{"
4810 "\"title\":\"Info\","
4811 "\"type\":\"array\","
4812 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004813 "\"title\":\"InfoItem\","
4814 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004815 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004816 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4817 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4818 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4819 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4820 "},"
4821 "\"required\":[\"field\",\"processNum\",\"tags\","
4822 "\"value\"]"
4823 "}"
4824 "},"
4825 "{"
4826 "\"title\":\"Stat\","
4827 "\"type\":\"array\","
4828 "\"items\":{"
4829 "\"title\":\"InfoItem\","
4830 "\"type\":\"object\","
4831 "\"properties\":{"
4832 "\"objType\":{"
4833 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4834 "\"Server\",\"Unknown\"]"
4835 "},"
4836 "\"proxyId\":{"
4837 "\"type\":\"integer\","
4838 "\"minimum\":0"
4839 "},"
4840 "\"id\":{"
4841 "\"type\":\"integer\","
4842 "\"minimum\":0"
4843 "},"
4844 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4845 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4846 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4847 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4848 "},"
4849 "\"required\":[\"objType\",\"proxyId\",\"id\","
4850 "\"field\",\"processNum\",\"tags\","
4851 "\"value\"]"
4852 "}"
4853 "},"
4854 "{"
4855 "\"title\":\"Error\","
4856 "\"type\":\"object\","
4857 "\"properties\":{"
4858 "\"errorStr\":{"
4859 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004860 "}"
4861 "},"
4862 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004863 "}"
4864 "],"
4865 "\"definitions\":{"
4866 "\"field\":{"
4867 "\"type\":\"object\","
4868 "\"pos\":{"
4869 "\"type\":\"integer\","
4870 "\"minimum\":0"
4871 "},"
4872 "\"name\":{"
4873 "\"type\":\"string\""
4874 "},"
4875 "\"required\":[\"pos\",\"name\"]"
4876 "},"
4877 "\"processNum\":{"
4878 "\"type\":\"integer\","
4879 "\"minimum\":1"
4880 "},"
4881 "\"tags\":{"
4882 "\"type\":\"object\","
4883 "\"origin\":{"
4884 "\"type\":\"string\","
4885 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4886 "\"Config\",\"Product\",\"Unknown\"]"
4887 "},"
4888 "\"nature\":{"
4889 "\"type\":\"string\","
4890 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4891 "\"Rate\",\"Counter\",\"Duration\","
4892 "\"Age\",\"Time\",\"Name\",\"Output\","
4893 "\"Avg\", \"Unknown\"]"
4894 "},"
4895 "\"scope\":{"
4896 "\"type\":\"string\","
4897 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4898 "\"System\",\"Unknown\"]"
4899 "},"
4900 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4901 "},"
4902 "\"typedValue\":{"
4903 "\"type\":\"object\","
4904 "\"oneOf\":["
4905 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4906 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4907 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4908 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4909 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4910 "],"
4911 "\"definitions\":{"
4912 "\"s32Value\":{"
4913 "\"properties\":{"
4914 "\"type\":{"
4915 "\"type\":\"string\","
4916 "\"enum\":[\"s32\"]"
4917 "},"
4918 "\"value\":{"
4919 "\"type\":\"integer\","
4920 "\"minimum\":-2147483648,"
4921 "\"maximum\":2147483647"
4922 "}"
4923 "},"
4924 "\"required\":[\"type\",\"value\"]"
4925 "},"
4926 "\"s64Value\":{"
4927 "\"properties\":{"
4928 "\"type\":{"
4929 "\"type\":\"string\","
4930 "\"enum\":[\"s64\"]"
4931 "},"
4932 "\"value\":{"
4933 "\"type\":\"integer\","
4934 "\"minimum\":-9007199254740991,"
4935 "\"maximum\":9007199254740991"
4936 "}"
4937 "},"
4938 "\"required\":[\"type\",\"value\"]"
4939 "},"
4940 "\"u32Value\":{"
4941 "\"properties\":{"
4942 "\"type\":{"
4943 "\"type\":\"string\","
4944 "\"enum\":[\"u32\"]"
4945 "},"
4946 "\"value\":{"
4947 "\"type\":\"integer\","
4948 "\"minimum\":0,"
4949 "\"maximum\":4294967295"
4950 "}"
4951 "},"
4952 "\"required\":[\"type\",\"value\"]"
4953 "},"
4954 "\"u64Value\":{"
4955 "\"properties\":{"
4956 "\"type\":{"
4957 "\"type\":\"string\","
4958 "\"enum\":[\"u64\"]"
4959 "},"
4960 "\"value\":{"
4961 "\"type\":\"integer\","
4962 "\"minimum\":0,"
4963 "\"maximum\":9007199254740991"
4964 "}"
4965 "},"
4966 "\"required\":[\"type\",\"value\"]"
4967 "},"
4968 "\"strValue\":{"
4969 "\"properties\":{"
4970 "\"type\":{"
4971 "\"type\":\"string\","
4972 "\"enum\":[\"str\"]"
4973 "},"
4974 "\"value\":{\"type\":\"string\"}"
4975 "},"
4976 "\"required\":[\"type\",\"value\"]"
4977 "},"
4978 "\"unknownValue\":{"
4979 "\"properties\":{"
4980 "\"type\":{"
4981 "\"type\":\"integer\","
4982 "\"minimum\":0"
4983 "},"
4984 "\"value\":{"
4985 "\"type\":\"string\","
4986 "\"enum\":[\"unknown\"]"
4987 "}"
4988 "},"
4989 "\"required\":[\"type\",\"value\"]"
4990 "}"
4991 "}"
4992 "}"
4993 "}"
4994 "}");
4995
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004996 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01004997 chunk_reset(out);
4998 chunk_appendf(out,
4999 "{\"errorStr\":\"output buffer too short\"}");
5000 }
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02005001 chunk_appendf(out, "\n");
Simon Horman6f6bb382017-01-04 09:37:26 +01005002}
5003
Willy Tarreau4596fe22022-05-17 19:07:51 +02005004/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01005005 * It returns 0 as long as it does not complete, non-zero upon completion.
5006 * No state is used.
5007 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005008static int stats_dump_json_schema_to_buffer(struct appctx *appctx)
Simon Horman6f6bb382017-01-04 09:37:26 +01005009{
Simon Horman6f6bb382017-01-04 09:37:26 +01005010
Christopher Fauleta8b76842022-12-16 15:08:36 +01005011 chunk_reset(&trash_chunk);
Simon Horman6f6bb382017-01-04 09:37:26 +01005012
Christopher Fauleta8b76842022-12-16 15:08:36 +01005013 stats_dump_json_schema(&trash_chunk);
5014
5015 if (applet_putchk(appctx, &trash_chunk) == -1)
Simon Horman6f6bb382017-01-04 09:37:26 +01005016 return 0;
Simon Horman6f6bb382017-01-04 09:37:26 +01005017
5018 return 1;
5019}
5020
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005021static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01005022{
5023 struct proxy *px;
5024 struct server *sv;
5025 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005026 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005027 int clrall = 0;
5028
5029 if (strcmp(args[2], "all") == 0)
5030 clrall = 1;
5031
5032 /* check permissions */
5033 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
5034 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
5035 return 1;
5036
Olivier Houchardfbc74e82017-11-24 16:54:05 +01005037 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01005038 if (clrall) {
5039 memset(&px->be_counters, 0, sizeof(px->be_counters));
5040 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
5041 }
5042 else {
5043 px->be_counters.conn_max = 0;
5044 px->be_counters.p.http.rps_max = 0;
5045 px->be_counters.sps_max = 0;
5046 px->be_counters.cps_max = 0;
5047 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005048 px->be_counters.qtime_max = 0;
5049 px->be_counters.ctime_max = 0;
5050 px->be_counters.dtime_max = 0;
5051 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005052
5053 px->fe_counters.conn_max = 0;
5054 px->fe_counters.p.http.rps_max = 0;
5055 px->fe_counters.sps_max = 0;
5056 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005057 }
5058
5059 for (sv = px->srv; sv; sv = sv->next)
5060 if (clrall)
5061 memset(&sv->counters, 0, sizeof(sv->counters));
5062 else {
5063 sv->counters.cur_sess_max = 0;
5064 sv->counters.nbpend_max = 0;
5065 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005066 sv->counters.qtime_max = 0;
5067 sv->counters.ctime_max = 0;
5068 sv->counters.dtime_max = 0;
5069 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005070 }
5071
5072 list_for_each_entry(li, &px->conf.listeners, by_fe)
5073 if (li->counters) {
5074 if (clrall)
5075 memset(li->counters, 0, sizeof(*li->counters));
5076 else
5077 li->counters->conn_max = 0;
5078 }
5079 }
5080
5081 global.cps_max = 0;
5082 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02005083 global.ssl_max = 0;
5084 global.ssl_fe_keys_max = 0;
5085 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005086
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005087 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5088 if (!mod->clearable && !clrall)
5089 continue;
5090
5091 for (px = proxies_list; px; px = px->next) {
5092 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
5093
5094 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
5095 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
5096 mod,
5097 mod->counters,
5098 mod->counters_size);
5099 }
5100
5101 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
5102 EXTRA_COUNTERS_INIT(px->extra_counters_be,
5103 mod,
5104 mod->counters,
5105 mod->counters_size);
5106 }
5107
5108 if (mod_cap & STATS_PX_CAP_SRV) {
5109 for (sv = px->srv; sv; sv = sv->next) {
5110 EXTRA_COUNTERS_INIT(sv->extra_counters,
5111 mod,
5112 mod->counters,
5113 mod->counters_size);
5114 }
5115 }
5116
5117 if (mod_cap & STATS_PX_CAP_LI) {
5118 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5119 EXTRA_COUNTERS_INIT(li->extra_counters,
5120 mod,
5121 mod->counters,
5122 mod->counters_size);
5123 }
5124 }
5125 }
5126 }
5127
Emeric Brunf8642ee2021-10-29 17:59:18 +02005128 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005129
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005130 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01005131 return 1;
5132}
5133
5134
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005135static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005136{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005137 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005138 int arg = 2;
5139
Willy Tarreau91cefca2022-05-03 17:08:29 +02005140 ctx->scope_str = 0;
5141 ctx->scope_len = 0;
5142 ctx->flags = 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01005143 ctx->field = 0; /* explicit default value */
Willy Tarreaud25fc792016-12-16 12:33:47 +01005144
Willy Tarreau2f397382019-10-09 11:43:59 +02005145 while (*args[arg]) {
5146 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005147 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005148 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005149 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005150 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005151 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02005152 else if (strcmp(args[arg], "float") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005153 ctx->flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02005154 arg++;
5155 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005156 return 0;
5157}
5158
5159
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005160static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01005161{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005162 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005163 int arg = 2;
5164
Willy Tarreau91cefca2022-05-03 17:08:29 +02005165 ctx->scope_str = 0;
5166 ctx->scope_len = 0;
5167 ctx->flags = STAT_SHNODE | STAT_SHDESC;
Willy Tarreau578d6e42019-10-09 11:00:22 +02005168
Willy Tarreau0698c802022-05-11 14:09:57 +02005169 if ((strm_li(appctx_strm(appctx))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005170 ctx->flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01005171
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005172 /* proxy is the default domain */
Willy Tarreau91cefca2022-05-03 17:08:29 +02005173 ctx->domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005174 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005175 ++args;
5176
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005177 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005178 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005179 } else if (strcmp(args[arg], "resolvers") == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02005180 ctx->domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005181 ++args;
5182 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005183 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005184 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005185 }
5186
Willy Tarreau91cefca2022-05-03 17:08:29 +02005187 if (ctx->domain == STATS_DOMAIN_PROXY
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005188 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005189 struct proxy *px;
5190
Willy Tarreau2f397382019-10-09 11:43:59 +02005191 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005192 if (px)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005193 ctx->iid = px->uuid;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005194 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02005195 ctx->iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005196
Willy Tarreau91cefca2022-05-03 17:08:29 +02005197 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02005198 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005199
Willy Tarreau91cefca2022-05-03 17:08:29 +02005200 ctx->flags |= STAT_BOUND;
5201 ctx->type = atoi(args[arg+1]);
5202 ctx->sid = atoi(args[arg+2]);
Willy Tarreau2f397382019-10-09 11:43:59 +02005203 arg += 3;
5204 }
5205
5206 while (*args[arg]) {
5207 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005208 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005209 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005210 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005211 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005212 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02005213 else if (strcmp(args[arg], "no-maint") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005214 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02005215 else if (strcmp(args[arg], "up") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005216 ctx->flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02005217 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01005218 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01005219
Willy Tarreau2b812e22016-11-22 16:18:05 +01005220 return 0;
5221}
5222
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005223static int cli_io_handler_dump_info(struct appctx *appctx)
5224{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005225 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005226 return stats_dump_info_to_buffer(appctx_sc(appctx));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005227}
5228
Willy Tarreau4596fe22022-05-17 19:07:51 +02005229/* This I/O handler runs as an applet embedded in a stream connector. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01005230 * used to send raw stats over a socket.
5231 */
5232static int cli_io_handler_dump_stat(struct appctx *appctx)
5233{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005234 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005235 return stats_dump_stat_to_buffer(appctx_sc(appctx), NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01005236}
5237
Simon Horman6f6bb382017-01-04 09:37:26 +01005238static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5239{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005240 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005241 return stats_dump_json_schema_to_buffer(appctx);
Simon Horman6f6bb382017-01-04 09:37:26 +01005242}
5243
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005244int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5245 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005246{
5247 struct stats_module *mod;
5248
5249 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5250
5251 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5252 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5253 continue;
5254
5255 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5256 }
5257
5258 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5259
5260 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5261 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5262 continue;
5263
5264 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5265 }
5266
5267 return 1;
5268
5269 alloc_failed:
5270 return 0;
5271}
5272
5273/* Initialize and allocate all extra counters for a proxy and its attached
5274 * servers/listeners with all already registered stats module
5275 */
5276int stats_allocate_proxy_counters(struct proxy *px)
5277{
5278 struct server *sv;
5279 struct listener *li;
5280
5281 if (px->cap & PR_CAP_FE) {
5282 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5283 COUNTERS_FE,
5284 STATS_PX_CAP_FE)) {
5285 return 0;
5286 }
5287 }
5288
5289 if (px->cap & PR_CAP_BE) {
5290 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5291 COUNTERS_BE,
5292 STATS_PX_CAP_BE)) {
5293 return 0;
5294 }
5295 }
5296
5297 for (sv = px->srv; sv; sv = sv->next) {
5298 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5299 COUNTERS_SV,
5300 STATS_PX_CAP_SRV)) {
5301 return 0;
5302 }
5303 }
5304
5305 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5306 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5307 COUNTERS_LI,
5308 STATS_PX_CAP_LI)) {
5309 return 0;
5310 }
5311 }
5312
5313 return 1;
5314}
5315
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005316void stats_register_module(struct stats_module *m)
5317{
5318 const uint8_t domain = stats_get_domain(m->domain_flags);
5319
Willy Tarreau2b718102021-04-21 07:32:39 +02005320 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005321 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005322}
5323
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005324static int allocate_stats_px_postcheck(void)
5325{
5326 struct stats_module *mod;
5327 size_t i = ST_F_TOTAL_FIELDS;
5328 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005329 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005330
5331 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5332
5333 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5334 if (!stat_f[STATS_DOMAIN_PROXY]) {
5335 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5336 err_code |= ERR_ALERT | ERR_FATAL;
5337 return err_code;
5338 }
5339
5340 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5341 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5342
5343 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5344 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5345 mod->stats,
5346 mod->stats_count * sizeof(struct name_desc));
5347 i += mod->stats_count;
5348 }
5349
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005350 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005351 if (!stats_allocate_proxy_counters(px)) {
5352 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5353 err_code |= ERR_ALERT | ERR_FATAL;
5354 return err_code;
5355 }
5356 }
5357
Christopher Fauletde79cd22021-01-06 07:41:56 +01005358 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005359
5360 return err_code;
5361}
5362
5363REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5364
Emeric Brunf8642ee2021-10-29 17:59:18 +02005365static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005366{
5367 struct stats_module *mod;
5368 size_t i = 0;
5369 int err_code = 0;
5370
Emeric Brunf8642ee2021-10-29 17:59:18 +02005371 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5372 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5373 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005374 err_code |= ERR_ALERT | ERR_FATAL;
5375 return err_code;
5376 }
5377
Emeric Brunf8642ee2021-10-29 17:59:18 +02005378 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5379 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005380 mod->stats,
5381 mod->stats_count * sizeof(struct name_desc));
5382 i += mod->stats_count;
5383 }
5384
Emeric Brunf8642ee2021-10-29 17:59:18 +02005385 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5386 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005387 err_code |= ERR_ALERT | ERR_FATAL;
5388 return err_code;
5389 }
5390
Christopher Fauletde79cd22021-01-06 07:41:56 +01005391 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005392
5393 return err_code;
5394}
5395
Emeric Brunf8642ee2021-10-29 17:59:18 +02005396REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005397
Christopher Fauletde79cd22021-01-06 07:41:56 +01005398static int allocate_stat_lines_per_thread(void)
5399{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005400 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005401
5402 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5403 const int domain = domains[i];
5404
5405 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5406 if (!stat_l[domain])
5407 return 0;
5408 }
5409 return 1;
5410}
5411
5412REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5413
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005414static int allocate_trash_counters(void)
5415{
5416 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005417 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005418 size_t max_counters_size = 0;
5419
5420 /* calculate the greatest counters used by any stats modules */
5421 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5422 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5423 max_counters_size = mod->counters_size > max_counters_size ?
5424 mod->counters_size : max_counters_size;
5425 }
5426 }
5427
5428 /* allocate the trash with the size of the greatest counters */
5429 if (max_counters_size) {
5430 trash_counters = malloc(max_counters_size);
5431 if (!trash_counters) {
5432 ha_alert("stats: cannot allocate trash counters for statistics\n");
5433 return 0;
5434 }
5435 }
5436
5437 return 1;
5438}
5439
5440REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5441
Christopher Fauletde79cd22021-01-06 07:41:56 +01005442static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005443{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005444 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005445
5446 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5447 const int domain = domains[i];
5448
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005449 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005450 }
5451}
5452
5453
5454REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5455
5456static void deinit_stats(void)
5457{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005458 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005459
5460 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5461 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005462
5463 if (stat_f[domain])
5464 free(stat_f[domain]);
5465 }
5466}
5467
5468REGISTER_POST_DEINIT(deinit_stats);
5469
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005470static void free_trash_counters(void)
5471{
5472 if (trash_counters)
5473 free(trash_counters);
5474}
5475
5476REGISTER_PER_THREAD_FREE(free_trash_counters);
5477
Willy Tarreau2b812e22016-11-22 16:18:05 +01005478/* register cli keywords */
5479static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005480 { { "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 +02005481 { { "show", "info", NULL }, "show info [desc|json|typed|float]* : report information about the running process", cli_parse_show_info, cli_io_handler_dump_info, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005482 { { "show", "stat", NULL }, "show stat [desc|json|no-maint|typed|up]*: report counters for each proxy and server", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
5483 { { "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 +01005484 {{},}
5485}};
5486
Willy Tarreau0108d902018-11-25 19:14:37 +01005487INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5488
William Lallemand74c24fb2016-11-21 17:18:36 +01005489struct applet http_stats_applet = {
5490 .obj_type = OBJ_TYPE_APPLET,
5491 .name = "<STATS>", /* used for logging */
5492 .fct = http_stats_io_handler,
5493 .release = NULL,
5494};
5495
William Lallemand74c24fb2016-11-21 17:18:36 +01005496/*
5497 * Local variables:
5498 * c-indent-level: 8
5499 * c-basic-offset: 8
5500 * End:
5501 */