blob: 68adde6c5810e1019e13554b0ab39cfc68335a76 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <pwd.h>
20#include <grp.h>
21
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020027#include <haproxy/activity.h>
Willy Tarreau91cefca2022-05-03 17:08:29 +020028#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020029#include <haproxy/backend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020031#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020032#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020033#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau55542642021-10-08 09:33:24 +020035#include <haproxy/clock.h>
Willy Tarreau0a3bd392020-06-04 08:52:38 +020036#include <haproxy/compression.h>
Willy Tarreau2a83d602020-05-27 16:58:08 +020037#include <haproxy/debug.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020038#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/fd.h>
40#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020041#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020042#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020043#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020044#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020045#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020046#include <haproxy/htx.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020047#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020048#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020049#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020050#include <haproxy/map-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020051#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020052#include <haproxy/pipe.h>
53#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020054#include <haproxy/proxy.h>
Emeric Brunc9437992021-02-12 19:42:55 +010055#include <haproxy/resolvers.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020056#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020057#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020058#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020059#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020060#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020061#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020062#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020063#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020064#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020065#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020066#include <haproxy/uri_auth-t.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020067#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010068
William Lallemand74c24fb2016-11-21 17:18:36 +010069
Christopher Faulet2b9b6782019-02-27 16:42:58 +010070/* status codes available for the stats admin page (strictly 4 chars length) */
71const char *stat_status_codes[STAT_STATUS_SIZE] = {
72 [STAT_STATUS_DENY] = "DENY",
73 [STAT_STATUS_DONE] = "DONE",
74 [STAT_STATUS_ERRP] = "ERRP",
75 [STAT_STATUS_EXCD] = "EXCD",
76 [STAT_STATUS_NONE] = "NONE",
77 [STAT_STATUS_PART] = "PART",
78 [STAT_STATUS_UNKN] = "UNKN",
79 [STAT_STATUS_IVAL] = "IVAL",
80};
81
Willy Tarreau0baac8c2016-11-22 16:36:53 +010082/* These are the field names for each INF_* field position. Please pay attention
83 * to always use the exact same name except that the strings for new names must
84 * be lower case or CamelCase while the enum entries must be upper case.
85 */
Willy Tarreaueaa55372019-10-09 07:39:11 +020086const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +020087 [INF_NAME] = { .name = "Name", .desc = "Product name" },
88 [INF_VERSION] = { .name = "Version", .desc = "Product version" },
89 [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
90 [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
Willy Tarreau91358592021-06-15 08:08:04 +020091 [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (historical, always 1)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +020092 [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020093 [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
94 [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
95 [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
William Dauchydefd1562021-01-15 22:41:38 +010096 [INF_START_TIME_SEC] = { .name = "Start_time_sec", .desc = "Start time in seconds" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020097 [INF_MEMMAX_MB] = { .name = "Memmax_MB", .desc = "Worker process's hard limit on memory usage in MB (-m on command line)" },
William Dauchya8766cf2021-01-15 22:41:37 +010098 [INF_MEMMAX_BYTES] = { .name = "Memmax_bytes", .desc = "Worker process's hard limit on memory usage in byes (-m on command line)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +020099 [INF_POOL_ALLOC_MB] = { .name = "PoolAlloc_MB", .desc = "Amount of memory allocated in pools (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100100 [INF_POOL_ALLOC_BYTES] = { .name = "PoolAlloc_bytes", .desc = "Amount of memory allocated in pools (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200101 [INF_POOL_USED_MB] = { .name = "PoolUsed_MB", .desc = "Amount of pool memory currently used (in MB)" },
William Dauchya8766cf2021-01-15 22:41:37 +0100102 [INF_POOL_USED_BYTES] = { .name = "PoolUsed_bytes", .desc = "Amount of pool memory currently used (in bytes)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200103 [INF_POOL_FAILED] = { .name = "PoolFailed", .desc = "Number of failed pool allocations since this worker was started" },
104 [INF_ULIMIT_N] = { .name = "Ulimit-n", .desc = "Hard limit on the number of per-process file descriptors" },
105 [INF_MAXSOCK] = { .name = "Maxsock", .desc = "Hard limit on the number of per-process sockets" },
106 [INF_MAXCONN] = { .name = "Maxconn", .desc = "Hard limit on the number of per-process connections (configured or imposed by Ulimit-n)" },
107 [INF_HARD_MAXCONN] = { .name = "Hard_maxconn", .desc = "Hard limit on the number of per-process connections (imposed by Memmax_MB or Ulimit-n)" },
108 [INF_CURR_CONN] = { .name = "CurrConns", .desc = "Current number of connections on this worker process" },
109 [INF_CUM_CONN] = { .name = "CumConns", .desc = "Total number of connections on this worker process since started" },
110 [INF_CUM_REQ] = { .name = "CumReq", .desc = "Total number of requests on this worker process since started" },
111 [INF_MAX_SSL_CONNS] = { .name = "MaxSslConns", .desc = "Hard limit on the number of per-process SSL endpoints (front+back), 0=unlimited" },
112 [INF_CURR_SSL_CONNS] = { .name = "CurrSslConns", .desc = "Current number of SSL endpoints on this worker process (front+back)" },
113 [INF_CUM_SSL_CONNS] = { .name = "CumSslConns", .desc = "Total number of SSL endpoints on this worker process since started (front+back)" },
114 [INF_MAXPIPES] = { .name = "Maxpipes", .desc = "Hard limit on the number of pipes for splicing, 0=unlimited" },
115 [INF_PIPES_USED] = { .name = "PipesUsed", .desc = "Current number of pipes in use in this worker process" },
116 [INF_PIPES_FREE] = { .name = "PipesFree", .desc = "Current number of allocated and available pipes in this worker process" },
117 [INF_CONN_RATE] = { .name = "ConnRate", .desc = "Number of front connections created on this worker process over the last second" },
118 [INF_CONN_RATE_LIMIT] = { .name = "ConnRateLimit", .desc = "Hard limit for ConnRate (global.maxconnrate)" },
119 [INF_MAX_CONN_RATE] = { .name = "MaxConnRate", .desc = "Highest ConnRate reached on this worker process since started (in connections per second)" },
120 [INF_SESS_RATE] = { .name = "SessRate", .desc = "Number of sessions created on this worker process over the last second" },
121 [INF_SESS_RATE_LIMIT] = { .name = "SessRateLimit", .desc = "Hard limit for SessRate (global.maxsessrate)" },
122 [INF_MAX_SESS_RATE] = { .name = "MaxSessRate", .desc = "Highest SessRate reached on this worker process since started (in sessions per second)" },
123 [INF_SSL_RATE] = { .name = "SslRate", .desc = "Number of SSL connections created on this worker process over the last second" },
124 [INF_SSL_RATE_LIMIT] = { .name = "SslRateLimit", .desc = "Hard limit for SslRate (global.maxsslrate)" },
125 [INF_MAX_SSL_RATE] = { .name = "MaxSslRate", .desc = "Highest SslRate reached on this worker process since started (in connections per second)" },
126 [INF_SSL_FRONTEND_KEY_RATE] = { .name = "SslFrontendKeyRate", .desc = "Number of SSL keys created on frontends in this worker process over the last second" },
127 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .name = "SslFrontendMaxKeyRate", .desc = "Highest SslFrontendKeyRate reached on this worker process since started (in SSL keys per second)" },
128 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .name = "SslFrontendSessionReuse_pct", .desc = "Percent of frontend SSL connections which did not require a new key" },
129 [INF_SSL_BACKEND_KEY_RATE] = { .name = "SslBackendKeyRate", .desc = "Number of SSL keys created on backends in this worker process over the last second" },
130 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .name = "SslBackendMaxKeyRate", .desc = "Highest SslBackendKeyRate reached on this worker process since started (in SSL keys per second)" },
131 [INF_SSL_CACHE_LOOKUPS] = { .name = "SslCacheLookups", .desc = "Total number of SSL session ID lookups in the SSL session cache on this worker since started" },
132 [INF_SSL_CACHE_MISSES] = { .name = "SslCacheMisses", .desc = "Total number of SSL session ID lookups that didn't find a session in the SSL session cache on this worker since started" },
133 [INF_COMPRESS_BPS_IN] = { .name = "CompressBpsIn", .desc = "Number of bytes submitted to the HTTP compressor in this worker process over the last second" },
134 [INF_COMPRESS_BPS_OUT] = { .name = "CompressBpsOut", .desc = "Number of bytes emitted by the HTTP compressor in this worker process over the last second" },
135 [INF_COMPRESS_BPS_RATE_LIM] = { .name = "CompressBpsRateLim", .desc = "Limit of CompressBpsOut beyond which HTTP compression is automatically disabled" },
136 [INF_ZLIB_MEM_USAGE] = { .name = "ZlibMemUsage", .desc = "Amount of memory currently used by HTTP compression on the current worker process (in bytes)" },
137 [INF_MAX_ZLIB_MEM_USAGE] = { .name = "MaxZlibMemUsage", .desc = "Limit on the amount of memory used by HTTP compression above which it is automatically disabled (in bytes, see global.maxzlibmem)" },
138 [INF_TASKS] = { .name = "Tasks", .desc = "Total number of tasks in the current worker process (active + sleeping)" },
139 [INF_RUN_QUEUE] = { .name = "Run_queue", .desc = "Total number of active tasks+tasklets in the current worker process" },
140 [INF_IDLE_PCT] = { .name = "Idle_pct", .desc = "Percentage of last second spent waiting in the current worker thread" },
141 [INF_NODE] = { .name = "node", .desc = "Node name (global.node)" },
142 [INF_DESCRIPTION] = { .name = "description", .desc = "Node description (global.description)" },
143 [INF_STOPPING] = { .name = "Stopping", .desc = "1 if the worker process is currently stopping, otherwise zero" },
144 [INF_JOBS] = { .name = "Jobs", .desc = "Current number of active jobs on the current worker process (frontend connections, master connections, listeners)" },
145 [INF_UNSTOPPABLE_JOBS] = { .name = "Unstoppable Jobs", .desc = "Current number of unstoppable jobs on the current worker process (master connections)" },
146 [INF_LISTENERS] = { .name = "Listeners", .desc = "Current number of active listeners on the current worker process" },
147 [INF_ACTIVE_PEERS] = { .name = "ActivePeers", .desc = "Current number of verified active peers connections on the current worker process" },
148 [INF_CONNECTED_PEERS] = { .name = "ConnectedPeers", .desc = "Current number of peers having passed the connection step on the current worker process" },
149 [INF_DROPPED_LOGS] = { .name = "DroppedLogs", .desc = "Total number of dropped logs for current worker process since started" },
150 [INF_BUSY_POLLING] = { .name = "BusyPolling", .desc = "1 if busy-polling is currently in use on the worker process, otherwise zero (config.busy-polling)" },
151 [INF_FAILED_RESOLUTIONS] = { .name = "FailedResolutions", .desc = "Total number of failed DNS resolutions in current worker process since started" },
152 [INF_TOTAL_BYTES_OUT] = { .name = "TotalBytesOut", .desc = "Total number of bytes emitted by current worker process since started" },
Tim Duesterhus0ababda2023-03-27 15:23:44 +0200153 [INF_TOTAL_SPLICED_BYTES_OUT] = { .name = "TotalSplicedBytesOut", .desc = "Total number of bytes emitted by current worker process through a kernel pipe since started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200154 [INF_BYTES_OUT_RATE] = { .name = "BytesOutRate", .desc = "Number of bytes emitted by current worker process over the last second" },
Willy Tarreau9b013702019-10-24 18:18:02 +0200155 [INF_DEBUG_COMMANDS_ISSUED] = { .name = "DebugCommandsIssued", .desc = "Number of debug commands issued on this process (anything > 0 is unsafe)" },
Emeric Brun45c457a2020-07-09 23:23:34 +0200156 [INF_CUM_LOG_MSGS] = { .name = "CumRecvLogs", .desc = "Total number of log messages received by log-forwarding listeners on this worker process since started" },
Adis Nezirovicb62b78b2021-01-15 13:12:33 +0100157 [INF_BUILD_INFO] = { .name = "Build info", .desc = "Build info" },
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +0200158 [INF_TAINTED] = { .name = "Tainted", .desc = "Experimental features used" },
Willy Tarreau3c4a2972023-05-11 12:02:21 +0200159 [INF_WARNINGS] = { .name = "TotalWarnings", .desc = "Total warnings issued" },
Willy Tarreau96151022023-05-11 13:51:31 +0200160 [INF_MAXCONN_REACHED] = { .name = "MaxconnReached", .desc = "Number of times an accepted connection resulted in Maxconn being reached" },
Willy Tarreau5723b382023-05-17 09:05:20 +0200161 [INF_BOOTTIME_MS] = { .name = "BootTime_ms", .desc = "How long ago it took to parse and process the config before being ready (milliseconds)" },
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100162};
163
Willy Tarreaueaa55372019-10-09 07:39:11 +0200164const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200165 [ST_F_PXNAME] = { .name = "pxname", .desc = "Proxy name" },
166 [ST_F_SVNAME] = { .name = "svname", .desc = "Server name" },
William Dauchyeedb9b12021-02-01 13:11:53 +0100167 [ST_F_QCUR] = { .name = "qcur", .desc = "Number of current queued connections" },
168 [ST_F_QMAX] = { .name = "qmax", .desc = "Highest value of queued connections encountered since process started" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100169 [ST_F_SCUR] = { .name = "scur", .desc = "Number of current sessions on the frontend, backend or server" },
170 [ST_F_SMAX] = { .name = "smax", .desc = "Highest value of current sessions encountered since process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200171 [ST_F_SLIM] = { .name = "slim", .desc = "Frontend/listener/server's maxconn, backend's fullconn" },
172 [ST_F_STOT] = { .name = "stot", .desc = "Total number of sessions since process started" },
173 [ST_F_BIN] = { .name = "bin", .desc = "Total number of request bytes since process started" },
174 [ST_F_BOUT] = { .name = "bout", .desc = "Total number of response bytes since process started" },
175 [ST_F_DREQ] = { .name = "dreq", .desc = "Total number of denied requests since process started" },
176 [ST_F_DRESP] = { .name = "dresp", .desc = "Total number of denied responses since process started" },
177 [ST_F_EREQ] = { .name = "ereq", .desc = "Total number of invalid requests since process started" },
178 [ST_F_ECON] = { .name = "econ", .desc = "Total number of failed connections to server since the worker process started" },
179 [ST_F_ERESP] = { .name = "eresp", .desc = "Total number of invalid responses since the worker process started" },
180 [ST_F_WRETR] = { .name = "wretr", .desc = "Total number of server connection retries since the worker process started" },
181 [ST_F_WREDIS] = { .name = "wredis", .desc = "Total number of server redispatches due to connection failures since the worker process started" },
182 [ST_F_STATUS] = { .name = "status", .desc = "Frontend/listen status: OPEN/WAITING/FULL/STOP; backend: UP/DOWN; server: last check status" },
Willy Tarreaubd715102020-10-23 22:44:30 +0200183 [ST_F_WEIGHT] = { .name = "weight", .desc = "Server's effective weight, or sum of active servers' effective weights for a backend" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200184 [ST_F_ACT] = { .name = "act", .desc = "Total number of active UP servers with a non-zero weight" },
185 [ST_F_BCK] = { .name = "bck", .desc = "Total number of backup UP servers with a non-zero weight" },
186 [ST_F_CHKFAIL] = { .name = "chkfail", .desc = "Total number of failed individual health checks per server/backend, since the worker process started" },
187 [ST_F_CHKDOWN] = { .name = "chkdown", .desc = "Total number of failed checks causing UP to DOWN server transitions, per server/backend, since the worker process started" },
188 [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
189 [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
190 [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
Willy Tarreaue8422bf2021-06-15 09:08:18 +0200191 [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1)" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200192 [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
193 [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
194 [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
195 [ST_F_LBTOT] = { .name = "lbtot", .desc = "Total number of requests routed by load balancing since the worker process started (ignores queue pop and stickiness)" },
196 [ST_F_TRACKED] = { .name = "tracked", .desc = "Name of the other server this server tracks for its state" },
197 [ST_F_TYPE] = { .name = "type", .desc = "Type of the object (Listener, Frontend, Backend, Server)" },
198 [ST_F_RATE] = { .name = "rate", .desc = "Total number of sessions processed by this object over the last second (sessions for listeners/frontends, requests for backends/servers)" },
199 [ST_F_RATE_LIM] = { .name = "rate_lim", .desc = "Limit on the number of sessions accepted in a second (frontend only, 'rate-limit sessions' setting)" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100200 [ST_F_RATE_MAX] = { .name = "rate_max", .desc = "Highest value of sessions per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200201 [ST_F_CHECK_STATUS] = { .name = "check_status", .desc = "Status report of the server's latest health check, prefixed with '*' if a check is currently in progress" },
202 [ST_F_CHECK_CODE] = { .name = "check_code", .desc = "HTTP/SMTP/LDAP status code reported by the latest server health check" },
203 [ST_F_CHECK_DURATION] = { .name = "check_duration", .desc = "Total duration of the latest server health check, in milliseconds" },
204 [ST_F_HRSP_1XX] = { .name = "hrsp_1xx", .desc = "Total number of HTTP responses with status 100-199 returned by this object since the worker process started" },
205 [ST_F_HRSP_2XX] = { .name = "hrsp_2xx", .desc = "Total number of HTTP responses with status 200-299 returned by this object since the worker process started" },
206 [ST_F_HRSP_3XX] = { .name = "hrsp_3xx", .desc = "Total number of HTTP responses with status 300-399 returned by this object since the worker process started" },
207 [ST_F_HRSP_4XX] = { .name = "hrsp_4xx", .desc = "Total number of HTTP responses with status 400-499 returned by this object since the worker process started" },
208 [ST_F_HRSP_5XX] = { .name = "hrsp_5xx", .desc = "Total number of HTTP responses with status 500-599 returned by this object since the worker process started" },
209 [ST_F_HRSP_OTHER] = { .name = "hrsp_other", .desc = "Total number of HTTP responses with status <100, >599 returned by this object since the worker process started (error -1 included)" },
210 [ST_F_HANAFAIL] = { .name = "hanafail", .desc = "Total number of failed checks caused by an 'on-error' directive after an 'observe' condition matched" },
211 [ST_F_REQ_RATE] = { .name = "req_rate", .desc = "Number of HTTP requests processed over the last second on this object" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100212 [ST_F_REQ_RATE_MAX] = { .name = "req_rate_max", .desc = "Highest value of http requests observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200213 [ST_F_REQ_TOT] = { .name = "req_tot", .desc = "Total number of HTTP requests processed by this object since the worker process started" },
214 [ST_F_CLI_ABRT] = { .name = "cli_abrt", .desc = "Total number of requests or connections aborted by the client since the worker process started" },
215 [ST_F_SRV_ABRT] = { .name = "srv_abrt", .desc = "Total number of requests or connections aborted by the server since the worker process started" },
216 [ST_F_COMP_IN] = { .name = "comp_in", .desc = "Total number of bytes submitted to the HTTP compressor for this object since the worker process started" },
217 [ST_F_COMP_OUT] = { .name = "comp_out", .desc = "Total number of bytes emitted by the HTTP compressor for this object since the worker process started" },
218 [ST_F_COMP_BYP] = { .name = "comp_byp", .desc = "Total number of bytes that bypassed HTTP compression for this object since the worker process started (CPU/memory/bandwidth limitation)" },
219 [ST_F_COMP_RSP] = { .name = "comp_rsp", .desc = "Total number of HTTP responses that were compressed for this object since the worker process started" },
220 [ST_F_LASTSESS] = { .name = "lastsess", .desc = "How long ago some traffic was seen on this object on this worker process, in seconds" },
221 [ST_F_LAST_CHK] = { .name = "last_chk", .desc = "Short description of the latest health check report for this server (see also check_desc)" },
222 [ST_F_LAST_AGT] = { .name = "last_agt", .desc = "Short description of the latest agent check report for this server (see also agent_desc)" },
223 [ST_F_QTIME] = { .name = "qtime", .desc = "Time spent in the queue, in milliseconds, averaged over the 1024 last requests (backend/server)" },
224 [ST_F_CTIME] = { .name = "ctime", .desc = "Time spent waiting for a connection to complete, in milliseconds, averaged over the 1024 last requests (backend/server)" },
225 [ST_F_RTIME] = { .name = "rtime", .desc = "Time spent waiting for a server response, in milliseconds, averaged over the 1024 last requests (backend/server)" },
226 [ST_F_TTIME] = { .name = "ttime", .desc = "Total request+response time (request+queue+connect+response+processing), in milliseconds, averaged over the 1024 last requests (backend/server)" },
227 [ST_F_AGENT_STATUS] = { .name = "agent_status", .desc = "Status report of the server's latest agent check, prefixed with '*' if a check is currently in progress" },
228 [ST_F_AGENT_CODE] = { .name = "agent_code", .desc = "Status code reported by the latest server agent check" },
229 [ST_F_AGENT_DURATION] = { .name = "agent_duration", .desc = "Total duration of the latest server agent check, in milliseconds" },
230 [ST_F_CHECK_DESC] = { .name = "check_desc", .desc = "Textual description of the latest health check report for this server" },
231 [ST_F_AGENT_DESC] = { .name = "agent_desc", .desc = "Textual description of the latest agent check report for this server" },
232 [ST_F_CHECK_RISE] = { .name = "check_rise", .desc = "Number of successful health checks before declaring a server UP (server 'rise' setting)" },
233 [ST_F_CHECK_FALL] = { .name = "check_fall", .desc = "Number of failed health checks before declaring a server DOWN (server 'fall' setting)" },
234 [ST_F_CHECK_HEALTH] = { .name = "check_health", .desc = "Current server health check level (0..fall-1=DOWN, fall..rise-1=UP)" },
235 [ST_F_AGENT_RISE] = { .name = "agent_rise", .desc = "Number of successful agent checks before declaring a server UP (server 'rise' setting)" },
236 [ST_F_AGENT_FALL] = { .name = "agent_fall", .desc = "Number of failed agent checks before declaring a server DOWN (server 'fall' setting)" },
237 [ST_F_AGENT_HEALTH] = { .name = "agent_health", .desc = "Current server agent check level (0..fall-1=DOWN, fall..rise-1=UP)" },
238 [ST_F_ADDR] = { .name = "addr", .desc = "Server's address:port, shown only if show-legends is set, or at levels oper/admin for the CLI" },
239 [ST_F_COOKIE] = { .name = "cookie", .desc = "Backend's cookie name or Server's cookie value, shown only if show-legends is set, or at levels oper/admin for the CLI" },
240 [ST_F_MODE] = { .name = "mode", .desc = "'mode' setting (tcp/http/health/cli)" },
241 [ST_F_ALGO] = { .name = "algo", .desc = "Backend's load balancing algorithm, shown only if show-legends is set, or at levels oper/admin for the CLI" },
242 [ST_F_CONN_RATE] = { .name = "conn_rate", .desc = "Number of new connections accepted over the last second on the frontend for this worker process" },
William Dauchy19f7cfc2021-02-01 13:11:54 +0100243 [ST_F_CONN_RATE_MAX] = { .name = "conn_rate_max", .desc = "Highest value of connections per second observed since the worker process started" },
Willy Tarreau6d4897e2019-10-11 16:31:46 +0200244 [ST_F_CONN_TOT] = { .name = "conn_tot", .desc = "Total number of new connections accepted on this frontend since the worker process started" },
245 [ST_F_INTERCEPTED] = { .name = "intercepted", .desc = "Total number of HTTP requests intercepted on the frontend (redirects/stats/services) since the worker process started" },
246 [ST_F_DCON] = { .name = "dcon", .desc = "Total number of incoming connections blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
247 [ST_F_DSES] = { .name = "dses", .desc = "Total number of incoming sessions blocked on a listener/frontend by a tcp-request connection rule since the worker process started" },
248 [ST_F_WREW] = { .name = "wrew", .desc = "Total number of failed HTTP header rewrites since the worker process started" },
249 [ST_F_CONNECT] = { .name = "connect", .desc = "Total number of outgoing connection attempts on this backend/server since the worker process started" },
250 [ST_F_REUSE] = { .name = "reuse", .desc = "Total number of reused connection on this backend/server since the worker process started" },
251 [ST_F_CACHE_LOOKUPS] = { .name = "cache_lookups", .desc = "Total number of HTTP requests looked up in the cache on this frontend/backend since the worker process started" },
252 [ST_F_CACHE_HITS] = { .name = "cache_hits", .desc = "Total number of HTTP requests not found in the cache on this frontend/backend since the worker process started" },
253 [ST_F_SRV_ICUR] = { .name = "srv_icur", .desc = "Current number of idle connections available for reuse on this server" },
254 [ST_F_SRV_ILIM] = { .name = "src_ilim", .desc = "Limit on the number of available idle connections on this server (server 'pool_max_conn' directive)" },
Christopher Faulet0d1c2a62019-11-08 14:59:51 +0100255 [ST_F_QT_MAX] = { .name = "qtime_max", .desc = "Maximum observed time spent in the queue, in milliseconds (backend/server)" },
256 [ST_F_CT_MAX] = { .name = "ctime_max", .desc = "Maximum observed time spent waiting for a connection to complete, in milliseconds (backend/server)" },
257 [ST_F_RT_MAX] = { .name = "rtime_max", .desc = "Maximum observed time spent waiting for a server response, in milliseconds (backend/server)" },
258 [ST_F_TT_MAX] = { .name = "ttime_max", .desc = "Maximum observed total request+response time (request+queue+connect+response+processing), in milliseconds (backend/server)" },
Christopher Faulet0159ee42019-12-16 14:40:39 +0100259 [ST_F_EINT] = { .name = "eint", .desc = "Total number of internal errors since process started"},
Willy Tarreau3bb617c2020-06-29 13:51:05 +0200260 [ST_F_IDLE_CONN_CUR] = { .name = "idle_conn_cur", .desc = "Current number of unsafe idle connections"},
261 [ST_F_SAFE_CONN_CUR] = { .name = "safe_conn_cur", .desc = "Current number of safe idle connections"},
262 [ST_F_USED_CONN_CUR] = { .name = "used_conn_cur", .desc = "Current number of connections in use"},
Willy Tarreaua9fcecb2020-06-29 15:38:53 +0200263 [ST_F_NEED_CONN_EST] = { .name = "need_conn_est", .desc = "Estimated needed number of connections"},
Willy Tarreaubd715102020-10-23 22:44:30 +0200264 [ST_F_UWEIGHT] = { .name = "uweight", .desc = "Server's user weight, or sum of active servers' user weights for a backend" },
Cedric Paillet7d6644e2022-12-08 09:17:00 +0000265 [ST_F_AGG_SRV_CHECK_STATUS] = { .name = "agg_server_check_status", .desc = "[DEPRECATED] Backend's aggregated gauge of servers' status" },
266 [ST_F_AGG_SRV_STATUS ] = { .name = "agg_server_status", .desc = "Backend's aggregated gauge of servers' status" },
Cedric Paillete06e31e2022-12-08 09:17:01 +0000267 [ST_F_AGG_CHECK_STATUS] = { .name = "agg_check_status", .desc = "Backend's aggregated gauge of servers' state check status" },
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +0100268 [ST_F_SRID] = { .name = "srid", .desc = "Server id revision, to prevent server id reuse mixups" },
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100269 [ST_F_SESS_OTHER] = { .name = "sess_other", .desc = "Total number of sessions other than HTTP since process started" },
270 [ST_F_H1SESS] = { .name = "h1sess", .desc = "Total number of HTTP/1 sessions since process started" },
271 [ST_F_H2SESS] = { .name = "h2sess", .desc = "Total number of HTTP/2 sessions since process started" },
272 [ST_F_H3SESS] = { .name = "h3sess", .desc = "Total number of HTTP/3 sessions since process started" },
273 [ST_F_REQ_OTHER] = { .name = "req_other", .desc = "Total number of sessions other than HTTP processed by this object since the worker process started" },
274 [ST_F_H1REQ] = { .name = "h1req", .desc = "Total number of HTTP/1 sessions processed by this object since the worker process started" },
275 [ST_F_H2REQ] = { .name = "h2req", .desc = "Total number of hTTP/2 sessions processed by this object since the worker process started" },
276 [ST_F_H3REQ] = { .name = "h3req", .desc = "Total number of HTTP/3 sessions processed by this object since the worker process started" },
William Lallemand74c24fb2016-11-21 17:18:36 +0100277};
278
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100279/* one line of info */
William Dauchy5d9b8f32021-01-11 20:07:49 +0100280THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200281
282/* description of statistics (static and dynamic) */
283static struct name_desc *stat_f[STATS_DOMAIN_COUNT];
284static size_t stat_count[STATS_DOMAIN_COUNT];
285
286/* one line for stats */
William Dauchyb9577452021-01-17 18:27:46 +0100287THREAD_LOCAL struct field *stat_l[STATS_DOMAIN_COUNT];
William Lallemand74c24fb2016-11-21 17:18:36 +0100288
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200289/* list of all registered stats module */
290static struct list stats_module_list[STATS_DOMAIN_COUNT] = {
291 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_PROXY]),
Emeric Brunf8642ee2021-10-29 17:59:18 +0200292 LIST_HEAD_INIT(stats_module_list[STATS_DOMAIN_RESOLVERS]),
Amaury Denoyelle58d395e2020-10-05 11:49:40 +0200293};
294
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100295THREAD_LOCAL void *trash_counters;
Christopher Fauleta8b76842022-12-16 15:08:36 +0100296static THREAD_LOCAL struct buffer trash_chunk = BUF_NULL;
297
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +0100298
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200299static inline uint8_t stats_get_domain(uint32_t domain)
300{
301 return domain >> STATS_DOMAIN & STATS_DOMAIN_MASK;
302}
303
Amaury Denoyelle72b16e52020-10-05 11:49:38 +0200304static inline enum stats_domain_px_cap stats_px_get_cap(uint32_t domain)
305{
306 return domain >> STATS_PX_CAP & STATS_PX_CAP_MASK;
307}
308
Christopher Faulet6338a082019-09-09 15:50:54 +0200309static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100310
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200311int stats_putchk(struct appctx *appctx, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +0100312{
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200313 struct stconn *sc = appctx_sc(appctx);
314 struct channel *chn = sc_ic(sc);
Aurelien DARRAGONe5958d02023-02-02 17:27:27 +0100315 struct buffer *chk = &trash_chunk;
316
Christopher Fauletef779222018-10-31 08:47:01 +0100317 if (htx) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200318 if (chk->data >= channel_htx_recv_max(chn, htx)) {
319 sc_need_room(sc, chk->data);
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100320 return 0;
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200321 }
322 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data))) {
323 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +0100324 return 0;
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200325 }
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100326 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100327 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100328 }
329 else {
Christopher Fauletf4258bd2023-05-05 10:59:39 +0200330 if (applet_putchk(appctx, chk) == -1)
Christopher Fauletef779222018-10-31 08:47:01 +0100331 return 0;
332 }
333 return 1;
334}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100335
Willy Tarreaucaff6312022-05-27 10:17:46 +0200336static const char *stats_scope_ptr(struct appctx *appctx, struct stconn *sc)
Christopher Fauleted7a0662019-01-14 11:07:34 +0100337{
Willy Tarreau91cefca2022-05-03 17:08:29 +0200338 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +0200339 struct channel *req = sc_oc(sc);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200340 struct htx *htx = htxbuf(&req->buf);
341 struct htx_blk *blk;
342 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100343
Christopher Fauletb7f88902019-07-15 21:56:43 +0200344 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +0100345 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb7f88902019-07-15 21:56:43 +0200346 ALREADY_CHECKED(blk);
347 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Willy Tarreau91cefca2022-05-03 17:08:29 +0200348 return uri.ptr + ctx->scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100349}
350
William Lallemand74c24fb2016-11-21 17:18:36 +0100351/*
352 * http_stats_io_handler()
353 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
354 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100355 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100356 * -> stats_dump_html_head() // emits the HTML headers
357 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
358 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
359 * -> stats_dump_html_px_hdr()
360 * -> stats_dump_fe_stats()
361 * -> stats_dump_li_stats()
362 * -> stats_dump_sv_stats()
363 * -> stats_dump_be_stats()
364 * -> stats_dump_html_px_end()
365 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100366 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100367 */
368
369
Christopher Fauleta8b76842022-12-16 15:08:36 +0100370/* Dumps the stats CSV header to the local trash buffer. The caller is
371 * responsible for clearing it if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +0100372 * NOTE: Some tools happen to rely on the field position instead of its name,
373 * so please only append new fields at the end, never in the middle.
374 */
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200375static void stats_dump_csv_header(enum stats_domain domain)
William Lallemand74c24fb2016-11-21 17:18:36 +0100376{
377 int field;
378
Christopher Fauleta8b76842022-12-16 15:08:36 +0100379 chunk_appendf(&trash_chunk, "# ");
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200380 if (stat_f[domain]) {
381 for (field = 0; field < stat_count[domain]; ++field) {
Christopher Fauleta8b76842022-12-16 15:08:36 +0100382 chunk_appendf(&trash_chunk, "%s,", stat_f[domain][field].name);
William Lallemand74c24fb2016-11-21 17:18:36 +0100383
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200384 /* print special delimiter on proxy stats to mark end of
385 static fields */
386 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS)
Christopher Fauleta8b76842022-12-16 15:08:36 +0100387 chunk_appendf(&trash_chunk, "-,");
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200388 }
389 }
390
Christopher Fauleta8b76842022-12-16 15:08:36 +0100391 chunk_appendf(&trash_chunk, "\n");
William Lallemand74c24fb2016-11-21 17:18:36 +0100392}
393
William Lallemand74c24fb2016-11-21 17:18:36 +0100394/* Emits a stats field without any surrounding element and properly encoded to
395 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
396 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200397int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100398{
399 switch (field_format(f, 0)) {
400 case FF_EMPTY: return 1;
401 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
402 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
403 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
404 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200405 case FF_FLT: {
406 size_t prev_data = out->data;
407 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
408 return out->data;
409 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100410 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
411 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
412 }
413}
414
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200415const char *field_to_html_str(const struct field *f)
416{
417 switch (field_format(f, 0)) {
418 case FF_S32: return U2H(f->u.s32);
419 case FF_S64: return U2H(f->u.s64);
420 case FF_U64: return U2H(f->u.u64);
421 case FF_U32: return U2H(f->u.u32);
Willy Tarreau6004fb72021-05-08 07:37:38 +0200422 case FF_FLT: return F2H(f->u.flt);
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200423 case FF_STR: return field_str(f, 0);
424 case FF_EMPTY:
425 default:
426 return "";
427 }
428}
429
William Lallemand74c24fb2016-11-21 17:18:36 +0100430/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
431 * output is supposed to be used on its own line. Returns non-zero on success, 0
432 * if the buffer is full.
433 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200434int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100435{
436 switch (field_format(f, 0)) {
437 case FF_EMPTY: return 1;
438 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
439 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
440 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
441 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Willy Tarreau065ba312021-05-08 10:38:20 +0200442 case FF_FLT: {
443 size_t prev_data = out->data;
444 out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
445 return out->data;
446 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100447 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
448 default: return chunk_appendf(out, "%08x:?", f->type);
449 }
450}
451
Simon Horman05ee2132017-01-04 09:37:25 +0100452/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
453 * the recommendation for interoperable integers in section 6 of RFC 7159.
454 */
455#define JSON_INT_MAX ((1ULL << 53) - 1)
456#define JSON_INT_MIN (0 - JSON_INT_MAX)
457
458/* Emits a stats field value and its type in JSON.
459 * Returns non-zero on success, 0 on error.
460 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200461int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100462{
463 int old_len;
464 char buf[20];
465 const char *type, *value = buf, *quote = "";
466
467 switch (field_format(f, 0)) {
468 case FF_EMPTY: return 1;
469 case FF_S32: type = "\"s32\"";
470 snprintf(buf, sizeof(buf), "%d", f->u.s32);
471 break;
472 case FF_U32: type = "\"u32\"";
473 snprintf(buf, sizeof(buf), "%u", f->u.u32);
474 break;
475 case FF_S64: type = "\"s64\"";
476 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
477 return 0;
478 type = "\"s64\"";
479 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
480 break;
481 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
482 return 0;
483 type = "\"u64\"";
484 snprintf(buf, sizeof(buf), "%llu",
485 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200486 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200487 case FF_FLT: type = "\"flt\"";
Willy Tarreau065ba312021-05-08 10:38:20 +0200488 flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
Simon Horman05ee2132017-01-04 09:37:25 +0100489 break;
490 case FF_STR: type = "\"str\"";
491 value = field_str(f, 0);
492 quote = "\"";
493 break;
494 default: snprintf(buf, sizeof(buf), "%u", f->type);
495 type = buf;
496 value = "unknown";
497 quote = "\"";
498 break;
499 }
500
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100502 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
503 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200504 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100505}
506
William Lallemand74c24fb2016-11-21 17:18:36 +0100507/* Emits an encoding of the field type on 3 characters followed by a delimiter.
508 * Returns non-zero on success, 0 if the buffer is full.
509 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200510int stats_emit_field_tags(struct buffer *out, const struct field *f,
511 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100512{
513 char origin, nature, scope;
514
515 switch (field_origin(f, 0)) {
516 case FO_METRIC: origin = 'M'; break;
517 case FO_STATUS: origin = 'S'; break;
518 case FO_KEY: origin = 'K'; break;
519 case FO_CONFIG: origin = 'C'; break;
520 case FO_PRODUCT: origin = 'P'; break;
521 default: origin = '?'; break;
522 }
523
524 switch (field_nature(f, 0)) {
525 case FN_GAUGE: nature = 'G'; break;
526 case FN_LIMIT: nature = 'L'; break;
527 case FN_MIN: nature = 'm'; break;
528 case FN_MAX: nature = 'M'; break;
529 case FN_RATE: nature = 'R'; break;
530 case FN_COUNTER: nature = 'C'; break;
531 case FN_DURATION: nature = 'D'; break;
532 case FN_AGE: nature = 'A'; break;
533 case FN_TIME: nature = 'T'; break;
534 case FN_NAME: nature = 'N'; break;
535 case FN_OUTPUT: nature = 'O'; break;
536 case FN_AVG: nature = 'a'; break;
537 default: nature = '?'; break;
538 }
539
540 switch (field_scope(f, 0)) {
541 case FS_PROCESS: scope = 'P'; break;
542 case FS_SERVICE: scope = 'S'; break;
543 case FS_SYSTEM: scope = 's'; break;
544 case FS_CLUSTER: scope = 'C'; break;
545 default: scope = '?'; break;
546 }
547
548 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
549}
550
Simon Horman05ee2132017-01-04 09:37:25 +0100551/* Emits an encoding of the field type as JSON.
552 * Returns non-zero on success, 0 if the buffer is full.
553 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200554int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100555{
556 const char *origin, *nature, *scope;
557 int old_len;
558
559 switch (field_origin(f, 0)) {
560 case FO_METRIC: origin = "Metric"; break;
561 case FO_STATUS: origin = "Status"; break;
562 case FO_KEY: origin = "Key"; break;
563 case FO_CONFIG: origin = "Config"; break;
564 case FO_PRODUCT: origin = "Product"; break;
565 default: origin = "Unknown"; break;
566 }
567
568 switch (field_nature(f, 0)) {
569 case FN_GAUGE: nature = "Gauge"; break;
570 case FN_LIMIT: nature = "Limit"; break;
571 case FN_MIN: nature = "Min"; break;
572 case FN_MAX: nature = "Max"; break;
573 case FN_RATE: nature = "Rate"; break;
574 case FN_COUNTER: nature = "Counter"; break;
575 case FN_DURATION: nature = "Duration"; break;
576 case FN_AGE: nature = "Age"; break;
577 case FN_TIME: nature = "Time"; break;
578 case FN_NAME: nature = "Name"; break;
579 case FN_OUTPUT: nature = "Output"; break;
580 case FN_AVG: nature = "Avg"; break;
581 default: nature = "Unknown"; break;
582 }
583
584 switch (field_scope(f, 0)) {
585 case FS_PROCESS: scope = "Process"; break;
586 case FS_SERVICE: scope = "Service"; break;
587 case FS_SYSTEM: scope = "System"; break;
588 case FS_CLUSTER: scope = "Cluster"; break;
589 default: scope = "Unknown"; break;
590 }
591
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200592 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100593 chunk_appendf(out, "\"tags\":{"
594 "\"origin\":\"%s\","
595 "\"nature\":\"%s\","
596 "\"scope\":\"%s\""
597 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200598 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100599}
William Lallemand74c24fb2016-11-21 17:18:36 +0100600
601/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200602static int stats_dump_fields_csv(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200603 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100604 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100605{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100606 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100607 int field;
608
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200609 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100610 if (!stats_emit_raw_data_field(out, &stats[field]))
611 return 0;
612 if (!chunk_strcat(out, ","))
613 return 0;
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200614
615 /* print special delimiter on proxy stats to mark end of
616 static fields */
617 if (domain == STATS_DOMAIN_PROXY && field + 1 == ST_F_TOTAL_FIELDS) {
618 if (!chunk_strcat(out, "-,"))
619 return 0;
620 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100621 }
Amaury Denoyelle50660a82020-10-05 11:49:39 +0200622
William Lallemand74c24fb2016-11-21 17:18:36 +0100623 chunk_strcat(out, "\n");
624 return 1;
625}
626
627/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200628static int stats_dump_fields_typed(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200629 const struct field *stats,
630 size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100631 struct show_stat_ctx * ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100632{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100633 int flags = ctx->flags;
634 int domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +0100635 int field;
636
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200637 for (field = 0; field < stats_count; ++field) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100638 if (!stats[field].type)
639 continue;
640
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200641 switch (domain) {
642 case STATS_DOMAIN_PROXY:
643 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
644 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
645 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
646 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
647 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
648 '?',
649 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
650 field,
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200651 stat_f[domain][field].name,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200652 stats[ST_F_PID].u.u32);
653 break;
654
Emeric Brunf8642ee2021-10-29 17:59:18 +0200655 case STATS_DOMAIN_RESOLVERS:
656 chunk_appendf(out, "N.%d.%s:", field,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200657 stat_f[domain][field].name);
658 break;
659
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200660 default:
661 break;
662 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100663
664 if (!stats_emit_field_tags(out, &stats[field], ':'))
665 return 0;
666 if (!stats_emit_typed_data_field(out, &stats[field]))
667 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200668
Willy Tarreaufc8e4382021-06-17 07:22:27 +0200669 if (flags & STAT_SHOW_FDESC &&
670 !chunk_appendf(out, ":\"%s\"", stat_f[domain][field].desc)) {
Willy Tarreau6b19b142019-10-09 15:44:21 +0200671 return 0;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200672 }
673
William Lallemand74c24fb2016-11-21 17:18:36 +0100674 if (!chunk_strcat(out, "\n"))
675 return 0;
676 }
677 return 1;
678}
679
Simon Horman05ee2132017-01-04 09:37:25 +0100680/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200681static int stats_dump_json_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100682 const struct field *info,
683 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100684{
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100685 int started = (ctx->field) ? 1 : 0;
686 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100687
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100688 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100689 return 0;
690
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100691 for (; ctx->field < INF_TOTAL_FIELDS; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100692 int old_len;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100693 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100694
695 if (!field_format(info, field))
696 continue;
697
698 if (started && !chunk_strcat(out, ","))
699 goto err;
700 started = 1;
701
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200702 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100703 chunk_appendf(out,
704 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
705 "\"processNum\":%u,",
Willy Tarreaueaa55372019-10-09 07:39:11 +0200706 field, info_fields[field].name,
Simon Horman05ee2132017-01-04 09:37:25 +0100707 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200708 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100709 goto err;
710
711 if (!stats_emit_json_field_tags(out, &info[field]))
712 goto err;
713
714 if (!stats_emit_json_data_field(out, &info[field]))
715 goto err;
716
717 if (!chunk_strcat(out, "}"))
718 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100719 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100720 }
721
Willy Tarreauc6b7a972022-06-10 15:12:21 +0200722 if (!chunk_strcat(out, "]\n"))
Simon Horman05ee2132017-01-04 09:37:25 +0100723 goto err;
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100724 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100725 return 1;
726
727err:
Aurelien DARRAGON16c9ca92022-12-15 14:24:30 +0100728 if (!ready_data) {
729 /* not enough buffer space for a single entry.. */
730 chunk_reset(out);
731 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
732 return 0; /* hard error */
733 }
734 /* push ready data and wait for a new buffer to complete the dump */
735 out->data = ready_data;
736 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100737}
738
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200739static void stats_print_proxy_field_json(struct buffer *out,
740 const struct field *stat,
741 const char *name,
742 int pos,
743 uint32_t field_type,
744 uint32_t iid,
745 uint32_t sid,
746 uint32_t pid)
747{
748 const char *obj_type;
749 switch (field_type) {
750 case STATS_TYPE_FE: obj_type = "Frontend"; break;
751 case STATS_TYPE_BE: obj_type = "Backend"; break;
752 case STATS_TYPE_SO: obj_type = "Listener"; break;
753 case STATS_TYPE_SV: obj_type = "Server"; break;
754 default: obj_type = "Unknown"; break;
755 }
756
757 chunk_appendf(out,
758 "{"
759 "\"objType\":\"%s\","
760 "\"proxyId\":%u,"
761 "\"id\":%u,"
762 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
763 "\"processNum\":%u,",
764 obj_type, iid, sid, pos, name, pid);
765}
766
Emeric Brunf8642ee2021-10-29 17:59:18 +0200767static void stats_print_rslv_field_json(struct buffer *out,
768 const struct field *stat,
769 const char *name,
770 int pos)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200771{
772 chunk_appendf(out,
773 "{"
774 "\"field\":{\"pos\":%d,\"name\":\"%s\"},",
775 pos, name);
776}
777
778
Simon Horman05ee2132017-01-04 09:37:25 +0100779/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200780static int stats_dump_fields_json(struct buffer *out,
Amaury Denoyelle97323c92020-10-02 18:32:01 +0200781 const struct field *stats, size_t stats_count,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100782 struct show_stat_ctx *ctx)
Simon Horman05ee2132017-01-04 09:37:25 +0100783{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100784 int flags = ctx->flags;
785 int domain = ctx->domain;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100786 int started = (ctx->field) ? 1 : 0;
787 int ready_data = 0;
Simon Horman05ee2132017-01-04 09:37:25 +0100788
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100789 if (!started && (flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100790 return 0;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100791 if (!started && !chunk_strcat(out, "["))
Simon Horman05ee2132017-01-04 09:37:25 +0100792 return 0;
793
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100794 for (; ctx->field < stats_count; ctx->field++) {
Simon Horman05ee2132017-01-04 09:37:25 +0100795 int old_len;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100796 int field = ctx->field;
Simon Horman05ee2132017-01-04 09:37:25 +0100797
798 if (!stats[field].type)
799 continue;
800
801 if (started && !chunk_strcat(out, ","))
802 goto err;
803 started = 1;
804
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200805 old_len = out->data;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200806 if (domain == STATS_DOMAIN_PROXY) {
807 stats_print_proxy_field_json(out, &stats[field],
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +0200808 stat_f[domain][field].name,
809 field,
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200810 stats[ST_F_TYPE].u.u32,
811 stats[ST_F_IID].u.u32,
812 stats[ST_F_SID].u.u32,
813 stats[ST_F_PID].u.u32);
Emeric Brunf8642ee2021-10-29 17:59:18 +0200814 } else if (domain == STATS_DOMAIN_RESOLVERS) {
815 stats_print_rslv_field_json(out, &stats[field],
816 stat_f[domain][field].name,
817 field);
Amaury Denoyelle072f97e2020-10-05 11:49:37 +0200818 }
Amaury Denoyellef34017b2020-10-02 18:32:03 +0200819
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200820 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100821 goto err;
822
823 if (!stats_emit_json_field_tags(out, &stats[field]))
824 goto err;
825
826 if (!stats_emit_json_data_field(out, &stats[field]))
827 goto err;
828
829 if (!chunk_strcat(out, "}"))
830 goto err;
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100831 ready_data = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100832 }
833
834 if (!chunk_strcat(out, "]"))
835 goto err;
836
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100837 ctx->field = 0; /* we're done */
Simon Horman05ee2132017-01-04 09:37:25 +0100838 return 1;
839
840err:
Aurelien DARRAGON42b18fb2022-12-15 14:17:09 +0100841 if (!ready_data) {
842 /* not enough buffer space for a single entry.. */
843 chunk_reset(out);
844 if (ctx->flags & STAT_STARTED)
845 chunk_strcat(out, ",");
846 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
847 return 0; /* hard error */
848 }
849 /* push ready data and wait for a new buffer to complete the dump */
850 out->data = ready_data;
851 return 1;
Simon Horman05ee2132017-01-04 09:37:25 +0100852}
853
William Lallemand74c24fb2016-11-21 17:18:36 +0100854/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200855 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +0200856 * are provided if STAT_SHLGNDS is present in <flags>. The statistics from
857 * extra modules are displayed at the end of the lines if STAT_SHMODULES is
858 * present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100859 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200860static int stats_dump_fields_html(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100861 const struct field *stats,
862 struct show_stat_ctx *ctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100863{
Willy Tarreau83061a82018-07-13 11:56:34 +0200864 struct buffer src;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200865 struct stats_module *mod;
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +0100866 int flags = ctx->flags;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +0200867 int i = 0, j = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100868
869 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
870 chunk_appendf(out,
871 /* name, queue */
872 "<tr class=\"frontend\">");
873
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200874 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100875 /* Column sub-heading for Enable or Disable server */
876 chunk_appendf(out, "<td></td>");
877 }
878
879 chunk_appendf(out,
880 "<td class=ac>"
881 "<a name=\"%s/Frontend\"></a>"
882 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
883 "<td colspan=3></td>"
884 "",
885 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
886
887 chunk_appendf(out,
888 /* sessions rate : current */
889 "<td><u>%s<div class=tips><table class=det>"
890 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
891 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
892 "",
893 U2H(stats[ST_F_RATE].u.u32),
894 U2H(stats[ST_F_CONN_RATE].u.u32),
895 U2H(stats[ST_F_RATE].u.u32));
896
897 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
898 chunk_appendf(out,
899 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
900 U2H(stats[ST_F_REQ_RATE].u.u32));
901
902 chunk_appendf(out,
903 "</table></div></u></td>"
904 /* sessions rate : max */
905 "<td><u>%s<div class=tips><table class=det>"
906 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
907 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
908 "",
909 U2H(stats[ST_F_RATE_MAX].u.u32),
910 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
911 U2H(stats[ST_F_RATE_MAX].u.u32));
912
913 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
914 chunk_appendf(out,
915 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
916 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
917
918 chunk_appendf(out,
919 "</table></div></u></td>"
920 /* sessions rate : limit */
921 "<td>%s</td>",
922 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
923
924 chunk_appendf(out,
925 /* sessions: current, max, limit, total */
926 "<td>%s</td><td>%s</td><td>%s</td>"
927 "<td><u>%s<div class=tips><table class=det>"
928 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
929 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
930 "",
931 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
932 U2H(stats[ST_F_STOT].u.u64),
933 U2H(stats[ST_F_CONN_TOT].u.u64),
934 U2H(stats[ST_F_STOT].u.u64));
935
936 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
937 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
938 chunk_appendf(out,
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100939 "<tr><th>- HTTP/1 sessions:</th><td>%s</td></tr>"
940 "<tr><th>- HTTP/2 sessions:</th><td>%s</td></tr>"
941 "<tr><th>- HTTP/3 sessions:</th><td>%s</td></tr>"
942 "<tr><th>- other sessions:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100943 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100944 "<tr><th>- HTTP/1 requests:</th><td>%s</td></tr>"
945 "<tr><th>- HTTP/2 requests:</th><td>%s</td></tr>"
946 "<tr><th>- HTTP/3 requests:</th><td>%s</td></tr>"
947 "<tr><th>- other requests:</th><td>%s</td></tr>"
948 "",
949 U2H(stats[ST_F_H1SESS].u.u64),
950 U2H(stats[ST_F_H2SESS].u.u64),
951 U2H(stats[ST_F_H3SESS].u.u64),
952 U2H(stats[ST_F_SESS_OTHER].u.u64),
953 U2H(stats[ST_F_REQ_TOT].u.u64),
954 U2H(stats[ST_F_H1REQ].u.u64),
955 U2H(stats[ST_F_H2REQ].u.u64),
956 U2H(stats[ST_F_H3REQ].u.u64),
957 U2H(stats[ST_F_REQ_OTHER].u.u64));
958
959 chunk_appendf(out,
William Lallemand74c24fb2016-11-21 17:18:36 +0100960 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
961 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
962 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
963 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
964 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
965 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
966 "<tr><th>- other responses:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100967 "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100968 U2H(stats[ST_F_HRSP_1XX].u.u64),
969 U2H(stats[ST_F_HRSP_2XX].u.u64),
970 U2H(stats[ST_F_COMP_RSP].u.u64),
971 stats[ST_F_HRSP_2XX].u.u64 ?
972 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
973 U2H(stats[ST_F_HRSP_3XX].u.u64),
974 U2H(stats[ST_F_HRSP_4XX].u.u64),
975 U2H(stats[ST_F_HRSP_5XX].u.u64),
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100976 U2H(stats[ST_F_HRSP_OTHER].u.u64));
977
978 chunk_appendf(out,
979 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
980 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
981 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
982 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
983 "<tr><th>Internal errors:</th><td>%s</td></tr>"
984 "",
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200985 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100986 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
987 U2H(stats[ST_F_CACHE_HITS].u.u64),
988 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
989 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +0100990 U2H(stats[ST_F_WREW].u.u64),
991 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100992 }
993
994 chunk_appendf(out,
995 "</table></div></u></td>"
996 /* sessions: lbtot, lastsess */
997 "<td></td><td></td>"
998 /* bytes : in */
999 "<td>%s</td>"
1000 "",
1001 U2H(stats[ST_F_BIN].u.u64));
1002
1003 chunk_appendf(out,
1004 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1005 "<td>%s%s<div class=tips><table class=det>"
1006 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1007 "<tr><th>Compression in:</th><td>%s</td></tr>"
1008 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1009 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1010 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1011 "</table></div>%s</td>",
1012 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1013 U2H(stats[ST_F_BOUT].u.u64),
1014 U2H(stats[ST_F_BOUT].u.u64),
1015 U2H(stats[ST_F_COMP_IN].u.u64),
1016 U2H(stats[ST_F_COMP_OUT].u.u64),
1017 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1018 U2H(stats[ST_F_COMP_BYP].u.u64),
1019 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1020 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,
1021 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1022
1023 chunk_appendf(out,
1024 /* denied: req, resp */
1025 "<td>%s</td><td>%s</td>"
1026 /* errors : request, connect, response */
1027 "<td>%s</td><td></td><td></td>"
1028 /* warnings: retries, redispatches */
1029 "<td></td><td></td>"
1030 /* server status : reflect frontend status */
1031 "<td class=ac>%s</td>"
1032 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001033 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001034 "",
1035 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1036 U2H(stats[ST_F_EREQ].u.u64),
1037 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001038
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001039 if (flags & STAT_SHMODULES) {
1040 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001041 chunk_appendf(out, "<td>");
1042
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001043 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001044 chunk_appendf(out,
1045 "<u>%s<div class=tips><table class=det>",
1046 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001047 for (j = 0; j < mod->stats_count; ++j) {
1048 chunk_appendf(out,
1049 "<tr><th>%s</th><td>%s</td></tr>",
1050 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1051 ++i;
1052 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001053 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001054 } else {
1055 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001056 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001057
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001058 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001059 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001060 }
1061
1062 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001063 }
1064 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
1065 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001066 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001067 /* Column sub-heading for Enable or Disable server */
1068 chunk_appendf(out, "<td></td>");
1069 }
1070
1071 chunk_appendf(out,
1072 /* frontend name, listener name */
1073 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1074 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1075 "",
1076 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001077 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001078 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1079
Willy Tarreau708c4162019-10-09 10:19:16 +02001080 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001081 chunk_appendf(out, "<div class=tips>");
1082
Willy Tarreau90807112020-02-25 08:16:33 +01001083 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001084 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1085 else if (*field_str(stats, ST_F_ADDR) == '[')
1086 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1087 else if (*field_str(stats, ST_F_ADDR))
1088 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1089
Willy Tarreau4cfb0012023-05-11 14:49:43 +02001090 chunk_appendf(out, "proto=%s, ", field_str(stats, ST_F_PROTO));
1091
William Lallemand74c24fb2016-11-21 17:18:36 +01001092 /* id */
1093 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
1094 }
1095
1096 chunk_appendf(out,
1097 /* queue */
1098 "%s</td><td colspan=3></td>"
1099 /* sessions rate: current, max, limit */
1100 "<td colspan=3>&nbsp;</td>"
1101 /* sessions: current, max, limit, total, lbtot, lastsess */
1102 "<td>%s</td><td>%s</td><td>%s</td>"
1103 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
1104 /* bytes: in, out */
1105 "<td>%s</td><td>%s</td>"
1106 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001107 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001108 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
1109 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
1110
1111 chunk_appendf(out,
1112 /* denied: req, resp */
1113 "<td>%s</td><td>%s</td>"
1114 /* errors: request, connect, response */
1115 "<td>%s</td><td></td><td></td>"
1116 /* warnings: retries, redispatches */
1117 "<td></td><td></td>"
1118 /* server status: reflect listener status */
1119 "<td class=ac>%s</td>"
1120 /* rest of server: nothing */
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001121 "<td class=ac colspan=8></td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001122 "",
1123 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1124 U2H(stats[ST_F_EREQ].u.u64),
1125 field_str(stats, ST_F_STATUS));
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001126
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001127 if (flags & STAT_SHMODULES) {
1128 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001129 chunk_appendf(out, "<td>");
1130
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001131 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001132 chunk_appendf(out,
1133 "<u>%s<div class=tips><table class=det>",
1134 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001135 for (j = 0; j < mod->stats_count; ++j) {
1136 chunk_appendf(out,
1137 "<tr><th>%s</th><td>%s</td></tr>",
1138 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1139 ++i;
1140 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001141 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001142 } else {
1143 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001144 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001145
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001146 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001147 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001148 }
1149
1150 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001151 }
1152 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
1153 const char *style;
1154
1155 /* determine the style to use depending on the server's state,
1156 * its health and weight. There isn't a 1-to-1 mapping between
1157 * state and styles for the cases where the server is (still)
1158 * up. The reason is that we don't want to report nolb and
1159 * drain with the same color.
1160 */
1161
1162 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
1163 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
1164 style = "down";
1165 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001166 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001167 style = "going_up";
1168 }
Daniel Corbettb4285172020-03-28 12:35:50 -04001169 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
1170 style = "draining";
1171 }
Florian Apolloner39272c22021-03-30 13:28:35 +02001172 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001173 style = "going_down";
1174 }
1175 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
1176 style = "nolb";
1177 }
1178 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
1179 style = "no_check";
1180 }
1181 else if (!stats[ST_F_CHKFAIL].type ||
1182 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
1183 /* no check or max health = UP */
1184 if (stats[ST_F_WEIGHT].u.u32)
1185 style = "up";
1186 else
1187 style = "draining";
1188 }
1189 else {
1190 style = "going_down";
1191 }
1192
Willy Tarreau7b524852020-08-11 10:26:36 +02001193 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01001194 chunk_appendf(out, "<tr class=\"maintain\">");
1195 else
1196 chunk_appendf(out,
1197 "<tr class=\"%s_%s\">",
1198 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
1199
1200
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001201 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +01001202 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +00001203 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
1204 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +01001205 field_str(stats, ST_F_SVNAME));
1206
1207 chunk_appendf(out,
1208 "<td class=ac><a name=\"%s/%s\"></a>%s"
1209 "<a class=lfsb href=\"#%s/%s\">%s</a>"
1210 "",
1211 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +02001212 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001213 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
1214
Willy Tarreau708c4162019-10-09 10:19:16 +02001215 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001216 chunk_appendf(out, "<div class=tips>");
1217
Willy Tarreau90807112020-02-25 08:16:33 +01001218 if (isdigit((unsigned char)*field_str(stats, ST_F_ADDR)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001219 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
1220 else if (*field_str(stats, ST_F_ADDR) == '[')
1221 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
1222 else if (*field_str(stats, ST_F_ADDR))
1223 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
1224
1225 /* id */
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01001226 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 +01001227
1228 /* cookie */
1229 if (stats[ST_F_COOKIE].type) {
1230 chunk_appendf(out, ", cookie: '");
1231 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1232 chunk_htmlencode(out, &src);
1233 chunk_appendf(out, "'");
1234 }
1235
1236 chunk_appendf(out, "</div>");
1237 }
1238
1239 chunk_appendf(out,
1240 /* queue : current, max, limit */
1241 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
1242 /* sessions rate : current, max, limit */
1243 "<td>%s</td><td>%s</td><td></td>"
1244 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001245 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01001246 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
1247 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1248
1249 chunk_appendf(out,
1250 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001251 "<td><u>%s<div class=tips>"
1252 "<table class=det>"
1253 "<tr><th>Current active connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001254 "<tr><th>Current used connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001255 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001256 "<tr><th>- unsafe:</th><td>%s</td></tr>"
1257 "<tr><th>- safe:</th><td>%s</td></tr>"
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001258 "<tr><th>Estimated need of connections:</th><td>%s</td></tr>"
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001259 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1260 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1261 "</table></div></u>"
1262 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001263 "<td><u>%s<div class=tips><table class=det>"
1264 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1265 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001266 U2H(stats[ST_F_SCUR].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001267 U2H(stats[ST_F_SCUR].u.u32),
1268 U2H(stats[ST_F_USED_CONN_CUR].u.u32),
1269 U2H(stats[ST_F_SRV_ICUR].u.u32),
1270 U2H(stats[ST_F_IDLE_CONN_CUR].u.u32),
1271 U2H(stats[ST_F_SAFE_CONN_CUR].u.u32),
Willy Tarreaua9fcecb2020-06-29 15:38:53 +02001272 U2H(stats[ST_F_NEED_CONN_EST].u.u32),
Willy Tarreau3bb617c2020-06-29 13:51:05 +02001273
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001274 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1275 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1276 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001277 U2H(stats[ST_F_STOT].u.u64),
1278 U2H(stats[ST_F_STOT].u.u64));
1279
1280 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1281 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001282 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001283 "<tr><th>New connections:</th><td>%s</td></tr>"
1284 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001285 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001286 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1287 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1288 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1289 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1290 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1291 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001292 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001293 "<tr><th>Internal error:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001294 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001295 U2H(stats[ST_F_CONNECT].u.u64),
1296 U2H(stats[ST_F_REUSE].u.u64),
1297 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1298 (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 +02001299 U2H(stats[ST_F_REQ_TOT].u.u64),
1300 U2H(stats[ST_F_HRSP_1XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1301 (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1302 U2H(stats[ST_F_HRSP_2XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1303 (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1304 U2H(stats[ST_F_HRSP_3XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1305 (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1306 U2H(stats[ST_F_HRSP_4XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1307 (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1308 U2H(stats[ST_F_HRSP_5XX].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1309 (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
1310 U2H(stats[ST_F_HRSP_OTHER].u.u64), stats[ST_F_REQ_TOT].u.u64 ?
1311 (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / stats[ST_F_REQ_TOT].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001312 U2H(stats[ST_F_WREW].u.u64),
1313 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001314 }
1315
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001316 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1317 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1318 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1319 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1320 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001321 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001322 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1323 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1324 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1325 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001326
1327 chunk_appendf(out,
1328 "</table></div></u></td>"
1329 /* sessions: lbtot, last */
1330 "<td>%s</td><td>%s</td>",
1331 U2H(stats[ST_F_LBTOT].u.u64),
1332 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1333
1334 chunk_appendf(out,
1335 /* bytes : in, out */
1336 "<td>%s</td><td>%s</td>"
1337 /* denied: req, resp */
1338 "<td></td><td>%s</td>"
1339 /* errors : request, connect */
1340 "<td></td><td>%s</td>"
1341 /* errors : response */
1342 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1343 /* warnings: retries, redispatches */
1344 "<td>%lld</td><td>%lld</td>"
1345 "",
1346 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1347 U2H(stats[ST_F_DRESP].u.u64),
1348 U2H(stats[ST_F_ECON].u.u64),
1349 U2H(stats[ST_F_ERESP].u.u64),
1350 (long long)stats[ST_F_CLI_ABRT].u.u64,
1351 (long long)stats[ST_F_SRV_ABRT].u.u64,
1352 (long long)stats[ST_F_WRETR].u.u64,
1353 (long long)stats[ST_F_WREDIS].u.u64);
1354
1355 /* status, last change */
1356 chunk_appendf(out, "<td class=ac>");
1357
1358 /* FIXME!!!!
1359 * LASTCHG should contain the last change for *this* server and must be computed
1360 * properly above, as was done below, ie: this server if maint, otherwise ref server
1361 * if tracking. Note that ref is either local or remote depending on tracking.
1362 */
1363
1364
Willy Tarreau7b524852020-08-11 10:26:36 +02001365 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001366 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1367 }
Willy Tarreau7b524852020-08-11 10:26:36 +02001368 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001369 chunk_strcat(out, "<i>no check</i>");
1370 }
1371 else {
1372 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 +02001373 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001374 if (stats[ST_F_CHECK_HEALTH].u.u32)
1375 chunk_strcat(out, " &uarr;");
1376 }
1377 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1378 chunk_strcat(out, " &darr;");
1379 }
1380
Willy Tarreau7b524852020-08-11 10:26:36 +02001381 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001382 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1383 chunk_appendf(out,
1384 "</td><td class=ac><u> %s",
1385 field_str(stats, ST_F_AGENT_STATUS));
1386
1387 if (stats[ST_F_AGENT_CODE].type)
1388 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1389
1390 if (stats[ST_F_AGENT_DURATION].type)
1391 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1392
1393 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1394
1395 if (*field_str(stats, ST_F_LAST_AGT)) {
1396 chunk_appendf(out, ": ");
1397 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1398 chunk_htmlencode(out, &src);
1399 }
1400 chunk_appendf(out, "</div></u>");
1401 }
1402 else if (stats[ST_F_CHECK_STATUS].type) {
1403 chunk_appendf(out,
1404 "</td><td class=ac><u> %s",
1405 field_str(stats, ST_F_CHECK_STATUS));
1406
1407 if (stats[ST_F_CHECK_CODE].type)
1408 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1409
1410 if (stats[ST_F_CHECK_DURATION].type)
1411 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1412
1413 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1414
1415 if (*field_str(stats, ST_F_LAST_CHK)) {
1416 chunk_appendf(out, ": ");
1417 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1418 chunk_htmlencode(out, &src);
1419 }
1420 chunk_appendf(out, "</div></u>");
1421 }
1422 else
1423 chunk_appendf(out, "</td><td>");
1424
1425 chunk_appendf(out,
Willy Tarreaubd715102020-10-23 22:44:30 +02001426 /* weight / uweight */
1427 "</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001428 /* act, bck */
1429 "<td class=ac>%s</td><td class=ac>%s</td>"
1430 "",
Willy Tarreaubd715102020-10-23 22:44:30 +02001431 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001432 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1433 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1434
1435 /* check failures: unique, fatal, down time */
1436 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1437 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1438 }
1439 else if (stats[ST_F_CHKFAIL].type) {
1440 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1441
1442 if (stats[ST_F_HANAFAIL].type)
1443 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1444
1445 chunk_appendf(out,
1446 "<div class=tips>Failed Health Checks%s</div></u></td>"
1447 "<td>%lld</td><td>%s</td>"
1448 "",
1449 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1450 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1451 }
1452 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1453 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1454 chunk_appendf(out,
1455 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1456 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1457 }
1458 else
1459 chunk_appendf(out, "<td colspan=3></td>");
1460
1461 /* throttle */
1462 if (stats[ST_F_THROTTLE].type)
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001463 chunk_appendf(out, "<td class=ac>%d %%</td>\n", stats[ST_F_THROTTLE].u.u32);
William Lallemand74c24fb2016-11-21 17:18:36 +01001464 else
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001465 chunk_appendf(out, "<td class=ac>-</td>");
1466
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001467 if (flags & STAT_SHMODULES) {
1468 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001469 chunk_appendf(out, "<td>");
1470
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001471 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001472 chunk_appendf(out,
1473 "<u>%s<div class=tips><table class=det>",
1474 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001475 for (j = 0; j < mod->stats_count; ++j) {
1476 chunk_appendf(out,
1477 "<tr><th>%s</th><td>%s</td></tr>",
1478 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1479 ++i;
1480 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001481 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001482 } else {
1483 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001484 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001485
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001486 chunk_appendf(out, "</td>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001487 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001488 }
1489
1490 chunk_appendf(out, "</tr>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01001491 }
1492 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1493 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001494 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001495 /* Column sub-heading for Enable or Disable server */
1496 chunk_appendf(out, "<td></td>");
1497 }
1498 chunk_appendf(out,
1499 "<td class=ac>"
1500 /* name */
1501 "%s<a name=\"%s/Backend\"></a>"
1502 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1503 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001504 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001505 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1506
Willy Tarreau708c4162019-10-09 10:19:16 +02001507 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001508 /* balancing */
1509 chunk_appendf(out, "<div class=tips>balancing: %s",
1510 field_str(stats, ST_F_ALGO));
1511
1512 /* cookie */
1513 if (stats[ST_F_COOKIE].type) {
1514 chunk_appendf(out, ", cookie: '");
1515 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1516 chunk_htmlencode(out, &src);
1517 chunk_appendf(out, "'");
1518 }
1519 chunk_appendf(out, "</div>");
1520 }
1521
1522 chunk_appendf(out,
1523 "%s</td>"
1524 /* queue : current, max */
1525 "<td>%s</td><td>%s</td><td></td>"
1526 /* sessions rate : current, max, limit */
1527 "<td>%s</td><td>%s</td><td></td>"
1528 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001529 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001530 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1531 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1532
1533 chunk_appendf(out,
1534 /* sessions: current, max, limit, total */
1535 "<td>%s</td><td>%s</td><td>%s</td>"
1536 "<td><u>%s<div class=tips><table class=det>"
1537 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1538 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001539 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 +01001540 U2H(stats[ST_F_STOT].u.u64),
1541 U2H(stats[ST_F_STOT].u.u64));
1542
1543 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1544 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1545 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001546 "<tr><th>New connections:</th><td>%s</td></tr>"
1547 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001548 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1549 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1550 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1551 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1552 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1553 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1554 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1555 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001556 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1557 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001558 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Faulet0159ee42019-12-16 14:40:39 +01001559 "<tr><th>Internal errors:</th><td>%s</td></tr>"
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001560 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001561 U2H(stats[ST_F_CONNECT].u.u64),
1562 U2H(stats[ST_F_REUSE].u.u64),
1563 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1564 (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 +01001565 U2H(stats[ST_F_REQ_TOT].u.u64),
1566 U2H(stats[ST_F_HRSP_1XX].u.u64),
1567 U2H(stats[ST_F_HRSP_2XX].u.u64),
1568 U2H(stats[ST_F_COMP_RSP].u.u64),
1569 stats[ST_F_HRSP_2XX].u.u64 ?
1570 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1571 U2H(stats[ST_F_HRSP_3XX].u.u64),
1572 U2H(stats[ST_F_HRSP_4XX].u.u64),
1573 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001574 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001575 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1576 U2H(stats[ST_F_CACHE_HITS].u.u64),
1577 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1578 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Christopher Faulet0159ee42019-12-16 14:40:39 +01001579 U2H(stats[ST_F_WREW].u.u64),
1580 U2H(stats[ST_F_EINT].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001581 }
1582
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001583 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1584 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1585 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1586 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1587 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001588 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Faulet0d1c2a62019-11-08 14:59:51 +01001589 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1590 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1591 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1592 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001593
1594 chunk_appendf(out,
1595 "</table></div></u></td>"
1596 /* sessions: lbtot, last */
1597 "<td>%s</td><td>%s</td>"
1598 /* bytes: in */
1599 "<td>%s</td>"
1600 "",
1601 U2H(stats[ST_F_LBTOT].u.u64),
1602 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1603 U2H(stats[ST_F_BIN].u.u64));
1604
1605 chunk_appendf(out,
1606 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1607 "<td>%s%s<div class=tips><table class=det>"
1608 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1609 "<tr><th>Compression in:</th><td>%s</td></tr>"
1610 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1611 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1612 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1613 "</table></div>%s</td>",
1614 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1615 U2H(stats[ST_F_BOUT].u.u64),
1616 U2H(stats[ST_F_BOUT].u.u64),
1617 U2H(stats[ST_F_COMP_IN].u.u64),
1618 U2H(stats[ST_F_COMP_OUT].u.u64),
1619 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1620 U2H(stats[ST_F_COMP_BYP].u.u64),
1621 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1622 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,
1623 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1624
1625 chunk_appendf(out,
1626 /* denied: req, resp */
1627 "<td>%s</td><td>%s</td>"
1628 /* errors : request, connect */
1629 "<td></td><td>%s</td>"
1630 /* errors : response */
1631 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1632 /* warnings: retries, redispatches */
1633 "<td>%lld</td><td>%lld</td>"
1634 /* backend status: reflect backend status (up/down): we display UP
1635 * if the backend has known working servers or if it has no server at
1636 * all (eg: for stats). Then we display the total weight, number of
1637 * active and backups. */
Willy Tarreaubd715102020-10-23 22:44:30 +02001638 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d/%d</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001639 "<td class=ac>%d</td><td class=ac>%d</td>"
1640 "",
1641 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1642 U2H(stats[ST_F_ECON].u.u64),
1643 U2H(stats[ST_F_ERESP].u.u64),
1644 (long long)stats[ST_F_CLI_ABRT].u.u64,
1645 (long long)stats[ST_F_SRV_ABRT].u.u64,
1646 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1647 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1648 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 +02001649 stats[ST_F_WEIGHT].u.u32, stats[ST_F_UWEIGHT].u.u32,
William Lallemand74c24fb2016-11-21 17:18:36 +01001650 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1651
1652 chunk_appendf(out,
1653 /* rest of backend: nothing, down transitions, total downtime, throttle */
1654 "<td class=ac>&nbsp;</td><td>%d</td>"
1655 "<td>%s</td>"
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001656 "<td></td>",
William Lallemand74c24fb2016-11-21 17:18:36 +01001657 stats[ST_F_CHKDOWN].u.u32,
1658 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001659
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001660 if (flags & STAT_SHMODULES) {
1661 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001662 chunk_appendf(out, "<td>");
1663
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001664 if (stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE) {
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001665 chunk_appendf(out,
1666 "<u>%s<div class=tips><table class=det>",
1667 mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001668 for (j = 0; j < mod->stats_count; ++j) {
1669 chunk_appendf(out,
1670 "<tr><th>%s</th><td>%s</td></tr>",
1671 mod->stats[j].desc, field_to_html_str(&stats[ST_F_TOTAL_FIELDS + i]));
1672 ++i;
1673 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001674 chunk_appendf(out, "</table></div></u>");
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001675 } else {
1676 i += mod->stats_count;
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001677 }
Amaury Denoyellee6ee8202020-11-03 15:04:46 +01001678
1679 chunk_appendf(out, "</td>");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001680 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001681 }
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02001682
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001683 chunk_appendf(out, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01001684 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02001685
William Lallemand74c24fb2016-11-21 17:18:36 +01001686 return 1;
1687}
1688
Amaury Denoyelle97323c92020-10-02 18:32:01 +02001689int stats_dump_one_line(const struct field *stats, size_t stats_count,
1690 struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001691{
Willy Tarreau91cefca2022-05-03 17:08:29 +02001692 struct show_stat_ctx *ctx = appctx->svcctx;
Simon Horman05ee2132017-01-04 09:37:25 +01001693 int ret;
1694
Willy Tarreau91cefca2022-05-03 17:08:29 +02001695 if (ctx->flags & STAT_FMT_HTML)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001696 ret = stats_dump_fields_html(&trash_chunk, stats, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001697 else if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001698 ret = stats_dump_fields_typed(&trash_chunk, stats, stats_count, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001699 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01001700 ret = stats_dump_fields_json(&trash_chunk, stats, stats_count, ctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001701 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01001702 ret = stats_dump_fields_csv(&trash_chunk, stats, stats_count, ctx);
Simon Horman05ee2132017-01-04 09:37:25 +01001703
Simon Horman05ee2132017-01-04 09:37:25 +01001704 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001705}
1706
William Dauchy0ef54392021-01-17 18:27:45 +01001707/* Fill <stats> with the frontend statistics. <stats> is preallocated array of
Christopher Fauletd808f172021-01-22 17:33:22 +01001708 * length <len>. If <selected_field> is != NULL, only fill this one. The length
1709 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
1710 * this value, or if the selected field is not implemented for frontends, the
1711 * function returns 0, otherwise, it returns 1.
William Lallemand74c24fb2016-11-21 17:18:36 +01001712 */
William Dauchy0ef54392021-01-17 18:27:45 +01001713int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
1714 enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001715{
William Dauchy0ef54392021-01-17 18:27:45 +01001716 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
William Dauchy0ef54392021-01-17 18:27:45 +01001717
William Lallemand74c24fb2016-11-21 17:18:36 +01001718 if (len < ST_F_TOTAL_FIELDS)
1719 return 0;
1720
William Dauchy0ef54392021-01-17 18:27:45 +01001721 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
Christopher Faulet8596bfb2021-01-25 15:16:41 +01001722 struct field metric = { 0 };
1723
William Dauchy0ef54392021-01-17 18:27:45 +01001724 switch (current_field) {
1725 case ST_F_PXNAME:
1726 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1727 break;
1728 case ST_F_SVNAME:
1729 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1730 break;
1731 case ST_F_MODE:
1732 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1733 break;
1734 case ST_F_SCUR:
1735 metric = mkf_u32(0, px->feconn);
1736 break;
1737 case ST_F_SMAX:
1738 metric = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1739 break;
1740 case ST_F_SLIM:
1741 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1742 break;
1743 case ST_F_STOT:
1744 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1745 break;
1746 case ST_F_BIN:
1747 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1748 break;
1749 case ST_F_BOUT:
1750 metric = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1751 break;
1752 case ST_F_DREQ:
1753 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1754 break;
1755 case ST_F_DRESP:
1756 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1757 break;
1758 case ST_F_EREQ:
1759 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1760 break;
1761 case ST_F_DCON:
1762 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1763 break;
1764 case ST_F_DSES:
1765 metric = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1766 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001767 case ST_F_STATUS: {
1768 const char *state;
1769
1770 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
1771 state = "STOP";
1772 else if (px->flags & PR_FL_PAUSED)
1773 state = "PAUSED";
1774 else
1775 state = "OPEN";
1776 metric = mkf_str(FO_STATUS, state);
William Dauchy0ef54392021-01-17 18:27:45 +01001777 break;
Aurelien DARRAGONcddec0a2022-09-09 15:58:57 +02001778 }
William Dauchy0ef54392021-01-17 18:27:45 +01001779 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02001780 metric = mkf_u32(FO_KEY, 1);
William Dauchy0ef54392021-01-17 18:27:45 +01001781 break;
1782 case ST_F_IID:
1783 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1784 break;
1785 case ST_F_SID:
1786 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
1787 break;
1788 case ST_F_TYPE:
1789 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1790 break;
1791 case ST_F_RATE:
1792 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1793 break;
1794 case ST_F_RATE_LIM:
1795 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1796 break;
1797 case ST_F_RATE_MAX:
1798 metric = mkf_u32(FN_MAX, px->fe_counters.sps_max);
1799 break;
1800 case ST_F_WREW:
1801 metric = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
1802 break;
1803 case ST_F_EINT:
1804 metric = mkf_u64(FN_COUNTER, px->fe_counters.internal_errors);
1805 break;
1806 case ST_F_HRSP_1XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001807 if (px->mode == PR_MODE_HTTP)
1808 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
William Dauchy0ef54392021-01-17 18:27:45 +01001809 break;
1810 case ST_F_HRSP_2XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001811 if (px->mode == PR_MODE_HTTP)
1812 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
William Dauchy0ef54392021-01-17 18:27:45 +01001813 break;
1814 case ST_F_HRSP_3XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001815 if (px->mode == PR_MODE_HTTP)
1816 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
William Dauchy0ef54392021-01-17 18:27:45 +01001817 break;
1818 case ST_F_HRSP_4XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001819 if (px->mode == PR_MODE_HTTP)
1820 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
William Dauchy0ef54392021-01-17 18:27:45 +01001821 break;
1822 case ST_F_HRSP_5XX:
William Dauchy2107a0f2021-01-22 21:09:48 +01001823 if (px->mode == PR_MODE_HTTP)
1824 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
William Dauchy0ef54392021-01-17 18:27:45 +01001825 break;
1826 case ST_F_HRSP_OTHER:
William Dauchy2107a0f2021-01-22 21:09:48 +01001827 if (px->mode == PR_MODE_HTTP)
1828 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
William Dauchy0ef54392021-01-17 18:27:45 +01001829 break;
1830 case ST_F_INTERCEPTED:
William Dauchy2107a0f2021-01-22 21:09:48 +01001831 if (px->mode == PR_MODE_HTTP)
1832 metric = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001833 break;
1834 case ST_F_CACHE_LOOKUPS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001835 if (px->mode == PR_MODE_HTTP)
1836 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
William Dauchy0ef54392021-01-17 18:27:45 +01001837 break;
1838 case ST_F_CACHE_HITS:
William Dauchy2107a0f2021-01-22 21:09:48 +01001839 if (px->mode == PR_MODE_HTTP)
1840 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Dauchy0ef54392021-01-17 18:27:45 +01001841 break;
1842 case ST_F_REQ_RATE:
1843 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1844 break;
1845 case ST_F_REQ_RATE_MAX:
1846 metric = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1847 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001848 case ST_F_REQ_TOT: {
1849 int i;
1850 uint64_t total_req;
1851 size_t nb_reqs =
1852 sizeof(px->fe_counters.p.http.cum_req) / sizeof(*px->fe_counters.p.http.cum_req);
1853
1854 total_req = 0;
1855 for (i = 0; i < nb_reqs; i++)
1856 total_req += px->fe_counters.p.http.cum_req[i];
1857 metric = mkf_u64(FN_COUNTER, total_req);
William Dauchy0ef54392021-01-17 18:27:45 +01001858 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001859 }
William Dauchy0ef54392021-01-17 18:27:45 +01001860 case ST_F_COMP_IN:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001861 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_in[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001862 break;
1863 case ST_F_COMP_OUT:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001864 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_out[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001865 break;
1866 case ST_F_COMP_BYP:
Olivier Houcharddea25f52023-04-06 00:33:01 +02001867 metric = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp[COMP_DIR_RES]);
William Dauchy0ef54392021-01-17 18:27:45 +01001868 break;
1869 case ST_F_COMP_RSP:
1870 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1871 break;
1872 case ST_F_CONN_RATE:
1873 metric = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1874 break;
1875 case ST_F_CONN_RATE_MAX:
1876 metric = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1877 break;
1878 case ST_F_CONN_TOT:
1879 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1880 break;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001881 case ST_F_SESS_OTHER: {
1882 int i;
1883 uint64_t total_sess;
1884 size_t nb_sess =
1885 sizeof(px->fe_counters.cum_sess_ver) / sizeof(*px->fe_counters.cum_sess_ver);
1886
1887 total_sess = px->fe_counters.cum_sess;
1888 for (i = 0; i < nb_sess; i++)
1889 total_sess -= px->fe_counters.cum_sess_ver[i];
Frédéric Lécailled97d1d72023-02-06 09:23:56 +01001890 total_sess = (int64_t)total_sess < 0 ? 0 : total_sess;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01001891 metric = mkf_u64(FN_COUNTER, total_sess);
1892 break;
1893 }
1894 case ST_F_H1SESS:
1895 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[0]);
1896 break;
1897 case ST_F_H2SESS:
1898 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[1]);
1899 break;
1900 case ST_F_H3SESS:
1901 metric = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess_ver[2]);
1902 break;
1903 case ST_F_REQ_OTHER:
1904 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[0]);
1905 break;
1906 case ST_F_H1REQ:
1907 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[1]);
1908 break;
1909 case ST_F_H2REQ:
1910 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[2]);
1911 break;
1912 case ST_F_H3REQ:
1913 metric = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req[3]);
1914 break;
William Dauchy0ef54392021-01-17 18:27:45 +01001915 default:
Christopher Fauletd808f172021-01-22 17:33:22 +01001916 /* not used for frontends. If a specific metric
1917 * is requested, return an error. Otherwise continue.
1918 */
1919 if (selected_field != NULL)
1920 return 0;
1921 continue;
William Dauchy0ef54392021-01-17 18:27:45 +01001922 }
1923 stats[current_field] = metric;
1924 if (selected_field != NULL)
1925 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001926 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001927 return 1;
1928}
1929
Christopher Fauleta8b76842022-12-16 15:08:36 +01001930/* Dumps a frontend's line to the local trash buffer for the current proxy <px>
1931 * and uses the state from stream connector <sc>. The caller is responsible for
1932 * clearing the local trash buffer if needed. Returns non-zero if it emits
1933 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001934 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02001935static int stats_dump_fe_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001936{
Willy Tarreaucaff6312022-05-27 10:17:46 +02001937 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02001938 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001939 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
1940 struct stats_module *mod;
1941 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01001942
1943 if (!(px->cap & PR_CAP_FE))
1944 return 0;
1945
Willy Tarreau91cefca2022-05-03 17:08:29 +02001946 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_FE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001947 return 0;
1948
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001949 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
1950
William Dauchy0ef54392021-01-17 18:27:45 +01001951 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01001952 return 0;
1953
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02001954 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
1955 void *counters;
1956
1957 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_FE)) {
1958 stats_count += mod->stats_count;
1959 continue;
1960 }
1961
1962 counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, mod);
1963 mod->fill_stats(counters, stats + stats_count);
1964 stats_count += mod->stats_count;
1965 }
1966
1967 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001968}
1969
William Dauchy655e14e2021-02-14 23:22:54 +01001970/* Fill <stats> with the listener statistics. <stats> is preallocated array of
1971 * length <len>. The length of the array must be at least ST_F_TOTAL_FIELDS. If
1972 * this length is less then this value, the function returns 0, otherwise, it
1973 * returns 1. If selected_field is != NULL, only fill this one. <flags> can
1974 * take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001975 */
1976int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
William Dauchy655e14e2021-02-14 23:22:54 +01001977 struct field *stats, int len, enum stat_field *selected_field)
William Lallemand74c24fb2016-11-21 17:18:36 +01001978{
William Dauchy655e14e2021-02-14 23:22:54 +01001979 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
Willy Tarreau83061a82018-07-13 11:56:34 +02001980 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001981
1982 if (len < ST_F_TOTAL_FIELDS)
1983 return 0;
1984
1985 if (!l->counters)
1986 return 0;
1987
1988 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01001989
William Dauchy655e14e2021-02-14 23:22:54 +01001990 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
1991 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01001992
William Dauchy655e14e2021-02-14 23:22:54 +01001993 switch (current_field) {
1994 case ST_F_PXNAME:
1995 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1996 break;
1997 case ST_F_SVNAME:
1998 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1999 break;
2000 case ST_F_MODE:
2001 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2002 break;
2003 case ST_F_SCUR:
2004 metric = mkf_u32(0, l->nbconn);
2005 break;
2006 case ST_F_SMAX:
2007 metric = mkf_u32(FN_MAX, l->counters->conn_max);
2008 break;
2009 case ST_F_SLIM:
Willy Tarreau758c69d2023-01-12 18:59:37 +01002010 metric = mkf_u32(FO_CONFIG|FN_LIMIT, l->bind_conf->maxconn);
William Dauchy655e14e2021-02-14 23:22:54 +01002011 break;
2012 case ST_F_STOT:
2013 metric = mkf_u64(FN_COUNTER, l->counters->cum_conn);
2014 break;
2015 case ST_F_BIN:
2016 metric = mkf_u64(FN_COUNTER, l->counters->bytes_in);
2017 break;
2018 case ST_F_BOUT:
2019 metric = mkf_u64(FN_COUNTER, l->counters->bytes_out);
2020 break;
2021 case ST_F_DREQ:
2022 metric = mkf_u64(FN_COUNTER, l->counters->denied_req);
2023 break;
2024 case ST_F_DRESP:
2025 metric = mkf_u64(FN_COUNTER, l->counters->denied_resp);
2026 break;
2027 case ST_F_EREQ:
2028 metric = mkf_u64(FN_COUNTER, l->counters->failed_req);
2029 break;
2030 case ST_F_DCON:
2031 metric = mkf_u64(FN_COUNTER, l->counters->denied_conn);
2032 break;
2033 case ST_F_DSES:
2034 metric = mkf_u64(FN_COUNTER, l->counters->denied_sess);
2035 break;
2036 case ST_F_STATUS:
William Dauchy3679d0c2021-02-14 23:22:55 +01002037 metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
William Dauchy655e14e2021-02-14 23:22:54 +01002038 break;
2039 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002040 metric = mkf_u32(FO_KEY, 1);
William Dauchy655e14e2021-02-14 23:22:54 +01002041 break;
2042 case ST_F_IID:
2043 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2044 break;
2045 case ST_F_SID:
2046 metric = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
2047 break;
2048 case ST_F_TYPE:
2049 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
2050 break;
2051 case ST_F_WREW:
2052 metric = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
2053 break;
2054 case ST_F_EINT:
2055 metric = mkf_u64(FN_COUNTER, l->counters->internal_errors);
2056 break;
2057 case ST_F_ADDR:
2058 if (flags & STAT_SHLGNDS) {
2059 char str[INET6_ADDRSTRLEN];
2060 int port;
William Lallemand74c24fb2016-11-21 17:18:36 +01002061
William Dauchy655e14e2021-02-14 23:22:54 +01002062 port = get_host_port(&l->rx.addr);
2063 switch (addr_to_str(&l->rx.addr, str, sizeof(str))) {
2064 case AF_INET:
2065 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2066 chunk_appendf(out, "%s:%d", str, port);
2067 break;
2068 case AF_INET6:
2069 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2070 chunk_appendf(out, "[%s]:%d", str, port);
2071 break;
2072 case AF_UNIX:
2073 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2074 break;
2075 case -1:
2076 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2077 chunk_strcat(out, strerror(errno));
2078 break;
2079 default: /* address family not supported */
2080 break;
2081 }
2082 }
2083 break;
Willy Tarreau4cfb0012023-05-11 14:49:43 +02002084 case ST_F_PROTO:
2085 metric = mkf_str(FO_STATUS, l->rx.proto->name);
2086 break;
William Dauchy655e14e2021-02-14 23:22:54 +01002087 default:
2088 /* not used for listen. If a specific metric
2089 * is requested, return an error. Otherwise continue.
2090 */
2091 if (selected_field != NULL)
2092 return 0;
2093 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002094 }
William Dauchy655e14e2021-02-14 23:22:54 +01002095 stats[current_field] = metric;
2096 if (selected_field != NULL)
2097 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002098 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002099 return 1;
2100}
2101
Christopher Fauleta8b76842022-12-16 15:08:36 +01002102/* Dumps a line for listener <l> and proxy <px> to the local trash buffer and
2103 * uses the state from stream connector <sc>. The caller is responsible for
2104 * clearing the local trash buffer if needed. Returns non-zero if it emits
2105 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002106 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002107static int stats_dump_li_stats(struct stconn *sc, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01002108{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002109 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002110 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002111 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2112 struct stats_module *mod;
2113 size_t stats_count = ST_F_TOTAL_FIELDS;
2114
2115 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002116
Willy Tarreau91cefca2022-05-03 17:08:29 +02002117 if (!stats_fill_li_stats(px, l, ctx->flags, stats,
William Dauchy655e14e2021-02-14 23:22:54 +01002118 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002119 return 0;
2120
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002121 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2122 void *counters;
2123
2124 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_LI)) {
2125 stats_count += mod->stats_count;
2126 continue;
2127 }
2128
2129 counters = EXTRA_COUNTERS_GET(l->extra_counters, mod);
2130 mod->fill_stats(counters, stats + stats_count);
2131 stats_count += mod->stats_count;
2132 }
2133
2134 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002135}
2136
2137enum srv_stats_state {
2138 SRV_STATS_STATE_DOWN = 0,
2139 SRV_STATS_STATE_DOWN_AGENT,
2140 SRV_STATS_STATE_GOING_UP,
2141 SRV_STATS_STATE_UP_GOING_DOWN,
2142 SRV_STATS_STATE_UP,
2143 SRV_STATS_STATE_NOLB_GOING_DOWN,
2144 SRV_STATS_STATE_NOLB,
2145 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2146 SRV_STATS_STATE_DRAIN,
2147 SRV_STATS_STATE_DRAIN_AGENT,
2148 SRV_STATS_STATE_NO_CHECK,
2149
2150 SRV_STATS_STATE_COUNT, /* Must be last */
2151};
2152
2153static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2154 [SRV_STATS_STATE_DOWN] = "DOWN",
2155 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2156 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
2157 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
2158 [SRV_STATS_STATE_UP] = "UP",
2159 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
2160 [SRV_STATS_STATE_NOLB] = "NOLB",
2161 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
2162 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2163 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
2164 [SRV_STATS_STATE_NO_CHECK] = "no check"
2165};
2166
William Dauchyd3a9a492021-01-25 17:29:03 +01002167/* Compute server state helper
William Lallemand74c24fb2016-11-21 17:18:36 +01002168 */
William Dauchyd3a9a492021-01-25 17:29:03 +01002169static void stats_fill_sv_stats_computestate(struct server *sv, struct server *ref,
2170 enum srv_stats_state *state)
William Lallemand74c24fb2016-11-21 17:18:36 +01002171{
Emeric Brun52a91d32017-08-31 14:41:55 +02002172 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002173 if ((ref->check.state & CHK_ST_ENABLED) &&
2174 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002175 *state = SRV_STATS_STATE_UP_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002176 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002177 *state = SRV_STATS_STATE_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002178 }
2179
Emeric Brun52a91d32017-08-31 14:41:55 +02002180 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002181 if (ref->agent.state & CHK_ST_ENABLED)
William Dauchyd3a9a492021-01-25 17:29:03 +01002182 *state = SRV_STATS_STATE_DRAIN_AGENT;
2183 else if (*state == SRV_STATS_STATE_UP_GOING_DOWN)
2184 *state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002185 else
William Dauchyd3a9a492021-01-25 17:29:03 +01002186 *state = SRV_STATS_STATE_DRAIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002187 }
2188
William Dauchyd3a9a492021-01-25 17:29:03 +01002189 if (*state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
2190 *state = SRV_STATS_STATE_NO_CHECK;
William Lallemand74c24fb2016-11-21 17:18:36 +01002191 }
2192 }
Emeric Brun52a91d32017-08-31 14:41:55 +02002193 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002194 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
2195 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002196 *state = SRV_STATS_STATE_NOLB;
William Lallemand74c24fb2016-11-21 17:18:36 +01002197 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002198 *state = SRV_STATS_STATE_NOLB_GOING_DOWN;
William Lallemand74c24fb2016-11-21 17:18:36 +01002199 }
2200 }
2201 else { /* stopped */
2202 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002203 *state = SRV_STATS_STATE_DOWN_AGENT;
William Lallemand74c24fb2016-11-21 17:18:36 +01002204 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002205 *state = SRV_STATS_STATE_DOWN; /* DOWN */
William Lallemand74c24fb2016-11-21 17:18:36 +01002206 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
William Dauchyd3a9a492021-01-25 17:29:03 +01002207 *state = SRV_STATS_STATE_GOING_UP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002208 } else {
William Dauchyd3a9a492021-01-25 17:29:03 +01002209 *state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
William Lallemand74c24fb2016-11-21 17:18:36 +01002210 }
2211 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002212}
William Lallemand74c24fb2016-11-21 17:18:36 +01002213
William Dauchyd3a9a492021-01-25 17:29:03 +01002214/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2215 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2216 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2217 * this value, or if the selected field is not implemented for servers, the
2218 * function returns 0, otherwise, it returns 1. <flags> can take the value
2219 * STAT_SHLGNDS.
2220 */
2221int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
2222 struct field *stats, int len,
2223 enum stat_field *selected_field)
2224{
2225 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2226 struct server *via = sv->track ? sv->track : sv;
2227 struct server *ref = via;
2228 enum srv_stats_state state = 0;
2229 char str[INET6_ADDRSTRLEN];
2230 struct buffer *out = get_trash_chunk();
2231 char *fld_status;
2232 long long srv_samples_counter;
2233 unsigned int srv_samples_window = TIME_STATS_SAMPLES;
William Lallemand74c24fb2016-11-21 17:18:36 +01002234
William Dauchyd3a9a492021-01-25 17:29:03 +01002235 if (len < ST_F_TOTAL_FIELDS)
2236 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002237
William Dauchyd3a9a492021-01-25 17:29:03 +01002238 chunk_reset(out);
William Lallemand74c24fb2016-11-21 17:18:36 +01002239
William Dauchyd3a9a492021-01-25 17:29:03 +01002240 /* compute state for later use */
2241 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2242 *selected_field == ST_F_CHECK_RISE || *selected_field == ST_F_CHECK_FALL ||
2243 *selected_field == ST_F_CHECK_HEALTH || *selected_field == ST_F_HANAFAIL) {
2244 /* we have "via" which is the tracked server as described in the configuration,
2245 * and "ref" which is the checked server and the end of the chain.
2246 */
2247 while (ref->track)
2248 ref = ref->track;
2249 stats_fill_sv_stats_computestate(sv, ref, &state);
William Lallemand74c24fb2016-11-21 17:18:36 +01002250 }
2251
William Dauchyd3a9a492021-01-25 17:29:03 +01002252 /* compue time values for later use */
2253 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2254 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2255 *selected_field == ST_F_TTIME) {
2256 srv_samples_counter = (px->mode == PR_MODE_HTTP) ? sv->counters.p.http.cum_req : sv->counters.cum_lbconn;
2257 if (srv_samples_counter < TIME_STATS_SAMPLES && srv_samples_counter > 0)
2258 srv_samples_window = srv_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002259 }
2260
William Dauchyd3a9a492021-01-25 17:29:03 +01002261 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2262 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002263
William Dauchyd3a9a492021-01-25 17:29:03 +01002264 switch (current_field) {
2265 case ST_F_PXNAME:
2266 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2267 break;
2268 case ST_F_SVNAME:
2269 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
2270 break;
2271 case ST_F_MODE:
2272 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Christopher Faulet3888b8c2021-01-27 13:32:25 +01002273 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002274 case ST_F_QCUR:
Willy Tarreaua0570452021-06-18 09:30:30 +02002275 metric = mkf_u32(0, sv->queue.length);
William Dauchyd3a9a492021-01-25 17:29:03 +01002276 break;
2277 case ST_F_QMAX:
2278 metric = mkf_u32(FN_MAX, sv->counters.nbpend_max);
2279 break;
2280 case ST_F_SCUR:
2281 metric = mkf_u32(0, sv->cur_sess);
2282 break;
2283 case ST_F_SMAX:
2284 metric = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
2285 break;
2286 case ST_F_SLIM:
2287 if (sv->maxconn)
2288 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
2289 break;
2290 case ST_F_SRV_ICUR:
2291 metric = mkf_u32(0, sv->curr_idle_conns);
2292 break;
2293 case ST_F_SRV_ILIM:
2294 if (sv->max_idle_conns != -1)
2295 metric = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
2296 break;
2297 case ST_F_STOT:
2298 metric = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
2299 break;
2300 case ST_F_BIN:
2301 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
2302 break;
2303 case ST_F_BOUT:
2304 metric = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
2305 break;
2306 case ST_F_DRESP:
2307 metric = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
2308 break;
2309 case ST_F_ECON:
2310 metric = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
2311 break;
2312 case ST_F_ERESP:
2313 metric = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
2314 break;
2315 case ST_F_WRETR:
2316 metric = mkf_u64(FN_COUNTER, sv->counters.retries);
2317 break;
2318 case ST_F_WREDIS:
2319 metric = mkf_u64(FN_COUNTER, sv->counters.redispatches);
2320 break;
2321 case ST_F_WREW:
2322 metric = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
2323 break;
2324 case ST_F_EINT:
2325 metric = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
2326 break;
2327 case ST_F_CONNECT:
2328 metric = mkf_u64(FN_COUNTER, sv->counters.connect);
2329 break;
2330 case ST_F_REUSE:
2331 metric = mkf_u64(FN_COUNTER, sv->counters.reuse);
2332 break;
2333 case ST_F_IDLE_CONN_CUR:
2334 metric = mkf_u32(0, sv->curr_idle_nb);
2335 break;
2336 case ST_F_SAFE_CONN_CUR:
2337 metric = mkf_u32(0, sv->curr_safe_nb);
2338 break;
2339 case ST_F_USED_CONN_CUR:
2340 metric = mkf_u32(0, sv->curr_used_conns);
2341 break;
2342 case ST_F_NEED_CONN_EST:
2343 metric = mkf_u32(0, sv->est_need_conns);
2344 break;
2345 case ST_F_STATUS:
2346 fld_status = chunk_newstr(out);
2347 if (sv->cur_admin & SRV_ADMF_RMAINT)
2348 chunk_appendf(out, "MAINT (resolution)");
2349 else if (sv->cur_admin & SRV_ADMF_IMAINT)
2350 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
2351 else if (sv->cur_admin & SRV_ADMF_MAINT)
2352 chunk_appendf(out, "MAINT");
2353 else
2354 chunk_appendf(out,
2355 srv_hlt_st[state],
2356 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2357 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01002358
William Dauchyd3a9a492021-01-25 17:29:03 +01002359 metric = mkf_str(FO_STATUS, fld_status);
2360 break;
2361 case ST_F_LASTCHG:
Willy Tarreau69530f52023-04-28 09:16:15 +02002362 metric = mkf_u32(FN_AGE, ns_to_sec(now_ns) - sv->last_change);
William Dauchyd3a9a492021-01-25 17:29:03 +01002363 break;
2364 case ST_F_WEIGHT:
2365 metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2366 break;
2367 case ST_F_UWEIGHT:
2368 metric = mkf_u32(FN_AVG, sv->uweight);
2369 break;
2370 case ST_F_ACT:
2371 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
2372 break;
2373 case ST_F_BCK:
2374 metric = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
2375 break;
2376 case ST_F_CHKFAIL:
2377 if (sv->check.state & CHK_ST_ENABLED)
2378 metric = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
2379 break;
2380 case ST_F_CHKDOWN:
2381 if (sv->check.state & CHK_ST_ENABLED)
2382 metric = mkf_u64(FN_COUNTER, sv->counters.down_trans);
2383 break;
2384 case ST_F_DOWNTIME:
2385 if (sv->check.state & CHK_ST_ENABLED)
2386 metric = mkf_u32(FN_COUNTER, srv_downtime(sv));
2387 break;
2388 case ST_F_QLIMIT:
2389 if (sv->maxqueue)
2390 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
2391 break;
2392 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002393 metric = mkf_u32(FO_KEY, 1);
William Dauchyd3a9a492021-01-25 17:29:03 +01002394 break;
2395 case ST_F_IID:
2396 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2397 break;
2398 case ST_F_SID:
2399 metric = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
2400 break;
Aurelien DARRAGON745ce8e2022-11-17 16:34:07 +01002401 case ST_F_SRID:
2402 metric = mkf_u32(FN_COUNTER, sv->rid);
2403 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002404 case ST_F_THROTTLE:
2405 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
2406 metric = mkf_u32(FN_AVG, server_throttle_rate(sv));
2407 break;
2408 case ST_F_LBTOT:
2409 metric = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
2410 break;
2411 case ST_F_TRACKED:
2412 if (sv->track) {
2413 char *fld_track = chunk_newstr(out);
2414 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
2415 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
2416 }
2417 break;
2418 case ST_F_TYPE:
2419 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
2420 break;
2421 case ST_F_RATE:
2422 metric = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
2423 break;
2424 case ST_F_RATE_MAX:
2425 metric = mkf_u32(FN_MAX, sv->counters.sps_max);
2426 break;
2427 case ST_F_CHECK_STATUS:
2428 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2429 const char *fld_chksts;
Marcin Deraneka8dbdf32020-05-15 20:02:40 +02002430
William Dauchyd3a9a492021-01-25 17:29:03 +01002431 fld_chksts = chunk_newstr(out);
2432 chunk_strcat(out, "* "); // for check in progress
2433 chunk_strcat(out, get_check_status_info(sv->check.status));
2434 if (!(sv->check.state & CHK_ST_INPROGRESS))
2435 fld_chksts += 2; // skip "* "
Willy Tarreau973a9372021-05-12 17:29:14 +02002436 metric = mkf_str(FN_OUTPUT, fld_chksts);
William Dauchyd3a9a492021-01-25 17:29:03 +01002437 }
2438 break;
2439 case ST_F_CHECK_CODE:
2440 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2441 sv->check.status >= HCHK_STATUS_L57DATA)
2442 metric = mkf_u32(FN_OUTPUT, sv->check.code);
2443 break;
2444 case ST_F_CHECK_DURATION:
2445 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2446 sv->check.status >= HCHK_STATUS_CHECKED)
Aurelien DARRAGONb118f2f2022-12-07 14:52:10 +01002447 metric = mkf_u64(FN_DURATION, MAX(sv->check.duration, 0));
William Dauchyd3a9a492021-01-25 17:29:03 +01002448 break;
2449 case ST_F_CHECK_DESC:
2450 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2451 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
2452 break;
2453 case ST_F_LAST_CHK:
2454 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2455 metric = mkf_str(FN_OUTPUT, sv->check.desc);
2456 break;
2457 case ST_F_CHECK_RISE:
2458 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2459 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
2460 break;
2461 case ST_F_CHECK_FALL:
2462 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2463 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
2464 break;
2465 case ST_F_CHECK_HEALTH:
2466 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2467 metric = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
2468 break;
Christopher Faulet59bab612021-07-22 08:04:38 +02002469 case ST_F_AGENT_STATUS:
2470 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
2471 const char *fld_chksts;
2472
2473 fld_chksts = chunk_newstr(out);
2474 chunk_strcat(out, "* "); // for check in progress
2475 chunk_strcat(out, get_check_status_info(sv->agent.status));
2476 if (!(sv->agent.state & CHK_ST_INPROGRESS))
2477 fld_chksts += 2; // skip "* "
2478 metric = mkf_str(FN_OUTPUT, fld_chksts);
2479 }
2480 break;
2481 case ST_F_AGENT_CODE:
2482 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED &&
2483 (sv->agent.status >= HCHK_STATUS_L57DATA))
2484 metric = mkf_u32(FN_OUTPUT, sv->agent.code);
2485 break;
2486 case ST_F_AGENT_DURATION:
2487 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2488 metric = mkf_u64(FN_DURATION, sv->agent.duration);
2489 break;
2490 case ST_F_AGENT_DESC:
2491 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2492 metric = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
2493 break;
2494 case ST_F_LAST_AGT:
2495 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2496 metric = mkf_str(FN_OUTPUT, sv->agent.desc);
2497 break;
2498 case ST_F_AGENT_RISE:
2499 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2500 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
2501 break;
2502 case ST_F_AGENT_FALL:
2503 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2504 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
2505 break;
2506 case ST_F_AGENT_HEALTH:
2507 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED)
2508 metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
2509 break;
William Dauchyd3a9a492021-01-25 17:29:03 +01002510 case ST_F_REQ_TOT:
2511 if (px->mode == PR_MODE_HTTP)
2512 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
2513 break;
2514 case ST_F_HRSP_1XX:
2515 if (px->mode == PR_MODE_HTTP)
2516 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
2517 break;
2518 case ST_F_HRSP_2XX:
2519 if (px->mode == PR_MODE_HTTP)
2520 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
2521 break;
2522 case ST_F_HRSP_3XX:
2523 if (px->mode == PR_MODE_HTTP)
2524 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
2525 break;
2526 case ST_F_HRSP_4XX:
2527 if (px->mode == PR_MODE_HTTP)
2528 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
2529 break;
2530 case ST_F_HRSP_5XX:
2531 if (px->mode == PR_MODE_HTTP)
2532 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
2533 break;
2534 case ST_F_HRSP_OTHER:
2535 if (px->mode == PR_MODE_HTTP)
2536 metric = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
2537 break;
2538 case ST_F_HANAFAIL:
2539 if (ref->observe)
2540 metric = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
2541 break;
2542 case ST_F_CLI_ABRT:
2543 metric = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
2544 break;
2545 case ST_F_SRV_ABRT:
2546 metric = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
2547 break;
2548 case ST_F_LASTSESS:
2549 metric = mkf_s32(FN_AGE, srv_lastsession(sv));
2550 break;
2551 case ST_F_QTIME:
2552 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, srv_samples_window));
2553 break;
2554 case ST_F_CTIME:
2555 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, srv_samples_window));
2556 break;
2557 case ST_F_RTIME:
2558 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, srv_samples_window));
2559 break;
2560 case ST_F_TTIME:
2561 metric = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, srv_samples_window));
2562 break;
2563 case ST_F_QT_MAX:
2564 metric = mkf_u32(FN_MAX, sv->counters.qtime_max);
2565 break;
2566 case ST_F_CT_MAX:
2567 metric = mkf_u32(FN_MAX, sv->counters.ctime_max);
2568 break;
2569 case ST_F_RT_MAX:
2570 metric = mkf_u32(FN_MAX, sv->counters.dtime_max);
2571 break;
2572 case ST_F_TT_MAX:
2573 metric = mkf_u32(FN_MAX, sv->counters.ttime_max);
2574 break;
2575 case ST_F_ADDR:
2576 if (flags & STAT_SHLGNDS) {
2577 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2578 case AF_INET:
2579 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2580 chunk_appendf(out, "%s:%d", str, sv->svc_port);
2581 break;
2582 case AF_INET6:
2583 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2584 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
2585 break;
2586 case AF_UNIX:
2587 metric = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
2588 break;
2589 case -1:
2590 metric = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
2591 chunk_strcat(out, strerror(errno));
2592 break;
2593 default: /* address family not supported */
2594 break;
2595 }
2596 }
2597 break;
2598 case ST_F_COOKIE:
2599 if (flags & STAT_SHLGNDS && sv->cookie)
2600 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
2601 break;
2602 default:
2603 /* not used for servers. If a specific metric
2604 * is requested, return an error. Otherwise continue.
2605 */
2606 if (selected_field != NULL)
2607 return 0;
2608 continue;
William Lallemand74c24fb2016-11-21 17:18:36 +01002609 }
William Dauchyd3a9a492021-01-25 17:29:03 +01002610 stats[current_field] = metric;
2611 if (selected_field != NULL)
2612 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002613 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002614 return 1;
2615}
2616
Christopher Fauleta8b76842022-12-16 15:08:36 +01002617/* Dumps a line for server <sv> and proxy <px> to the local trash vbuffer and
2618 * uses the state from stream connector <sc>, and server state <state>. The
2619 * caller is responsible for clearing the local trash buffer if needed. Returns
2620 * non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002621 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002622static int stats_dump_sv_stats(struct stconn *sc, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01002623{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002624 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002625 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002626 struct stats_module *mod;
2627 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2628 size_t stats_count = ST_F_TOTAL_FIELDS;
2629
2630 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
William Lallemand74c24fb2016-11-21 17:18:36 +01002631
Willy Tarreau91cefca2022-05-03 17:08:29 +02002632 if (!stats_fill_sv_stats(px, sv, ctx->flags, stats,
William Dauchyd3a9a492021-01-25 17:29:03 +01002633 ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002634 return 0;
2635
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002636 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2637 void *counters;
2638
2639 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2640 continue;
2641
2642 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_SRV)) {
2643 stats_count += mod->stats_count;
2644 continue;
2645 }
2646
2647 counters = EXTRA_COUNTERS_GET(sv->extra_counters, mod);
2648 mod->fill_stats(counters, stats + stats_count);
2649 stats_count += mod->stats_count;
2650 }
2651
2652 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002653}
2654
William Dauchyda3b4662021-01-25 17:29:01 +01002655/* Helper to compute srv values for a given backend
William Lallemand74c24fb2016-11-21 17:18:36 +01002656 */
William Dauchyda3b4662021-01-25 17:29:01 +01002657static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int *nbsrv, int *totuw)
William Lallemand74c24fb2016-11-21 17:18:36 +01002658{
William Dauchyda3b4662021-01-25 17:29:01 +01002659 int nbup_tmp, nbsrv_tmp, totuw_tmp;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002660 const struct server *srv;
William Lallemand74c24fb2016-11-21 17:18:36 +01002661
William Dauchyda3b4662021-01-25 17:29:01 +01002662 nbup_tmp = nbsrv_tmp = totuw_tmp = 0;
Willy Tarreaubd715102020-10-23 22:44:30 +02002663 for (srv = px->srv; srv; srv = srv->next) {
2664 if (srv->cur_state != SRV_ST_STOPPED) {
William Dauchyda3b4662021-01-25 17:29:01 +01002665 nbup_tmp++;
Willy Tarreaubd715102020-10-23 22:44:30 +02002666 if (srv_currently_usable(srv) &&
2667 (!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
William Dauchyda3b4662021-01-25 17:29:01 +01002668 totuw_tmp += srv->uweight;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002669 }
William Dauchyda3b4662021-01-25 17:29:01 +01002670 nbsrv_tmp++;
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002671 }
2672
Willy Tarreaubd715102020-10-23 22:44:30 +02002673 HA_RWLOCK_RDLOCK(LBPRM_LOCK, &px->lbprm.lock);
2674 if (!px->srv_act && px->lbprm.fbck)
William Dauchyda3b4662021-01-25 17:29:01 +01002675 totuw_tmp = px->lbprm.fbck->uweight;
Willy Tarreaubd715102020-10-23 22:44:30 +02002676 HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &px->lbprm.lock);
2677
William Dauchyda3b4662021-01-25 17:29:01 +01002678 /* use tmp variable then assign result to make gcc happy */
2679 *nbup = nbup_tmp;
2680 *nbsrv = nbsrv_tmp;
2681 *totuw = totuw_tmp;
2682}
Willy Tarreau2fbe6942020-10-23 18:02:54 +02002683
William Dauchyda3b4662021-01-25 17:29:01 +01002684/* Fill <stats> with the backend statistics. <stats> is preallocated array of
2685 * length <len>. If <selected_field> is != NULL, only fill this one. The length
2686 * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
2687 * this value, or if the selected field is not implemented for backends, the
2688 * function returns 0, otherwise, it returns 1. <flags> can take the value
2689 * STAT_SHLGNDS.
2690 */
2691int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len,
2692 enum stat_field *selected_field)
2693{
2694 enum stat_field current_field = (selected_field != NULL ? *selected_field : 0);
2695 long long be_samples_counter;
2696 unsigned int be_samples_window = TIME_STATS_SAMPLES;
2697 struct buffer *out = get_trash_chunk();
2698 int nbup, nbsrv, totuw;
2699 char *fld;
William Lallemand74c24fb2016-11-21 17:18:36 +01002700
William Dauchyda3b4662021-01-25 17:29:01 +01002701 if (len < ST_F_TOTAL_FIELDS)
2702 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002703
William Dauchyda3b4662021-01-25 17:29:01 +01002704 nbup = nbsrv = totuw = 0;
2705 /* some srv values compute for later if we either select all fields or
2706 * need them for one of the mentioned ones */
2707 if (selected_field == NULL || *selected_field == ST_F_STATUS ||
2708 *selected_field == ST_F_UWEIGHT)
2709 stats_fill_be_stats_computesrv(px, &nbup, &nbsrv, &totuw);
William Lallemand74c24fb2016-11-21 17:18:36 +01002710
William Dauchyda3b4662021-01-25 17:29:01 +01002711 /* same here but specific to time fields */
2712 if (selected_field == NULL || *selected_field == ST_F_QTIME ||
2713 *selected_field == ST_F_CTIME || *selected_field == ST_F_RTIME ||
2714 *selected_field == ST_F_TTIME) {
2715 be_samples_counter = (px->mode == PR_MODE_HTTP) ? px->be_counters.p.http.cum_req : px->be_counters.cum_lbconn;
2716 if (be_samples_counter < TIME_STATS_SAMPLES && be_samples_counter > 0)
2717 be_samples_window = be_samples_counter;
William Lallemand74c24fb2016-11-21 17:18:36 +01002718 }
2719
William Dauchyda3b4662021-01-25 17:29:01 +01002720 for (; current_field < ST_F_TOTAL_FIELDS; current_field++) {
2721 struct field metric = { 0 };
William Lallemand74c24fb2016-11-21 17:18:36 +01002722
William Dauchyda3b4662021-01-25 17:29:01 +01002723 switch (current_field) {
2724 case ST_F_PXNAME:
2725 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
2726 break;
2727 case ST_F_SVNAME:
2728 metric = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
2729 break;
2730 case ST_F_MODE:
2731 metric = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
2732 break;
2733 case ST_F_QCUR:
Willy Tarreau7f3c1df2021-06-18 09:22:21 +02002734 metric = mkf_u32(0, px->queue.length);
William Dauchyda3b4662021-01-25 17:29:01 +01002735 break;
2736 case ST_F_QMAX:
2737 metric = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
2738 break;
2739 case ST_F_SCUR:
2740 metric = mkf_u32(0, px->beconn);
2741 break;
2742 case ST_F_SMAX:
2743 metric = mkf_u32(FN_MAX, px->be_counters.conn_max);
2744 break;
2745 case ST_F_SLIM:
2746 metric = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
2747 break;
2748 case ST_F_STOT:
2749 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
2750 break;
2751 case ST_F_BIN:
2752 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
2753 break;
2754 case ST_F_BOUT:
2755 metric = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
2756 break;
2757 case ST_F_DREQ:
2758 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
2759 break;
2760 case ST_F_DRESP:
2761 metric = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
2762 break;
2763 case ST_F_ECON:
2764 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
2765 break;
2766 case ST_F_ERESP:
2767 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
2768 break;
2769 case ST_F_WRETR:
2770 metric = mkf_u64(FN_COUNTER, px->be_counters.retries);
2771 break;
2772 case ST_F_WREDIS:
2773 metric = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
2774 break;
2775 case ST_F_WREW:
2776 metric = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
2777 break;
2778 case ST_F_EINT:
2779 metric = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
2780 break;
2781 case ST_F_CONNECT:
2782 metric = mkf_u64(FN_COUNTER, px->be_counters.connect);
2783 break;
2784 case ST_F_REUSE:
2785 metric = mkf_u64(FN_COUNTER, px->be_counters.reuse);
2786 break;
2787 case ST_F_STATUS:
2788 fld = chunk_newstr(out);
2789 chunk_appendf(out, "%s", (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
2790 if (flags & (STAT_HIDE_MAINT|STAT_HIDE_DOWN))
2791 chunk_appendf(out, " (%d/%d)", nbup, nbsrv);
2792 metric = mkf_str(FO_STATUS, fld);
2793 break;
Cedric Paillet7d6644e2022-12-08 09:17:00 +00002794 case ST_F_AGG_SRV_CHECK_STATUS: // DEPRECATED
2795 case ST_F_AGG_SRV_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +01002796 metric = mkf_u32(FN_GAUGE, 0);
2797 break;
Cedric Paillete06e31e2022-12-08 09:17:01 +00002798 case ST_F_AGG_CHECK_STATUS:
2799 metric = mkf_u32(FN_GAUGE, 0);
2800 break;
William Dauchyda3b4662021-01-25 17:29:01 +01002801 case ST_F_WEIGHT:
2802 metric = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
2803 break;
2804 case ST_F_UWEIGHT:
2805 metric = mkf_u32(FN_AVG, totuw);
2806 break;
2807 case ST_F_ACT:
2808 metric = mkf_u32(0, px->srv_act);
2809 break;
2810 case ST_F_BCK:
2811 metric = mkf_u32(0, px->srv_bck);
2812 break;
2813 case ST_F_CHKDOWN:
2814 metric = mkf_u64(FN_COUNTER, px->down_trans);
2815 break;
2816 case ST_F_LASTCHG:
Willy Tarreau69530f52023-04-28 09:16:15 +02002817 metric = mkf_u32(FN_AGE, ns_to_sec(now_ns) - px->last_change);
William Dauchyda3b4662021-01-25 17:29:01 +01002818 break;
2819 case ST_F_DOWNTIME:
2820 if (px->srv)
2821 metric = mkf_u32(FN_COUNTER, be_downtime(px));
2822 break;
2823 case ST_F_PID:
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002824 metric = mkf_u32(FO_KEY, 1);
William Dauchyda3b4662021-01-25 17:29:01 +01002825 break;
2826 case ST_F_IID:
2827 metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
2828 break;
2829 case ST_F_SID:
2830 metric = mkf_u32(FO_KEY|FS_SERVICE, 0);
2831 break;
2832 case ST_F_LBTOT:
2833 metric = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
2834 break;
2835 case ST_F_TYPE:
2836 metric = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
2837 break;
2838 case ST_F_RATE:
2839 metric = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
2840 break;
2841 case ST_F_RATE_MAX:
2842 metric = mkf_u32(0, px->be_counters.sps_max);
2843 break;
2844 case ST_F_COOKIE:
2845 if (flags & STAT_SHLGNDS && px->cookie_name)
2846 metric = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
2847 break;
2848 case ST_F_ALGO:
2849 if (flags & STAT_SHLGNDS)
2850 metric = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2851 break;
2852 case ST_F_REQ_TOT:
2853 if (px->mode == PR_MODE_HTTP)
2854 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
2855 break;
2856 case ST_F_HRSP_1XX:
2857 if (px->mode == PR_MODE_HTTP)
2858 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
2859 break;
2860 case ST_F_HRSP_2XX:
2861 if (px->mode == PR_MODE_HTTP)
2862 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
2863 break;
2864 case ST_F_HRSP_3XX:
2865 if (px->mode == PR_MODE_HTTP)
2866 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
2867 break;
2868 case ST_F_HRSP_4XX:
2869 if (px->mode == PR_MODE_HTTP)
2870 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
2871 break;
2872 case ST_F_HRSP_5XX:
2873 if (px->mode == PR_MODE_HTTP)
2874 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
2875 break;
2876 case ST_F_HRSP_OTHER:
2877 if (px->mode == PR_MODE_HTTP)
2878 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
2879 break;
2880 case ST_F_CACHE_LOOKUPS:
2881 if (px->mode == PR_MODE_HTTP)
2882 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
2883 break;
2884 case ST_F_CACHE_HITS:
2885 if (px->mode == PR_MODE_HTTP)
2886 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
2887 break;
2888 case ST_F_CLI_ABRT:
2889 metric = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
2890 break;
2891 case ST_F_SRV_ABRT:
2892 metric = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
2893 break;
2894 case ST_F_COMP_IN:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002895 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_in[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002896 break;
2897 case ST_F_COMP_OUT:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002898 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_out[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002899 break;
2900 case ST_F_COMP_BYP:
Olivier Houcharddea25f52023-04-06 00:33:01 +02002901 metric = mkf_u64(FN_COUNTER, px->be_counters.comp_byp[COMP_DIR_RES]);
William Dauchyda3b4662021-01-25 17:29:01 +01002902 break;
2903 case ST_F_COMP_RSP:
2904 metric = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
2905 break;
2906 case ST_F_LASTSESS:
2907 metric = mkf_s32(FN_AGE, be_lastsession(px));
2908 break;
2909 case ST_F_QTIME:
2910 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, be_samples_window));
2911 break;
2912 case ST_F_CTIME:
2913 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, be_samples_window));
2914 break;
2915 case ST_F_RTIME:
2916 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, be_samples_window));
2917 break;
2918 case ST_F_TTIME:
2919 metric = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, be_samples_window));
2920 break;
2921 case ST_F_QT_MAX:
2922 metric = mkf_u32(FN_MAX, px->be_counters.qtime_max);
2923 break;
2924 case ST_F_CT_MAX:
2925 metric = mkf_u32(FN_MAX, px->be_counters.ctime_max);
2926 break;
2927 case ST_F_RT_MAX:
2928 metric = mkf_u32(FN_MAX, px->be_counters.dtime_max);
2929 break;
2930 case ST_F_TT_MAX:
2931 metric = mkf_u32(FN_MAX, px->be_counters.ttime_max);
2932 break;
2933 default:
2934 /* not used for backends. If a specific metric
2935 * is requested, return an error. Otherwise continue.
2936 */
2937 if (selected_field != NULL)
2938 return 0;
2939 continue;
2940 }
2941 stats[current_field] = metric;
2942 if (selected_field != NULL)
2943 break;
2944 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002945 return 1;
2946}
2947
Christopher Fauleta8b76842022-12-16 15:08:36 +01002948/* Dumps a line for backend <px> to the local trash buffer for and uses the
2949 * state from stream interface <si>. The caller is responsible for clearing the
2950 * local trash buffer if needed. Returns non-zero if it emits anything, zero
2951 * otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01002952 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002953static int stats_dump_be_stats(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002954{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002955 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002956 struct show_stat_ctx *ctx = appctx->svcctx;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002957 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
2958 struct stats_module *mod;
2959 size_t stats_count = ST_F_TOTAL_FIELDS;
William Lallemand74c24fb2016-11-21 17:18:36 +01002960
2961 if (!(px->cap & PR_CAP_BE))
2962 return 0;
2963
Willy Tarreau91cefca2022-05-03 17:08:29 +02002964 if ((ctx->flags & STAT_BOUND) && !(ctx->type & (1 << STATS_TYPE_BE)))
William Lallemand74c24fb2016-11-21 17:18:36 +01002965 return 0;
2966
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002967 memset(stats, 0, sizeof(struct field) * stat_count[STATS_DOMAIN_PROXY]);
2968
Willy Tarreau91cefca2022-05-03 17:08:29 +02002969 if (!stats_fill_be_stats(px, ctx->flags, stats, ST_F_TOTAL_FIELDS, NULL))
William Lallemand74c24fb2016-11-21 17:18:36 +01002970 return 0;
2971
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02002972 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
2973 struct extra_counters *counters;
2974
2975 if (stats_get_domain(mod->domain_flags) != STATS_DOMAIN_PROXY)
2976 continue;
2977
2978 if (!(stats_px_get_cap(mod->domain_flags) & STATS_PX_CAP_BE)) {
2979 stats_count += mod->stats_count;
2980 continue;
2981 }
2982
2983 counters = EXTRA_COUNTERS_GET(px->extra_counters_be, mod);
2984 mod->fill_stats(counters, stats + stats_count);
2985 stats_count += mod->stats_count;
2986 }
2987
2988 return stats_dump_one_line(stats, stats_count, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01002989}
2990
Christopher Fauleta8b76842022-12-16 15:08:36 +01002991/* Dumps the HTML table header for proxy <px> to the local trash buffer for and
2992 * uses the state from stream connector <sc> and per-uri parameters <uri>. The
2993 * caller is responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01002994 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02002995static void stats_dump_html_px_hdr(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01002996{
Willy Tarreaucaff6312022-05-27 10:17:46 +02002997 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02002998 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01002999 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003000 struct stats_module *mod;
3001 int stats_module_len = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003002
Willy Tarreau91cefca2022-05-03 17:08:29 +02003003 if (px->cap & PR_CAP_BE && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003004 /* A form to enable/disable this proxy servers */
3005
Willy Tarreau91cefca2022-05-03 17:08:29 +02003006 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003007 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003008 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003009 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003010
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003011 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02003012 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3013 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003014 }
3015
Christopher Fauleta8b76842022-12-16 15:08:36 +01003016 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003017 "<form method=\"post\">");
3018 }
3019
3020 /* print a new table */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003021 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003022 "<table class=\"tbl\" width=\"100%%\">\n"
3023 "<tr class=\"titre\">"
3024 "<th class=\"pxname\" width=\"10%%\">");
3025
Christopher Fauleta8b76842022-12-16 15:08:36 +01003026 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003027 "<a name=\"%s\"></a>%s"
3028 "<a class=px href=\"#%s\">%s</a>",
3029 px->id,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003030 (ctx->flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01003031 px->id, px->id);
3032
Willy Tarreau91cefca2022-05-03 17:08:29 +02003033 if (ctx->flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003034 /* cap, mode, id */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003035 chunk_appendf(&trash_chunk, "<div class=tips>cap: %s, mode: %s, id: %d",
William Lallemand74c24fb2016-11-21 17:18:36 +01003036 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3037 px->uuid);
Christopher Fauleta8b76842022-12-16 15:08:36 +01003038 chunk_appendf(&trash_chunk, "</div>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003039 }
3040
Christopher Fauleta8b76842022-12-16 15:08:36 +01003041 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003042 "%s</th>"
3043 "<th class=\"%s\" width=\"90%%\">%s</th>"
3044 "</tr>\n"
3045 "</table>\n"
3046 "<table class=\"tbl\" width=\"100%%\">\n"
3047 "<tr class=\"titre\">",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003048 (ctx->flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01003049 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3050
Willy Tarreau91cefca2022-05-03 17:08:29 +02003051 if (ctx->flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003052 /* Column heading for Enable or Disable server */
Christopher Fauletbc271ec2019-12-02 11:29:04 +01003053 if ((px->cap & PR_CAP_BE) && px->srv)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003054 chunk_appendf(&trash_chunk,
Christopher Fauletbc271ec2019-12-02 11:29:04 +01003055 "<th rowspan=2 width=1><input type=\"checkbox\" "
3056 "onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) "
3057 "document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
3058 px->id,
3059 px->id);
3060 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003061 chunk_appendf(&trash_chunk, "<th rowspan=2></th>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003062 }
3063
Christopher Fauleta8b76842022-12-16 15:08:36 +01003064 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003065 "<th rowspan=2></th>"
3066 "<th colspan=3>Queue</th>"
3067 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
3068 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3069 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003070 "<th colspan=9>Server</th>");
3071
Willy Tarreau91cefca2022-05-03 17:08:29 +02003072 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003073 // calculate the count of module for colspan attribute
3074 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
3075 ++stats_module_len;
3076 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003077 chunk_appendf(&trash_chunk, "<th colspan=%d>Extra modules</th>",
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003078 stats_module_len);
3079 }
3080
Christopher Fauleta8b76842022-12-16 15:08:36 +01003081 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003082 "</tr>\n"
3083 "<tr class=\"titre\">"
3084 "<th>Cur</th><th>Max</th><th>Limit</th>"
3085 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
3086 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
3087 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3088 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3089 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3090 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003091 "<th>Thrtle</th>\n");
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003092
Willy Tarreau91cefca2022-05-03 17:08:29 +02003093 if (ctx->flags & STAT_SHMODULES) {
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003094 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
Christopher Fauleta8b76842022-12-16 15:08:36 +01003095 chunk_appendf(&trash_chunk, "<th>%s</th>", mod->name);
Amaury Denoyelle0b70a8a2020-10-05 11:49:45 +02003096 }
Amaury Denoyellee3f576c2020-10-05 11:49:44 +02003097 }
3098
Christopher Fauleta8b76842022-12-16 15:08:36 +01003099 chunk_appendf(&trash_chunk, "</tr>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003100}
3101
Christopher Fauleta8b76842022-12-16 15:08:36 +01003102/* Dumps the HTML table trailer for proxy <px> to the local trash buffer for and
3103 * uses the state from stream connector <sc>. The caller is responsible for
3104 * clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003105 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003106static void stats_dump_html_px_end(struct stconn *sc, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01003107{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003108 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003109 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Faulet693b23b2022-02-28 09:09:05 +01003110
Christopher Fauleta8b76842022-12-16 15:08:36 +01003111 chunk_appendf(&trash_chunk, "</table>");
William Lallemand74c24fb2016-11-21 17:18:36 +01003112
Willy Tarreau91cefca2022-05-03 17:08:29 +02003113 if ((px->cap & PR_CAP_BE) && px->srv && (ctx->flags & STAT_ADMIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003114 /* close the form used to enable/disable this proxy servers */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003115 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003116 "Choose the action to perform on the checked servers : "
3117 "<select name=action>"
3118 "<option value=\"\"></option>"
3119 "<option value=\"ready\">Set state to READY</option>"
3120 "<option value=\"drain\">Set state to DRAIN</option>"
3121 "<option value=\"maint\">Set state to MAINT</option>"
3122 "<option value=\"dhlth\">Health: disable checks</option>"
3123 "<option value=\"ehlth\">Health: enable checks</option>"
3124 "<option value=\"hrunn\">Health: force UP</option>"
3125 "<option value=\"hnolb\">Health: force NOLB</option>"
3126 "<option value=\"hdown\">Health: force DOWN</option>"
3127 "<option value=\"dagent\">Agent: disable checks</option>"
3128 "<option value=\"eagent\">Agent: enable checks</option>"
3129 "<option value=\"arunn\">Agent: force UP</option>"
3130 "<option value=\"adown\">Agent: force DOWN</option>"
3131 "<option value=\"shutdown\">Kill Sessions</option>"
3132 "</select>"
3133 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3134 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3135 "</form>",
3136 px->uuid);
3137 }
3138
Christopher Fauleta8b76842022-12-16 15:08:36 +01003139 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003140}
3141
3142/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02003143 * Dumps statistics for a proxy. The output is sent to the stream connector's
William Lallemand74c24fb2016-11-21 17:18:36 +01003144 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3145 * buffer space, or non-zero if everything completed. This function is used
3146 * both by the CLI and the HTTP entry points, and is able to dump the output
3147 * in HTML or CSV formats. If the later, <uri> must be NULL.
3148 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003149int stats_dump_proxy_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003150 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003151{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003152 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003153 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003154 struct stream *s = __sc_strm(sc);
3155 struct channel *rep = sc_ic(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +01003156 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
3157 struct listener *l;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003158 int current_field;
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003159 int px_st = ctx->px_st;
William Lallemand74c24fb2016-11-21 17:18:36 +01003160
Christopher Fauleta8b76842022-12-16 15:08:36 +01003161 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003162more:
3163 current_field = ctx->field;
William Lallemand74c24fb2016-11-21 17:18:36 +01003164
Willy Tarreau91cefca2022-05-03 17:08:29 +02003165 switch (ctx->px_st) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003166 case STAT_PX_ST_INIT:
3167 /* we are on a new proxy */
3168 if (uri && uri->scope) {
3169 /* we have a limited scope, we have to check the proxy name */
3170 struct stat_scope *scope;
3171 int len;
3172
3173 len = strlen(px->id);
3174 scope = uri->scope;
3175
3176 while (scope) {
3177 /* match exact proxy name */
3178 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3179 break;
3180
3181 /* match '.' which means 'self' proxy */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003182 if (strcmp(scope->px_id, ".") == 0 && px == s->be)
William Lallemand74c24fb2016-11-21 17:18:36 +01003183 break;
3184 scope = scope->next;
3185 }
3186
3187 /* proxy name not found : don't dump anything */
3188 if (scope == NULL)
3189 return 1;
3190 }
3191
3192 /* if the user has requested a limited output and the proxy
3193 * name does not match, skip it.
3194 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003195 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003196 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003197
Willy Tarreau91cefca2022-05-03 17:08:29 +02003198 if (strnistr(px->id, strlen(px->id), scope_ptr, ctx->scope_len) == NULL)
Christopher Fauleted7a0662019-01-14 11:07:34 +01003199 return 1;
3200 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003201
Willy Tarreau91cefca2022-05-03 17:08:29 +02003202 if ((ctx->flags & STAT_BOUND) &&
3203 (ctx->iid != -1) &&
3204 (px->uuid != ctx->iid))
William Lallemand74c24fb2016-11-21 17:18:36 +01003205 return 1;
3206
Willy Tarreau91cefca2022-05-03 17:08:29 +02003207 ctx->px_st = STAT_PX_ST_TH;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003208 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003209
3210 case STAT_PX_ST_TH:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003211 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003212 stats_dump_html_px_hdr(sc, px);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003213 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003214 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003215 }
3216
Willy Tarreau91cefca2022-05-03 17:08:29 +02003217 ctx->px_st = STAT_PX_ST_FE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003218 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003219
3220 case STAT_PX_ST_FE:
3221 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003222 if (stats_dump_fe_stats(sc, px)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003223 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003224 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003225 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003226 if (ctx->field)
3227 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003228 }
3229
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003230 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003231 ctx->obj2 = px->conf.listeners.n;
3232 ctx->px_st = STAT_PX_ST_LI;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003233 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003234
3235 case STAT_PX_ST_LI:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02003236 /* obj2 points to listeners list as initialized above */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003237 for (; ctx->obj2 != &px->conf.listeners; ctx->obj2 = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01003238 if (htx) {
Christopher Fauleta236c582023-05-09 16:31:01 +02003239 if (htx_almost_full(htx)) {
3240 sc_need_room(sc, htx->size / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003241 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003242 }
Christopher Fauletef779222018-10-31 08:47:01 +01003243 }
3244 else {
Christopher Fauleta236c582023-05-09 16:31:01 +02003245 if (buffer_almost_full(&rep->buf)) {
3246 sc_need_room(sc, b_size(&rep->buf) / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003247 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003248 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003249 }
3250
Willy Tarreau91cefca2022-05-03 17:08:29 +02003251 l = LIST_ELEM(ctx->obj2, struct listener *, by_fe);
William Lallemand74c24fb2016-11-21 17:18:36 +01003252 if (!l->counters)
3253 continue;
3254
Willy Tarreau91cefca2022-05-03 17:08:29 +02003255 if (ctx->flags & STAT_BOUND) {
3256 if (!(ctx->type & (1 << STATS_TYPE_SO)))
William Lallemand74c24fb2016-11-21 17:18:36 +01003257 break;
3258
Willy Tarreau91cefca2022-05-03 17:08:29 +02003259 if (ctx->sid != -1 && l->luid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003260 continue;
3261 }
3262
3263 /* print the frontend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003264 if (stats_dump_li_stats(sc, px, l)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003265 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003266 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003267 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003268 if (ctx->field)
3269 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003270 }
Aurelien DARRAGON9b07d4f2023-02-03 11:43:05 +01003271 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003272 }
3273
Willy Tarreau91cefca2022-05-03 17:08:29 +02003274 ctx->obj2 = px->srv; /* may be NULL */
3275 ctx->px_st = STAT_PX_ST_SV;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003276 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003277
3278 case STAT_PX_ST_SV:
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003279 /* check for dump resumption */
3280 if (px_st == STAT_PX_ST_SV) {
3281 struct server *cur = ctx->obj2;
3282
3283 /* re-entrant dump */
3284 BUG_ON(!cur);
3285 if (cur->flags & SRV_F_DELETED) {
3286 /* the server could have been marked as deleted
3287 * between two dumping attempts, skip it.
3288 */
3289 cur = cur->next;
3290 }
3291 srv_drop(ctx->obj2); /* drop old srv taken on last dumping attempt */
3292 ctx->obj2 = cur; /* could be NULL */
3293 /* back to normal */
3294 }
3295
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003296 /* obj2 points to servers list as initialized above.
3297 *
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003298 * A server may be removed during the stats dumping.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003299 * Temporarily increment its refcount to prevent its
Aurelien DARRAGON99a8d0f2023-01-31 09:51:32 +01003300 * anticipated cleaning. Call srv_drop() to release it.
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003301 */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003302 for (; ctx->obj2 != NULL;
3303 ctx->obj2 = srv_drop(sv)) {
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003304
Willy Tarreau91cefca2022-05-03 17:08:29 +02003305 sv = ctx->obj2;
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003306 srv_take(sv);
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003307
Christopher Fauletef779222018-10-31 08:47:01 +01003308 if (htx) {
Christopher Fauleta236c582023-05-09 16:31:01 +02003309 if (htx_almost_full(htx)) {
3310 sc_need_room(sc, htx->size / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003311 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003312 }
Christopher Fauletef779222018-10-31 08:47:01 +01003313 }
3314 else {
Christopher Fauleta236c582023-05-09 16:31:01 +02003315 if (buffer_almost_full(&rep->buf)) {
3316 sc_need_room(sc, b_size(&rep->buf) / 2);
Christopher Fauletef779222018-10-31 08:47:01 +01003317 goto full;
Christopher Fauleta236c582023-05-09 16:31:01 +02003318 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003319 }
3320
Willy Tarreau91cefca2022-05-03 17:08:29 +02003321 if (ctx->flags & STAT_BOUND) {
3322 if (!(ctx->type & (1 << STATS_TYPE_SV))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02003323 srv_drop(sv);
William Lallemand74c24fb2016-11-21 17:18:36 +01003324 break;
Amaury Denoyelle0a8d05d2021-08-25 14:39:53 +02003325 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003326
Willy Tarreau91cefca2022-05-03 17:08:29 +02003327 if (ctx->sid != -1 && sv->puid != ctx->sid)
William Lallemand74c24fb2016-11-21 17:18:36 +01003328 continue;
3329 }
3330
Willy Tarreau3e320362020-10-23 17:28:57 +02003331 /* do not report disabled servers */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003332 if (ctx->flags & STAT_HIDE_MAINT &&
Willy Tarreau3e320362020-10-23 17:28:57 +02003333 sv->cur_admin & SRV_ADMF_MAINT) {
3334 continue;
3335 }
3336
William Lallemand74c24fb2016-11-21 17:18:36 +01003337 svs = sv;
3338 while (svs->track)
3339 svs = svs->track;
3340
3341 /* do not report servers which are DOWN and not changing state */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003342 if ((ctx->flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02003343 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
3344 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01003345 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
3346 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
3347 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
3348 continue;
3349 }
3350
Willy Tarreaucaff6312022-05-27 10:17:46 +02003351 if (stats_dump_sv_stats(sc, px, sv)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003352 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003353 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003354 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003355 if (ctx->field)
3356 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003357 }
Aurelien DARRAGON28a23612023-02-02 18:13:30 +01003358 current_field = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003359 } /* for sv */
3360
Willy Tarreau91cefca2022-05-03 17:08:29 +02003361 ctx->px_st = STAT_PX_ST_BE;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003362 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003363
3364 case STAT_PX_ST_BE:
3365 /* print the backend */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003366 if (stats_dump_be_stats(sc, px)) {
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003367 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003368 goto full;
Aurelien DARRAGON90304dc2023-02-02 19:01:02 +01003369 ctx->flags |= STAT_STARTED;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003370 if (ctx->field)
3371 goto more;
William Lallemand74c24fb2016-11-21 17:18:36 +01003372 }
3373
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003374 current_field = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003375 ctx->px_st = STAT_PX_ST_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003376 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003377
3378 case STAT_PX_ST_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003379 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003380 stats_dump_html_px_end(sc, px);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003381 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003382 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003383 }
3384
Willy Tarreau91cefca2022-05-03 17:08:29 +02003385 ctx->px_st = STAT_PX_ST_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003386 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003387
3388 case STAT_PX_ST_FIN:
3389 return 1;
3390
3391 default:
3392 /* unknown state, we should put an abort() here ! */
3393 return 1;
3394 }
Christopher Fauletef779222018-10-31 08:47:01 +01003395
3396 full:
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003397 /* restore previous field */
3398 ctx->field = current_field;
Christopher Fauletef779222018-10-31 08:47:01 +01003399 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003400}
3401
Christopher Fauleta8b76842022-12-16 15:08:36 +01003402/* Dumps the HTTP stats head block to the local trash buffer for and uses the
3403 * per-uri parameters <uri>. The caller is responsible for clearing the local
3404 * trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003405 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02003406static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003407{
Willy Tarreau91cefca2022-05-03 17:08:29 +02003408 struct show_stat_ctx *ctx = appctx->svcctx;
3409
William Lallemand74c24fb2016-11-21 17:18:36 +01003410 /* WARNING! This must fit in the first buffer !!! */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003411 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003412 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3413 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3414 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003415 "<link rel=\"icon\" href=\"data:,\">\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003416 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3417 "<style type=\"text/css\"><!--\n"
3418 "body {"
3419 " font-family: arial, helvetica, sans-serif;"
3420 " font-size: 12px;"
3421 " font-weight: normal;"
3422 " color: black;"
3423 " background: white;"
3424 "}\n"
3425 "th,td {"
3426 " font-size: 10px;"
3427 "}\n"
3428 "h1 {"
3429 " font-size: x-large;"
3430 " margin-bottom: 0.5em;"
3431 "}\n"
3432 "h2 {"
3433 " font-family: helvetica, arial;"
3434 " font-size: x-large;"
3435 " font-weight: bold;"
3436 " font-style: italic;"
3437 " color: #6020a0;"
3438 " margin-top: 0em;"
3439 " margin-bottom: 0em;"
3440 "}\n"
3441 "h3 {"
3442 " font-family: helvetica, arial;"
3443 " font-size: 16px;"
3444 " font-weight: bold;"
3445 " color: #b00040;"
3446 " background: #e8e8d0;"
3447 " margin-top: 0em;"
3448 " margin-bottom: 0em;"
3449 "}\n"
3450 "li {"
3451 " margin-top: 0.25em;"
3452 " margin-right: 2em;"
3453 "}\n"
3454 ".hr {margin-top: 0.25em;"
3455 " border-color: black;"
3456 " border-bottom-style: solid;"
3457 "}\n"
3458 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3459 ".total {background: #20D0D0;color: #ffff80;}\n"
3460 ".frontend {background: #e8e8d0;}\n"
3461 ".socket {background: #d0d0d0;}\n"
3462 ".backend {background: #e8e8d0;}\n"
3463 ".active_down {background: #ff9090;}\n"
3464 ".active_going_up {background: #ffd020;}\n"
3465 ".active_going_down {background: #ffffa0;}\n"
3466 ".active_up {background: #c0ffc0;}\n"
3467 ".active_nolb {background: #20a0ff;}\n"
3468 ".active_draining {background: #20a0FF;}\n"
3469 ".active_no_check {background: #e0e0e0;}\n"
3470 ".backup_down {background: #ff9090;}\n"
3471 ".backup_going_up {background: #ff80ff;}\n"
3472 ".backup_going_down {background: #c060ff;}\n"
3473 ".backup_up {background: #b0d0ff;}\n"
3474 ".backup_nolb {background: #90b0e0;}\n"
3475 ".backup_draining {background: #cc9900;}\n"
3476 ".backup_no_check {background: #e0e0e0;}\n"
3477 ".maintain {background: #c07820;}\n"
3478 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3479 "\n"
3480 "a.px:link {color: #ffff40; text-decoration: none;}"
3481 "a.px:visited {color: #ffff40; text-decoration: none;}"
3482 "a.px:hover {color: #ffffff; text-decoration: none;}"
3483 "a.lfsb:link {color: #000000; text-decoration: none;}"
3484 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3485 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3486 "\n"
3487 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3488 "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"
3489 "table.tbl td.ac { text-align: center;}\n"
3490 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3491 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3492 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3493 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3494 "\n"
3495 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3496 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3497 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3498 "table.det { border-collapse: collapse; border-style: none; }\n"
3499 "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"
3500 "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"
3501 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Thierry Fournierb6b1cde2021-09-24 00:03:07 +02003502 "div.tips {\n"
3503 " display:block;\n"
3504 " visibility:hidden;\n"
3505 " z-index:2147483647;\n"
3506 " position:absolute;\n"
3507 " padding:2px 4px 3px;\n"
3508 " background:#f0f060; color:#000000;\n"
3509 " border:1px solid #7040c0;\n"
3510 " white-space:nowrap;\n"
3511 " font-style:normal;font-size:11px;font-weight:normal;\n"
3512 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3513 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3514 "}\n"
3515 "u:hover div.tips {visibility:visible;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003516 "@media (prefers-color-scheme: dark) {\n"
3517 " body { font-family: arial, helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #e8e6e3; background: #131516;}\n"
3518 " h1 { color: #a265e0!important; }\n"
3519 " h2 { color: #a265e0; }\n"
3520 " h3 { color: #ff5190; background-color: #3e3e1f; }\n"
3521 " a { color: #3391ff; }\n"
3522 " input { background-color: #2f3437; }\n"
3523 " .hr { border-color: #8c8273; }\n"
3524 " .titre { background-color: #1aa6a6; color: #e8e6e3; }\n"
3525 " .frontend {background: #2f3437;}\n"
Marno Krahmera690b732022-03-08 13:45:09 +01003526 " .socket {background: #2a2d2f;}\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003527 " .backend {background: #2f3437;}\n"
3528 " .active_down {background: #760000;}\n"
3529 " .active_going_up {background: #b99200;}\n"
3530 " .active_going_down {background: #6c6c00;}\n"
3531 " .active_up {background: #165900;}\n"
3532 " .active_nolb {background: #006ab9;}\n"
3533 " .active_draining {background: #006ab9;}\n"
3534 " .active_no_check {background: #2a2d2f;}\n"
3535 " .backup_down {background: #760000;}\n"
3536 " .backup_going_up {background: #7f007f;}\n"
3537 " .backup_going_down {background: #580092;}\n"
3538 " .backup_up {background: #2e3234;}\n"
3539 " .backup_nolb {background: #1e3c6a;}\n"
3540 " .backup_draining {background: #a37a00;}\n"
3541 " .backup_no_check {background: #2a2d2f;}\n"
3542 " .maintain {background: #9a601a;}\n"
3543 " a.px:link {color: #d8d83b; text-decoration: none;}\n"
3544 " a.px:visited {color: #d8d83b; text-decoration: none;}\n"
3545 " a.px:hover {color: #ffffff; text-decoration: none;}\n"
3546 " a.lfsb:link {color: #e8e6e3; text-decoration: none;}\n"
3547 " a.lfsb:visited {color: #e8e6e3; text-decoration: none;}\n"
3548 " a.lfsb:hover {color: #b5afa6; text-decoration: none;}\n"
3549 " table.tbl th.empty { background-color: #181a1b; }\n"
Willy Tarreau00147f72022-04-11 07:59:27 +02003550 " table.tbl th.desc { background: #181a1b; }\n"
Thierry Fourniere9ed63e2021-09-23 23:59:49 +02003551 " table.tbl th.pxname { background-color: #8d0033; color: #ffff46; }\n"
3552 " table.tbl th { border-color: #808080; }\n"
3553 " table.tbl td { border-color: #808080; }\n"
3554 " u {text-decoration:none; border-bottom: 1px dotted #e8e6e3;}\n"
3555 " div.tips {\n"
3556 " background:#8e8e0d;\n"
3557 " color:#e8e6e3;\n"
3558 " border-color: #4e2c86;\n"
3559 " -moz-box-shadow: #60686c 2px 2px 3px;\n"
3560 " -webkit-box-shadow: #60686c 2px 2px 3px;\n"
3561 " box-shadow: #60686c 2px 2px 3px;\n"
3562 " }\n"
3563 "}\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003564 "-->\n"
3565 "</style></head>\n",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003566 (ctx->flags & STAT_SHNODE) ? " on " : "",
3567 (ctx->flags & STAT_SHNODE) ? (uri && uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01003568 );
3569}
3570
Christopher Fauleta8b76842022-12-16 15:08:36 +01003571/* Dumps the HTML stats information block to the local trash buffer for and uses
3572 * the state from stream connector <sc> and per-uri parameters <uri>. The caller
3573 * is responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003574 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003575static void stats_dump_html_info(struct stconn *sc, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003576{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003577 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003578 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc05d30e2023-04-28 14:50:29 +02003579 unsigned int up = ns_to_sec(now_ns - start_time_ns);
William Lallemand74c24fb2016-11-21 17:18:36 +01003580 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02003581 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Willy Tarreau6be8d092023-01-12 16:08:41 +01003582 unsigned long long bps;
3583 int thr;
3584
3585 for (bps = thr = 0; thr < global.nbthread; thr++)
3586 bps += 32ULL * read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
Willy Tarreau1713c032019-05-23 12:23:55 +02003587
3588 /* Turn the bytes per second to bits per second and take care of the
3589 * usual ethernet overhead in order to help figure how far we are from
3590 * interface saturation since it's the only case which usually matters.
3591 * For this we count the total size of an Ethernet frame on the wire
3592 * including preamble and IFG (1538) for the largest TCP segment it
3593 * transports (1448 with TCP timestamps). This is not valid for smaller
3594 * packets (under-estimated), but it gives a reasonably accurate
3595 * estimation of how far we are from uplink saturation.
3596 */
3597 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01003598
3599 /* WARNING! this has to fit the first packet too.
3600 * We are around 3.5 kB, add adding entries will
3601 * become tricky if we want to support 4kB buffers !
3602 */
Christopher Fauleta8b76842022-12-16 15:08:36 +01003603 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003604 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3605 PRODUCT_NAME "%s</a></h1>\n"
3606 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3607 "<hr width=\"100%%\" class=\"hr\">\n"
3608 "<h3>&gt; General process information</h3>\n"
3609 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01003610 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
Willy Tarreau3c4a2972023-05-11 12:02:21 +02003611 "<b>uptime = </b> %dd %dh%02dm%02ds; warnings = %u<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003612 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
Willy Tarreau96151022023-05-11 13:51:31 +02003613 "<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 +02003614 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01003615 "Running tasks: %d/%d; idle = %d %%<br>\n"
3616 "</td><td align=\"center\" nowrap>\n"
3617 "<table class=\"lgd\"><tr>\n"
3618 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3619 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3620 "</tr><tr>\n"
3621 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
3622 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
3623 "</tr><tr>\n"
3624 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
3625 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3626 "</tr><tr>\n"
3627 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3628 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
3629 "</tr><tr>\n"
3630 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3631 "</tr><tr>\n"
3632 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
3633 "</tr></table>\n"
3634 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
3635 "</td>"
3636 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3637 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3638 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003639 (ctx->flags & STAT_HIDEVER) ? "" : (stats_version_string),
3640 pid, (ctx->flags & STAT_SHNODE) ? " on " : "",
3641 (ctx->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3642 (ctx->flags & STAT_SHDESC) ? ": " : "",
3643 (ctx->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003644 pid, 1, 1, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01003645 up / 86400, (up % 86400) / 3600,
3646 (up % 3600) / 60, (up % 60),
Willy Tarreau3c4a2972023-05-11 12:02:21 +02003647 HA_ATOMIC_LOAD(&tot_warnings),
William Lallemand74c24fb2016-11-21 17:18:36 +01003648 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3649 global.rlimit_memmax ? " MB" : "",
3650 global.rlimit_nofile,
Willy Tarreau96151022023-05-11 13:51:31 +02003651 global.maxsock, global.maxconn, HA_ATOMIC_LOAD(&maxconn_reached), global.maxpipes,
William Lallemand74c24fb2016-11-21 17:18:36 +01003652 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02003653 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
3654 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreauf9d5e102021-10-08 10:43:59 +02003655 total_run_queues(), total_allocated_tasks(), clock_report_idle()
William Lallemand74c24fb2016-11-21 17:18:36 +01003656 );
3657
Willy Tarreau91cefca2022-05-03 17:08:29 +02003658 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3659 memcpy(scope_txt, scope_ptr, ctx->scope_len);
3660 scope_txt[ctx->scope_len] = '\0';
William Lallemand74c24fb2016-11-21 17:18:36 +01003661
Christopher Fauleta8b76842022-12-16 15:08:36 +01003662 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003663 "<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 +02003664 (ctx->scope_len > 0) ? scope_txt : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003665 STAT_SCOPE_TXT_MAXLEN);
3666
Willy Tarreau91cefca2022-05-03 17:08:29 +02003667 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
William Lallemand74c24fb2016-11-21 17:18:36 +01003668 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003669 if (ctx->scope_len) {
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003670 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02003671 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
3672 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003673 }
3674
Willy Tarreau91cefca2022-05-03 17:08:29 +02003675 if (ctx->flags & STAT_HIDE_DOWN)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003676 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003677 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
3678 uri->uri_prefix,
3679 "",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003680 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003681 scope_txt);
3682 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003683 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003684 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3685 uri->uri_prefix,
3686 ";up",
Willy Tarreau91cefca2022-05-03 17:08:29 +02003687 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003688 scope_txt);
3689
3690 if (uri->refresh > 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003691 if (ctx->flags & STAT_NO_REFRESH)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003692 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003693 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
3694 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003695 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003696 "",
3697 scope_txt);
3698 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01003699 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003700 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
3701 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003702 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003703 ";norefresh",
3704 scope_txt);
3705 }
3706
Christopher Fauleta8b76842022-12-16 15:08:36 +01003707 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003708 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
3709 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003710 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3711 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003712 scope_txt);
3713
Christopher Fauleta8b76842022-12-16 15:08:36 +01003714 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003715 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
3716 uri->uri_prefix,
3717 (uri->refresh > 0) ? ";norefresh" : "",
3718 scope_txt);
3719
Christopher Fauleta8b76842022-12-16 15:08:36 +01003720 chunk_appendf(&trash_chunk,
Christopher Faulet6338a082019-09-09 15:50:54 +02003721 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
3722 uri->uri_prefix,
3723 (uri->refresh > 0) ? ";norefresh" : "",
3724 scope_txt, uri->uri_prefix);
3725
Christopher Fauleta8b76842022-12-16 15:08:36 +01003726 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003727 "</ul></td>"
3728 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3729 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3730 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3731 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3732 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3733 "</ul>"
3734 "</td>"
3735 "</tr></table>\n"
3736 ""
3737 );
3738
Willy Tarreau91cefca2022-05-03 17:08:29 +02003739 if (ctx->st_code) {
3740 switch (ctx->st_code) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003741 case STAT_STATUS_DONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003742 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003743 "<p><div class=active_up>"
3744 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3745 "Action processed successfully."
3746 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003747 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3748 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003749 scope_txt);
3750 break;
3751 case STAT_STATUS_NONE:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003752 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003753 "<p><div class=active_going_down>"
3754 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3755 "Nothing has changed."
3756 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003757 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3758 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003759 scope_txt);
3760 break;
3761 case STAT_STATUS_PART:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003762 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003763 "<p><div class=active_going_down>"
3764 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3765 "Action partially processed.<br>"
3766 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3767 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003768 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3769 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003770 scope_txt);
3771 break;
3772 case STAT_STATUS_ERRP:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003773 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003774 "<p><div class=active_down>"
3775 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3776 "Action not processed because of invalid parameters."
3777 "<ul>"
3778 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003779 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01003780 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3781 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3782 "</ul>"
3783 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003784 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3785 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003786 scope_txt);
3787 break;
3788 case STAT_STATUS_EXCD:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003789 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003790 "<p><div class=active_down>"
3791 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3792 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3793 "You should retry with less servers at a time.</b>"
3794 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003795 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3796 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003797 scope_txt);
3798 break;
3799 case STAT_STATUS_DENY:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003800 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003801 "<p><div class=active_down>"
3802 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3803 "<b>Action denied.</b>"
3804 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003805 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3806 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003807 scope_txt);
3808 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003809 case STAT_STATUS_IVAL:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003810 chunk_appendf(&trash_chunk,
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003811 "<p><div class=active_down>"
3812 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3813 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
3814 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003815 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3816 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01003817 scope_txt);
3818 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01003819 default:
Christopher Fauleta8b76842022-12-16 15:08:36 +01003820 chunk_appendf(&trash_chunk,
William Lallemand74c24fb2016-11-21 17:18:36 +01003821 "<p><div class=active_no_check>"
3822 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
3823 "Unexpected result."
3824 "</div>\n", uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02003825 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
3826 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +01003827 scope_txt);
3828 }
Christopher Fauleta8b76842022-12-16 15:08:36 +01003829 chunk_appendf(&trash_chunk, "<p>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003830 }
3831}
3832
Christopher Fauleta8b76842022-12-16 15:08:36 +01003833/* Dumps the HTML stats trailer block to the local trash buffer. The caller is
3834 * responsible for clearing the local trash buffer if needed.
William Lallemand74c24fb2016-11-21 17:18:36 +01003835 */
3836static void stats_dump_html_end()
3837{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003838 chunk_appendf(&trash_chunk, "</body></html>\n");
William Lallemand74c24fb2016-11-21 17:18:36 +01003839}
3840
Christopher Fauleta8b76842022-12-16 15:08:36 +01003841/* Dumps the stats JSON header to the local trash buffer buffer which. The
3842 * caller is responsible for clearing it if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003843 */
3844static void stats_dump_json_header()
3845{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003846 chunk_strcat(&trash_chunk, "[");
Simon Horman05ee2132017-01-04 09:37:25 +01003847}
3848
3849
Christopher Fauleta8b76842022-12-16 15:08:36 +01003850/* Dumps the JSON stats trailer block to the local trash buffer. The caller is
3851 * responsible for clearing the local trash buffer if needed.
Simon Horman05ee2132017-01-04 09:37:25 +01003852 */
3853static void stats_dump_json_end()
3854{
Christopher Fauleta8b76842022-12-16 15:08:36 +01003855 chunk_strcat(&trash_chunk, "]\n");
Simon Horman05ee2132017-01-04 09:37:25 +01003856}
3857
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003858/* Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
3859 * a pointer to the current server/listener.
3860 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003861static int stats_dump_proxies(struct stconn *sc,
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003862 struct htx *htx,
3863 struct uri_auth *uri)
3864{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003865 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003866 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreaucaff6312022-05-27 10:17:46 +02003867 struct channel *rep = sc_ic(sc);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003868 struct proxy *px;
3869
3870 /* dump proxies */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003871 while (ctx->obj1) {
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003872 if (htx) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003873 if (htx_almost_full(htx)) {
3874 sc_need_room(sc, htx->size / 2);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003875 goto full;
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003876 }
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003877 }
3878 else {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003879 if (buffer_almost_full(&rep->buf)) {
3880 sc_need_room(sc, b_size(&rep->buf) / 2);
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003881 goto full;
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003882 }
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003883 }
3884
Willy Tarreau91cefca2022-05-03 17:08:29 +02003885 px = ctx->obj1;
Marno Krahmer07954fb2021-06-24 16:51:08 +02003886 /* Skip the global frontend proxies and non-networked ones.
William Lallemand85a16b22021-08-03 13:18:11 +02003887 * Also skip proxies that were disabled in the configuration
Marno Krahmer07954fb2021-06-24 16:51:08 +02003888 * This change allows retrieving stats from "old" proxies after a reload.
3889 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003890 if (!(px->flags & PR_FL_DISABLED) && px->uuid > 0 &&
William Lallemande7f74622021-07-28 17:45:18 +02003891 (px->cap & (PR_CAP_FE | PR_CAP_BE)) && !(px->cap & PR_CAP_INT)) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003892 if (stats_dump_proxy_to_buffer(sc, htx, px, uri) == 0)
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003893 return 0;
3894 }
3895
Willy Tarreau91cefca2022-05-03 17:08:29 +02003896 ctx->obj1 = px->next;
3897 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003898 ctx->field = 0;
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003899 }
3900
3901 return 1;
3902
3903 full:
Amaury Denoyelle98b81cb2020-10-02 18:32:04 +02003904 return 0;
3905}
3906
Willy Tarreau4596fe22022-05-17 19:07:51 +02003907/* This function dumps statistics onto the stream connector's read buffer in
William Lallemand74c24fb2016-11-21 17:18:36 +01003908 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3909 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3910 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3911 * and the stream must be closed, or -1 in case of any error. This function is
3912 * used by both the CLI and the HTTP handlers.
3913 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003914static int stats_dump_stat_to_buffer(struct stconn *sc, struct htx *htx,
Christopher Fauletef779222018-10-31 08:47:01 +01003915 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01003916{
Willy Tarreaucaff6312022-05-27 10:17:46 +02003917 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003918 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003919 enum stats_domain domain = ctx->domain;
William Lallemand74c24fb2016-11-21 17:18:36 +01003920
Christopher Fauleta8b76842022-12-16 15:08:36 +01003921 chunk_reset(&trash_chunk);
William Lallemand74c24fb2016-11-21 17:18:36 +01003922
Willy Tarreau41f88522022-05-03 18:39:27 +02003923 switch (ctx->state) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003924 case STAT_STATE_INIT:
3925 ctx->state = STAT_STATE_HEAD; /* let's start producing data */
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003926 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003927
Willy Tarreau6ef16482022-05-06 18:07:53 +02003928 case STAT_STATE_HEAD:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003929 if (ctx->flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02003930 stats_dump_html_head(appctx, uri);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003931 else if (ctx->flags & STAT_JSON_SCHM)
Christopher Fauleta8b76842022-12-16 15:08:36 +01003932 stats_dump_json_schema(&trash_chunk);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003933 else if (ctx->flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02003934 stats_dump_json_header();
Willy Tarreau91cefca2022-05-03 17:08:29 +02003935 else if (!(ctx->flags & STAT_FMT_TYPED))
3936 stats_dump_csv_header(ctx->domain);
William Lallemand74c24fb2016-11-21 17:18:36 +01003937
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003938 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003939 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003940
Willy Tarreau91cefca2022-05-03 17:08:29 +02003941 if (ctx->flags & STAT_JSON_SCHM) {
Willy Tarreau6ef16482022-05-06 18:07:53 +02003942 ctx->state = STAT_STATE_FIN;
Christopher Faulet6338a082019-09-09 15:50:54 +02003943 return 1;
3944 }
Willy Tarreau6ef16482022-05-06 18:07:53 +02003945 ctx->state = STAT_STATE_INFO;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003946 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003947
Willy Tarreau6ef16482022-05-06 18:07:53 +02003948 case STAT_STATE_INFO:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003949 if (ctx->flags & STAT_FMT_HTML) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02003950 stats_dump_html_info(sc, uri);
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003951 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003952 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003953 }
3954
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003955 if (domain == STATS_DOMAIN_PROXY)
Willy Tarreau91cefca2022-05-03 17:08:29 +02003956 ctx->obj1 = proxies_list;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003957
Willy Tarreau91cefca2022-05-03 17:08:29 +02003958 ctx->px_st = STAT_PX_ST_INIT;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01003959 ctx->field = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003960 ctx->state = STAT_STATE_LIST;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003961 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003962
Willy Tarreau6ef16482022-05-06 18:07:53 +02003963 case STAT_STATE_LIST:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003964 switch (domain) {
Emeric Brunf8642ee2021-10-29 17:59:18 +02003965 case STATS_DOMAIN_RESOLVERS:
Willy Tarreaucaff6312022-05-27 10:17:46 +02003966 if (!stats_dump_resolvers(sc, stat_l[domain],
Emeric Brund3b44952021-01-06 16:01:02 +01003967 stat_count[domain],
3968 &stats_module_list[domain])) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003969 return 0;
3970 }
3971 break;
3972
3973 case STATS_DOMAIN_PROXY:
3974 default:
3975 /* dump proxies */
Willy Tarreaucaff6312022-05-27 10:17:46 +02003976 if (!stats_dump_proxies(sc, htx, uri))
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003977 return 0;
3978 break;
3979 }
William Lallemand74c24fb2016-11-21 17:18:36 +01003980
Willy Tarreau6ef16482022-05-06 18:07:53 +02003981 ctx->state = STAT_STATE_END;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003982 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003983
Willy Tarreau6ef16482022-05-06 18:07:53 +02003984 case STAT_STATE_END:
Willy Tarreau91cefca2022-05-03 17:08:29 +02003985 if (ctx->flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
3986 if (ctx->flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01003987 stats_dump_html_end();
3988 else
3989 stats_dump_json_end();
Christopher Fauletf4258bd2023-05-05 10:59:39 +02003990 if (!stats_putchk(appctx, htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003991 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01003992 }
3993
Willy Tarreau6ef16482022-05-06 18:07:53 +02003994 ctx->state = STAT_STATE_FIN;
Willy Tarreau9eb93c02022-11-14 07:12:05 +01003995 __fallthrough;
William Lallemand74c24fb2016-11-21 17:18:36 +01003996
Willy Tarreau6ef16482022-05-06 18:07:53 +02003997 case STAT_STATE_FIN:
William Lallemand74c24fb2016-11-21 17:18:36 +01003998 return 1;
3999
4000 default:
4001 /* unknown state ! */
Willy Tarreau6ef16482022-05-06 18:07:53 +02004002 ctx->state = STAT_STATE_FIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01004003 return -1;
4004 }
Christopher Fauletef779222018-10-31 08:47:01 +01004005
4006 full:
Christopher Fauletef779222018-10-31 08:47:01 +01004007 return 0;
4008
William Lallemand74c24fb2016-11-21 17:18:36 +01004009}
4010
4011/* We reached the stats page through a POST request. The appctx is
4012 * expected to have already been allocated by the caller.
4013 * Parse the posted data and enable/disable servers if necessary.
4014 * Returns 1 if request was parsed or zero if it needs more data.
4015 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004016static int stats_process_http_post(struct stconn *sc)
William Lallemand74c24fb2016-11-21 17:18:36 +01004017{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004018 struct stream *s = __sc_strm(sc);
4019 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004020 struct show_stat_ctx *ctx = appctx->svcctx;
William Lallemand74c24fb2016-11-21 17:18:36 +01004021
4022 struct proxy *px = NULL;
4023 struct server *sv = NULL;
4024
4025 char key[LINESIZE];
4026 int action = ST_ADM_ACTION_NONE;
4027 int reprocess = 0;
4028
4029 int total_servers = 0;
4030 int altered_servers = 0;
4031
4032 char *first_param, *cur_param, *next_param, *end_params;
4033 char *st_cur_param = NULL;
4034 char *st_next_param = NULL;
4035
Christopher Fauleta3618372018-12-13 21:59:56 +01004036 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01004037
Christopher Fauletb7f88902019-07-15 21:56:43 +02004038 struct htx *htx = htxbuf(&s->req.buf);
4039 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01004040
Christopher Fauletb7f88902019-07-15 21:56:43 +02004041 /* we need more data */
4042 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
4043 /* check if we can receive more */
4044 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004045 ctx->st_code = STAT_STATUS_EXCD;
Christopher Fauletb7f88902019-07-15 21:56:43 +02004046 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01004047 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004048 goto wait;
4049 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004050
Christopher Fauletb7f88902019-07-15 21:56:43 +02004051 /* The request was fully received. Copy data */
4052 blk = htx_get_head_blk(htx);
4053 while (blk) {
4054 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004055
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004056 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletb7f88902019-07-15 21:56:43 +02004057 break;
4058 if (type == HTX_BLK_DATA) {
4059 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01004060
Christopher Fauletb7f88902019-07-15 21:56:43 +02004061 if (!chunk_memcat(temp, v.ptr, v.len)) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004062 ctx->st_code = STAT_STATUS_EXCD;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004063 goto out;
4064 }
Christopher Fauleta3618372018-12-13 21:59:56 +01004065 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02004066 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01004067 }
4068
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004069 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01004070 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01004071 cur_param = next_param = end_params;
4072 *end_params = '\0';
4073
Willy Tarreau91cefca2022-05-03 17:08:29 +02004074 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004075
4076 /*
4077 * Parse the parameters in reverse order to only store the last value.
4078 * From the html form, the backend and the action are at the end.
4079 */
4080 while (cur_param > first_param) {
4081 char *value;
4082 int poffset, plen;
4083
4084 cur_param--;
4085
4086 if ((*cur_param == '&') || (cur_param == first_param)) {
4087 reprocess_servers:
4088 /* Parse the key */
4089 poffset = (cur_param != first_param ? 1 : 0);
4090 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4091 if ((plen > 0) && (plen <= sizeof(key))) {
4092 strncpy(key, cur_param + poffset, plen);
4093 key[plen - 1] = '\0';
4094 } else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004095 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004096 goto out;
4097 }
4098
4099 /* Parse the value */
4100 value = key;
4101 while (*value != '\0' && *value != '=') {
4102 value++;
4103 }
4104 if (*value == '=') {
4105 /* Ok, a value is found, we can mark the end of the key */
4106 *value++ = '\0';
4107 }
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02004108 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01004109 break;
4110
4111 /* Now we can check the key to see what to do */
4112 if (!px && (strcmp(key, "b") == 0)) {
4113 if ((px = proxy_be_by_name(value)) == NULL) {
4114 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004115 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004116 goto out;
4117 }
4118 }
4119 else if (!action && (strcmp(key, "action") == 0)) {
4120 if (strcmp(value, "ready") == 0) {
4121 action = ST_ADM_ACTION_READY;
4122 }
4123 else if (strcmp(value, "drain") == 0) {
4124 action = ST_ADM_ACTION_DRAIN;
4125 }
4126 else if (strcmp(value, "maint") == 0) {
4127 action = ST_ADM_ACTION_MAINT;
4128 }
4129 else if (strcmp(value, "shutdown") == 0) {
4130 action = ST_ADM_ACTION_SHUTDOWN;
4131 }
4132 else if (strcmp(value, "dhlth") == 0) {
4133 action = ST_ADM_ACTION_DHLTH;
4134 }
4135 else if (strcmp(value, "ehlth") == 0) {
4136 action = ST_ADM_ACTION_EHLTH;
4137 }
4138 else if (strcmp(value, "hrunn") == 0) {
4139 action = ST_ADM_ACTION_HRUNN;
4140 }
4141 else if (strcmp(value, "hnolb") == 0) {
4142 action = ST_ADM_ACTION_HNOLB;
4143 }
4144 else if (strcmp(value, "hdown") == 0) {
4145 action = ST_ADM_ACTION_HDOWN;
4146 }
4147 else if (strcmp(value, "dagent") == 0) {
4148 action = ST_ADM_ACTION_DAGENT;
4149 }
4150 else if (strcmp(value, "eagent") == 0) {
4151 action = ST_ADM_ACTION_EAGENT;
4152 }
4153 else if (strcmp(value, "arunn") == 0) {
4154 action = ST_ADM_ACTION_ARUNN;
4155 }
4156 else if (strcmp(value, "adown") == 0) {
4157 action = ST_ADM_ACTION_ADOWN;
4158 }
4159 /* else these are the old supported methods */
4160 else if (strcmp(value, "disable") == 0) {
4161 action = ST_ADM_ACTION_DISABLE;
4162 }
4163 else if (strcmp(value, "enable") == 0) {
4164 action = ST_ADM_ACTION_ENABLE;
4165 }
4166 else if (strcmp(value, "stop") == 0) {
4167 action = ST_ADM_ACTION_STOP;
4168 }
4169 else if (strcmp(value, "start") == 0) {
4170 action = ST_ADM_ACTION_START;
4171 }
4172 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004173 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004174 goto out;
4175 }
4176 }
4177 else if (strcmp(key, "s") == 0) {
4178 if (!(px && action)) {
4179 /*
4180 * Indicates that we'll need to reprocess the parameters
4181 * as soon as backend and action are known
4182 */
4183 if (!reprocess) {
4184 st_cur_param = cur_param;
4185 st_next_param = next_param;
4186 }
4187 reprocess = 1;
4188 }
4189 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004190 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004191 switch (action) {
4192 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004193 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004194 altered_servers++;
4195 total_servers++;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004196 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, SRV_ADM_STCHGC_STATS_DISABLE);
William Lallemand74c24fb2016-11-21 17:18:36 +01004197 }
4198 break;
4199 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02004200 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004201 altered_servers++;
4202 total_servers++;
4203 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
4204 }
4205 break;
4206 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02004207 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004208 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, SRV_ADM_STCHGC_STATS_STOP);
William Lallemand74c24fb2016-11-21 17:18:36 +01004209 altered_servers++;
4210 total_servers++;
4211 }
4212 break;
4213 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02004214 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01004215 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4216 altered_servers++;
4217 total_servers++;
4218 }
4219 break;
4220 case ST_ADM_ACTION_DHLTH:
4221 if (sv->check.state & CHK_ST_CONFIGURED) {
4222 sv->check.state &= ~CHK_ST_ENABLED;
4223 altered_servers++;
4224 total_servers++;
4225 }
4226 break;
4227 case ST_ADM_ACTION_EHLTH:
4228 if (sv->check.state & CHK_ST_CONFIGURED) {
4229 sv->check.state |= CHK_ST_ENABLED;
4230 altered_servers++;
4231 total_servers++;
4232 }
4233 break;
4234 case ST_ADM_ACTION_HRUNN:
4235 if (!(sv->track)) {
4236 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004237 srv_set_running(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004238 altered_servers++;
4239 total_servers++;
4240 }
4241 break;
4242 case ST_ADM_ACTION_HNOLB:
4243 if (!(sv->track)) {
4244 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004245 srv_set_stopping(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004246 altered_servers++;
4247 total_servers++;
4248 }
4249 break;
4250 case ST_ADM_ACTION_HDOWN:
4251 if (!(sv->track)) {
4252 sv->check.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004253 srv_set_stopped(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004254 altered_servers++;
4255 total_servers++;
4256 }
4257 break;
4258 case ST_ADM_ACTION_DAGENT:
4259 if (sv->agent.state & CHK_ST_CONFIGURED) {
4260 sv->agent.state &= ~CHK_ST_ENABLED;
4261 altered_servers++;
4262 total_servers++;
4263 }
4264 break;
4265 case ST_ADM_ACTION_EAGENT:
4266 if (sv->agent.state & CHK_ST_CONFIGURED) {
4267 sv->agent.state |= CHK_ST_ENABLED;
4268 altered_servers++;
4269 total_servers++;
4270 }
4271 break;
4272 case ST_ADM_ACTION_ARUNN:
4273 if (sv->agent.state & CHK_ST_ENABLED) {
4274 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004275 srv_set_running(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004276 altered_servers++;
4277 total_servers++;
4278 }
4279 break;
4280 case ST_ADM_ACTION_ADOWN:
4281 if (sv->agent.state & CHK_ST_ENABLED) {
4282 sv->agent.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004283 srv_set_stopped(sv, SRV_OP_STCHGC_STATS_WEB);
William Lallemand74c24fb2016-11-21 17:18:36 +01004284 altered_servers++;
4285 total_servers++;
4286 }
4287 break;
4288 case ST_ADM_ACTION_READY:
4289 srv_adm_set_ready(sv);
4290 altered_servers++;
4291 total_servers++;
4292 break;
4293 case ST_ADM_ACTION_DRAIN:
4294 srv_adm_set_drain(sv);
4295 altered_servers++;
4296 total_servers++;
4297 break;
4298 case ST_ADM_ACTION_MAINT:
4299 srv_adm_set_maint(sv);
4300 altered_servers++;
4301 total_servers++;
4302 break;
4303 case ST_ADM_ACTION_SHUTDOWN:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004304 if (!(px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Willy Tarreauaf7ea812019-11-14 16:42:04 +01004305 srv_shutdown_streams(sv, SF_ERR_KILLED);
William Lallemand74c24fb2016-11-21 17:18:36 +01004306 altered_servers++;
4307 total_servers++;
4308 }
4309 break;
4310 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004311 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01004312 } else {
4313 /* the server name is unknown or ambiguous (duplicate names) */
4314 total_servers++;
4315 }
4316 }
4317 if (reprocess && px && action) {
4318 /* Now, we know the backend and the action chosen by the user.
4319 * We can safely restart from the first server parameter
4320 * to reprocess them
4321 */
4322 cur_param = st_cur_param;
4323 next_param = st_next_param;
4324 reprocess = 0;
4325 goto reprocess_servers;
4326 }
4327
4328 next_param = cur_param;
4329 }
4330 }
4331
4332 if (total_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004333 ctx->st_code = STAT_STATUS_NONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004334 }
4335 else if (altered_servers == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004336 ctx->st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01004337 }
4338 else if (altered_servers == total_servers) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004339 ctx->st_code = STAT_STATUS_DONE;
William Lallemand74c24fb2016-11-21 17:18:36 +01004340 }
4341 else {
Willy Tarreau91cefca2022-05-03 17:08:29 +02004342 ctx->st_code = STAT_STATUS_PART;
William Lallemand74c24fb2016-11-21 17:18:36 +01004343 }
4344 out:
4345 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004346 wait:
Willy Tarreau91cefca2022-05-03 17:08:29 +02004347 ctx->st_code = STAT_STATUS_NONE;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01004348 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004349}
4350
4351
Willy Tarreaucaff6312022-05-27 10:17:46 +02004352static int stats_send_http_headers(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004353{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004354 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004355 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004356 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004357 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004358 struct htx_sl *sl;
4359 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004360
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004361 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);
4362 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
4363 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004364 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004365 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01004366
Christopher Fauletb829f4c2019-03-29 16:13:55 +01004367 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01004368 goto full;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004369 if (ctx->flags & STAT_FMT_HTML) {
Christopher Fauletef779222018-10-31 08:47:01 +01004370 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4371 goto full;
4372 }
Willy Tarreau91cefca2022-05-03 17:08:29 +02004373 else if (ctx->flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004374 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
4375 goto full;
4376 }
Christopher Fauletef779222018-10-31 08:47:01 +01004377 else {
4378 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
4379 goto full;
4380 }
4381
Willy Tarreau91cefca2022-05-03 17:08:29 +02004382 if (uri->refresh > 0 && !(ctx->flags & STAT_NO_REFRESH)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004383 const char *refresh = U2A(uri->refresh);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01004384 if (!htx_add_header(htx, ist("Refresh"), ist(refresh)))
Christopher Fauletef779222018-10-31 08:47:01 +01004385 goto full;
4386 }
4387
Willy Tarreau91cefca2022-05-03 17:08:29 +02004388 if (ctx->flags & STAT_CHUNKED) {
Christopher Fauletef779222018-10-31 08:47:01 +01004389 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
4390 goto full;
4391 }
4392
4393 if (!htx_add_endof(htx, HTX_BLK_EOH))
4394 goto full;
4395
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004396 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004397 return 1;
4398
4399 full:
4400 htx_reset(htx);
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004401 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004402 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01004403}
4404
Christopher Fauletef779222018-10-31 08:47:01 +01004405
Willy Tarreaucaff6312022-05-27 10:17:46 +02004406static int stats_send_http_redirect(struct stconn *sc, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01004407{
4408 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreaucaff6312022-05-27 10:17:46 +02004409 struct stream *s = __sc_strm(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004410 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreaucaff6312022-05-27 10:17:46 +02004411 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004412 struct show_stat_ctx *ctx = appctx->svcctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004413 struct htx_sl *sl;
4414 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01004415
Willy Tarreau91cefca2022-05-03 17:08:29 +02004416 /* scope_txt = search pattern + search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauletef779222018-10-31 08:47:01 +01004417 scope_txt[0] = 0;
Willy Tarreau91cefca2022-05-03 17:08:29 +02004418 if (ctx->scope_len) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004419 const char *scope_ptr = stats_scope_ptr(appctx, sc);
Christopher Fauleted7a0662019-01-14 11:07:34 +01004420
Willy Tarreaufc458ec2023-04-07 18:11:39 +02004421 strlcpy2(scope_txt, STAT_SCOPE_PATTERN, sizeof(scope_txt));
Willy Tarreau91cefca2022-05-03 17:08:29 +02004422 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, ctx->scope_len);
4423 scope_txt[strlen(STAT_SCOPE_PATTERN) + ctx->scope_len] = 0;
Christopher Fauletef779222018-10-31 08:47:01 +01004424 }
4425
4426 /* We don't want to land on the posted stats page because a refresh will
4427 * repost the data. We don't want this to happen on accident so we redirect
4428 * the browse to the stats page with a GET.
4429 */
4430 chunk_printf(&trash, "%s;st=%s%s%s%s",
4431 uri->uri_prefix,
Willy Tarreau91cefca2022-05-03 17:08:29 +02004432 ((ctx->st_code > STAT_STATUS_INIT) &&
4433 (ctx->st_code < STAT_STATUS_SIZE) &&
4434 stat_status_codes[ctx->st_code]) ?
4435 stat_status_codes[ctx->st_code] :
Christopher Fauletef779222018-10-31 08:47:01 +01004436 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau91cefca2022-05-03 17:08:29 +02004437 (ctx->flags & STAT_HIDE_DOWN) ? ";up" : "",
4438 (ctx->flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Christopher Fauletef779222018-10-31 08:47:01 +01004439 scope_txt);
4440
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004441 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
4442 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
4443 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01004444 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004445 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01004446
4447 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01004448 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
4449 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
4450 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
4451 goto full;
4452
4453 if (!htx_add_endof(htx, HTX_BLK_EOH))
4454 goto full;
4455
Christopher Faulet1e2d6362019-02-27 16:28:48 +01004456 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01004457 return 1;
4458
4459full:
4460 htx_reset(htx);
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004461 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004462 return 0;
4463}
William Lallemand74c24fb2016-11-21 17:18:36 +01004464
Simon Horman05ee2132017-01-04 09:37:25 +01004465
Willy Tarreau4596fe22022-05-17 19:07:51 +02004466/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +01004467 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
4468 * appctx->st0 contains the operation in progress (dump, done). The handler
4469 * automatically unregisters itself once transfer is complete.
4470 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02004471static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01004472{
Willy Tarreau91cefca2022-05-03 17:08:29 +02004473 struct show_stat_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02004474 struct stconn *sc = appctx_sc(appctx);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004475 struct stream *s = __sc_strm(sc);
4476 struct channel *req = sc_oc(sc);
4477 struct channel *res = sc_ic(sc);
Christopher Fauletef779222018-10-31 08:47:01 +01004478 struct htx *req_htx, *res_htx;
4479
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004480 /* only proxy stats are available via http */
Willy Tarreau91cefca2022-05-03 17:08:29 +02004481 ctx->domain = STATS_DOMAIN_PROXY;
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02004482
Christopher Fauletef779222018-10-31 08:47:01 +01004483 res_htx = htx_from_buf(&res->buf);
4484
Christopher Faulet9837bd82023-04-11 07:41:30 +02004485 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) {
4486 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004487 goto out;
Christopher Faulet9837bd82023-04-11 07:41:30 +02004488 }
Christopher Fauletef779222018-10-31 08:47:01 +01004489
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004490 /* Check if the input buffer is available. */
Christopher Fauletef779222018-10-31 08:47:01 +01004491 if (!b_size(&res->buf)) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004492 sc_need_room(sc, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01004493 goto out;
4494 }
4495
Christopher Fauletef779222018-10-31 08:47:01 +01004496 /* all states are processed in sequence */
4497 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004498 if (stats_send_http_headers(sc, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01004499 if (s->txn->meth == HTTP_METH_HEAD)
4500 appctx->st0 = STAT_HTTP_DONE;
4501 else
4502 appctx->st0 = STAT_HTTP_DUMP;
4503 }
4504 }
4505
4506 if (appctx->st0 == STAT_HTTP_DUMP) {
Aurelien DARRAGON14656842023-02-03 08:31:42 +01004507 trash_chunk = b_make(trash.area, res->buf.size, 0, 0);
Aurelien DARRAGON5e7ecbe2023-02-02 15:03:12 +01004508 /* adjust buffer size to take htx overhead into account,
4509 * make sure to perform this call on an empty buffer
4510 */
4511 trash_chunk.size = buf_room_for_htx_data(&trash_chunk);
Willy Tarreaucaff6312022-05-27 10:17:46 +02004512 if (stats_dump_stat_to_buffer(sc, res_htx, s->be->uri_auth))
Christopher Fauletef779222018-10-31 08:47:01 +01004513 appctx->st0 = STAT_HTTP_DONE;
4514 }
4515
4516 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004517 if (stats_process_http_post(sc))
Christopher Fauletef779222018-10-31 08:47:01 +01004518 appctx->st0 = STAT_HTTP_LAST;
Christopher Fauletca5309a2023-04-17 16:17:32 +02004519 else if (s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Fauletef779222018-10-31 08:47:01 +01004520 appctx->st0 = STAT_HTTP_DONE;
4521 }
4522
4523 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreaucaff6312022-05-27 10:17:46 +02004524 if (stats_send_http_redirect(sc, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01004525 appctx->st0 = STAT_HTTP_DONE;
4526 }
4527
4528 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004529 /* no more data are expected. If the response buffer is empty,
4530 * be sure to add something (EOT block in this case) to have
4531 * something to send. It is important to be sure the EOM flags
4532 * will be handled by the endpoint.
4533 */
4534 if (htx_is_empty(res_htx)) {
4535 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02004536 sc_need_room(sc, sizeof(struct htx_blk) + 1);
Christopher Faulet08b45cb2022-04-07 09:25:13 +02004537 goto out;
4538 }
4539 channel_add_input(res, 1);
4540 }
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004541 res_htx->flags |= HTX_FL_EOM;
Willy Tarreaud869e132022-05-17 18:05:31 +02004542 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004543 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01004544 }
4545
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004546 if (appctx->st0 == STAT_HTTP_END) {
Christopher Fauletdf15a5d2023-03-31 11:28:22 +02004547 se_fl_set(appctx->sedesc, SE_FL_EOS);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004548 applet_will_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004549 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01004550
Christopher Fauletef779222018-10-31 08:47:01 +01004551 out:
4552 /* we have left the request in the buffer for the case where we
4553 * process a POST, and this automatically re-enables activity on
4554 * read. It's better to indicate that we want to stop reading when
4555 * we're sending, so that we know there's at most one direction
4556 * deciding to wake the applet up. It saves it from looping when
4557 * emitting large blocks into small TCP windows.
4558 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004559 htx_to_buf(res_htx, &res->buf);
Christopher Fauletc2c043e2023-03-22 09:30:40 +01004560 if (appctx->st0 == STAT_HTTP_END) {
4561 /* eat the whole request */
4562 if (co_data(req)) {
4563 req_htx = htx_from_buf(&req->buf);
4564 co_htx_skip(req, req_htx, co_data(req));
4565 htx_to_buf(req_htx, &req->buf);
4566 }
4567 }
4568 else if (!channel_is_empty(res))
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02004569 applet_wont_consume(appctx);
Christopher Fauletef779222018-10-31 08:47:01 +01004570}
4571
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004572/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02004573static int stats_dump_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004574 const struct field *info,
4575 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004576{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004577 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004578 int field;
4579
4580 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4581 if (!field_format(info, field))
4582 continue;
4583
Willy Tarreaueaa55372019-10-09 07:39:11 +02004584 if (!chunk_appendf(out, "%s: ", info_fields[field].name))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004585 return 0;
4586 if (!stats_emit_raw_data_field(out, &info[field]))
4587 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004588 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4589 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004590 if (!chunk_strcat(out, "\n"))
4591 return 0;
4592 }
4593 return 1;
4594}
4595
4596/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02004597static int stats_dump_typed_info_fields(struct buffer *out,
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004598 const struct field *info,
4599 struct show_stat_ctx *ctx)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004600{
Aurelien DARRAGONe76a0272022-12-15 12:10:03 +01004601 int flags = ctx->flags;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004602 int field;
4603
4604 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
4605 if (!field_format(info, field))
4606 continue;
4607
Willy Tarreaueaa55372019-10-09 07:39:11 +02004608 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 +01004609 return 0;
4610 if (!stats_emit_field_tags(out, &info[field], ':'))
4611 return 0;
4612 if (!stats_emit_typed_data_field(out, &info[field]))
4613 return 0;
Willy Tarreau6b19b142019-10-09 15:44:21 +02004614 if ((flags & STAT_SHOW_FDESC) && !chunk_appendf(out, ":\"%s\"", info_fields[field].desc))
4615 return 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004616 if (!chunk_strcat(out, "\n"))
4617 return 0;
4618 }
4619 return 1;
4620}
4621
Willy Tarreau0b26b382021-05-08 07:43:53 +02004622/* Fill <info> with HAProxy global info. <info> is preallocated array of length
4623 * <len>. The length of the array must be INF_TOTAL_FIELDS. If this length is
4624 * less then this value, the function returns 0, otherwise, it returns 1. Some
4625 * fields' presence or precision may depend on some of the STAT_* flags present
4626 * in <flags>.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004627 */
Willy Tarreau0b26b382021-05-08 07:43:53 +02004628int stats_fill_info(struct field *info, int len, uint flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004629{
Willy Tarreau83061a82018-07-13 11:56:34 +02004630 struct buffer *out = get_trash_chunk();
Willy Tarreau6be8d092023-01-12 16:08:41 +01004631 uint64_t glob_out_bytes, glob_spl_bytes, glob_out_b32;
Willy Tarreau563efe62023-04-27 14:41:37 +02004632 uint up_sec, up_usec;
4633 ullong up;
Willy Tarreau5723b382023-05-17 09:05:20 +02004634 ulong boot;
Willy Tarreau6be8d092023-01-12 16:08:41 +01004635 int thr;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004636
4637#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004638 double ssl_sess_rate = read_freq_ctr_flt(&global.ssl_per_sec);
4639 double ssl_key_rate = read_freq_ctr_flt(&global.ssl_fe_keys_per_sec);
4640 double ssl_reuse = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004641
Willy Tarreauc5977722021-05-08 08:14:04 +02004642 if (ssl_key_rate < ssl_sess_rate)
4643 ssl_reuse = 100.0 * (1.0 - ssl_key_rate / ssl_sess_rate);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004644#endif
4645
Willy Tarreau6be8d092023-01-12 16:08:41 +01004646 /* sum certain per-thread totals (mostly byte counts) */
4647 glob_out_bytes = glob_spl_bytes = glob_out_b32 = 0;
4648 for (thr = 0; thr < global.nbthread; thr++) {
4649 glob_out_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].out_bytes);
4650 glob_spl_bytes += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].spliced_out_bytes);
4651 glob_out_b32 += read_freq_ctr(&ha_thread_ctx[thr].out_32bps);
4652 }
4653 glob_out_b32 *= 32; // values are 32-byte units
4654
Willy Tarreauc05d30e2023-04-28 14:50:29 +02004655 up = now_ns - start_time_ns;
Willy Tarreau563efe62023-04-27 14:41:37 +02004656 up_sec = ns_to_sec(up);
4657 up_usec = (up / 1000U) % 1000000U;
Willy Tarreaud37e26e2021-05-08 07:40:52 +02004658
Willy Tarreau5723b382023-05-17 09:05:20 +02004659 boot = tv_ms_remain(&start_date, &ready_date);
4660
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004661 if (len < INF_TOTAL_FIELDS)
4662 return 0;
4663
4664 chunk_reset(out);
4665 memset(info, 0, sizeof(*info) * len);
4666
4667 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004668 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovicb62b78b2021-01-15 13:12:33 +01004669 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01004670 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004671
Yves Lafon95317282018-02-26 11:10:37 +01004672 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau91358592021-06-15 08:08:04 +02004673 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
Willy Tarreaue8422bf2021-06-15 09:08:18 +02004674 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004675 info[INF_PID] = mkf_u32(FO_STATUS, pid);
4676
4677 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
Willy Tarreau563efe62023-04-27 14:41:37 +02004678 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 +01004679
Willy Tarreau563efe62023-04-27 14:41:37 +02004680 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 +02004681 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 +01004682 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
William Dauchya8766cf2021-01-15 22:41:37 +01004683 info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004684 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004685 info[INF_POOL_ALLOC_BYTES] = mkf_u64(0, pool_total_allocated());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004686 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
Christopher Fauletc960a3b2022-12-22 11:05:48 +01004687 info[INF_POOL_USED_BYTES] = mkf_u64(0, pool_total_used());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004688 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
4689 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
4690 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
4691 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
4692 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
4693 info[INF_CURR_CONN] = mkf_u32(0, actconn);
4694 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
4695 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
4696#ifdef USE_OPENSSL
4697 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
Willy Tarreau82531f62021-10-06 12:15:18 +02004698 info[INF_CURR_SSL_CONNS] = mkf_u32(0, global.sslconns);
4699 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, global.totalsslconns);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004700#endif
4701 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
4702 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
4703 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
Willy Tarreauc5977722021-05-08 08:14:04 +02004704 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 +01004705 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
4706 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004707 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 +01004708 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
4709 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
4710
4711#ifdef USE_OPENSSL
Willy Tarreauc5977722021-05-08 08:14:04 +02004712 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 +01004713 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
4714 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004715 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 +01004716 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
Willy Tarreauc5977722021-05-08 08:14:04 +02004717 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = (flags & STAT_USE_FLOAT) ? mkf_flt(FN_RATE, ssl_reuse) : mkf_u32(0, ssl_reuse);
4718 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 +01004719 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
4720 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
4721 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
4722#endif
Willy Tarreauc5977722021-05-08 08:14:04 +02004723 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));
4724 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 +01004725 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
4726#ifdef USE_ZLIB
4727 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
4728 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
4729#endif
Willy Tarreau955a11e2021-02-24 17:03:30 +01004730 info[INF_TASKS] = mkf_u32(0, total_allocated_tasks());
Willy Tarreau9c7b8082021-02-24 15:10:07 +01004731 info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues());
Willy Tarreauf9d5e102021-10-08 10:43:59 +02004732 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, clock_report_idle());
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004733 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
4734 if (global.desc)
4735 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004736 info[INF_STOPPING] = mkf_u32(0, stopping);
4737 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01004738 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01004739 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01004740 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01004741 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01004742 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01004743 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Emeric Brund30e9a12020-12-23 18:49:16 +01004744 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, resolv_failed_resolutions);
Willy Tarreau6be8d092023-01-12 16:08:41 +01004745 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, glob_out_bytes);
4746 info[INF_TOTAL_SPLICED_BYTES_OUT] = mkf_u64(0, glob_spl_bytes);
4747 info[INF_BYTES_OUT_RATE] = mkf_u64(FN_RATE, glob_out_b32);
Willy Tarreau9b013702019-10-24 18:18:02 +02004748 info[INF_DEBUG_COMMANDS_ISSUED] = mkf_u32(0, debug_commands_issued);
Emeric Brun45c457a2020-07-09 23:23:34 +02004749 info[INF_CUM_LOG_MSGS] = mkf_u32(FN_COUNTER, cum_log_messages);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004750
4751 info[INF_TAINTED] = mkf_str(FO_STATUS, chunk_newstr(out));
Willy Tarreau52fd8792023-05-17 08:43:15 +02004752 chunk_appendf(out, "%#x", get_tainted());
Willy Tarreau3c4a2972023-05-11 12:02:21 +02004753 info[INF_WARNINGS] = mkf_u32(FN_COUNTER, HA_ATOMIC_LOAD(&tot_warnings));
Willy Tarreau96151022023-05-11 13:51:31 +02004754 info[INF_MAXCONN_REACHED] = mkf_u32(FN_COUNTER, HA_ATOMIC_LOAD(&maxconn_reached));
Willy Tarreau5723b382023-05-17 09:05:20 +02004755 info[INF_BOOTTIME_MS] = mkf_u32(FN_DURATION, boot);
Amaury Denoyelle5dfdf3e2021-05-05 17:09:12 +02004756
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004757 return 1;
4758}
4759
Willy Tarreau4596fe22022-05-17 19:07:51 +02004760/* This function dumps information onto the stream connector's read buffer.
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004761 * It returns 0 as long as it does not complete, non-zero upon completion.
4762 * No state is used.
4763 */
Willy Tarreaucaff6312022-05-27 10:17:46 +02004764static int stats_dump_info_to_buffer(struct stconn *sc)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004765{
Willy Tarreaucaff6312022-05-27 10:17:46 +02004766 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004767 struct show_stat_ctx *ctx = appctx->svcctx;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004768 int ret;
4769 int current_field;
Christopher Faulet2da02ae2022-02-24 13:45:27 +01004770
Willy Tarreau91cefca2022-05-03 17:08:29 +02004771 if (!stats_fill_info(info, INF_TOTAL_FIELDS, ctx->flags))
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004772 return 0;
4773
Christopher Fauleta8b76842022-12-16 15:08:36 +01004774 chunk_reset(&trash_chunk);
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004775more:
4776 current_field = ctx->field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004777
Willy Tarreau91cefca2022-05-03 17:08:29 +02004778 if (ctx->flags & STAT_FMT_TYPED)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004779 ret = stats_dump_typed_info_fields(&trash_chunk, info, ctx);
Willy Tarreau91cefca2022-05-03 17:08:29 +02004780 else if (ctx->flags & STAT_FMT_JSON)
Christopher Fauleta8b76842022-12-16 15:08:36 +01004781 ret = stats_dump_json_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004782 else
Christopher Fauleta8b76842022-12-16 15:08:36 +01004783 ret = stats_dump_info_fields(&trash_chunk, info, ctx);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004784
Christopher Fauleta8b76842022-12-16 15:08:36 +01004785 if (applet_putchk(appctx, &trash_chunk) == -1) {
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004786 /* restore previous field */
4787 ctx->field = current_field;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004788 return 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01004789 }
4790 if (ret && ctx->field) {
4791 /* partial dump */
4792 goto more;
4793 }
4794 ctx->field = 0;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004795 return 1;
4796}
4797
Willy Tarreau4596fe22022-05-17 19:07:51 +02004798/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01004799 * It returns 0 as long as it does not complete, non-zero upon completion.
4800 * No state is used.
4801 *
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05004802 * Integer values bounded to the range [-(2**53)+1, (2**53)-1] as
Simon Horman6f6bb382017-01-04 09:37:26 +01004803 * per the recommendation for interoperable integers in section 6 of RFC 7159.
4804 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004805static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01004806{
4807
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004808 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01004809
4810 chunk_strcat(out,
4811 "{"
4812 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
4813 "\"oneOf\":["
4814 "{"
4815 "\"title\":\"Info\","
4816 "\"type\":\"array\","
4817 "\"items\":{"
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004818 "\"title\":\"InfoItem\","
4819 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01004820 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01004821 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4822 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4823 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4824 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
4825 "},"
4826 "\"required\":[\"field\",\"processNum\",\"tags\","
4827 "\"value\"]"
4828 "}"
4829 "},"
4830 "{"
4831 "\"title\":\"Stat\","
4832 "\"type\":\"array\","
4833 "\"items\":{"
4834 "\"title\":\"InfoItem\","
4835 "\"type\":\"object\","
4836 "\"properties\":{"
4837 "\"objType\":{"
4838 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
4839 "\"Server\",\"Unknown\"]"
4840 "},"
4841 "\"proxyId\":{"
4842 "\"type\":\"integer\","
4843 "\"minimum\":0"
4844 "},"
4845 "\"id\":{"
4846 "\"type\":\"integer\","
4847 "\"minimum\":0"
4848 "},"
4849 "\"field\":{\"$ref\":\"#/definitions/field\"},"
4850 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
4851 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
4852 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
4853 "},"
4854 "\"required\":[\"objType\",\"proxyId\",\"id\","
4855 "\"field\",\"processNum\",\"tags\","
4856 "\"value\"]"
4857 "}"
4858 "},"
4859 "{"
4860 "\"title\":\"Error\","
4861 "\"type\":\"object\","
4862 "\"properties\":{"
4863 "\"errorStr\":{"
4864 "\"type\":\"string\""
Amaury Denoyellea53ce4c2020-10-02 18:31:59 +02004865 "}"
4866 "},"
4867 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01004868 "}"
4869 "],"
4870 "\"definitions\":{"
4871 "\"field\":{"
4872 "\"type\":\"object\","
4873 "\"pos\":{"
4874 "\"type\":\"integer\","
4875 "\"minimum\":0"
4876 "},"
4877 "\"name\":{"
4878 "\"type\":\"string\""
4879 "},"
4880 "\"required\":[\"pos\",\"name\"]"
4881 "},"
4882 "\"processNum\":{"
4883 "\"type\":\"integer\","
4884 "\"minimum\":1"
4885 "},"
4886 "\"tags\":{"
4887 "\"type\":\"object\","
4888 "\"origin\":{"
4889 "\"type\":\"string\","
4890 "\"enum\":[\"Metric\",\"Status\",\"Key\","
4891 "\"Config\",\"Product\",\"Unknown\"]"
4892 "},"
4893 "\"nature\":{"
4894 "\"type\":\"string\","
4895 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
4896 "\"Rate\",\"Counter\",\"Duration\","
4897 "\"Age\",\"Time\",\"Name\",\"Output\","
4898 "\"Avg\", \"Unknown\"]"
4899 "},"
4900 "\"scope\":{"
4901 "\"type\":\"string\","
4902 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
4903 "\"System\",\"Unknown\"]"
4904 "},"
4905 "\"required\":[\"origin\",\"nature\",\"scope\"]"
4906 "},"
4907 "\"typedValue\":{"
4908 "\"type\":\"object\","
4909 "\"oneOf\":["
4910 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
4911 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
4912 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
4913 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
4914 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
4915 "],"
4916 "\"definitions\":{"
4917 "\"s32Value\":{"
4918 "\"properties\":{"
4919 "\"type\":{"
4920 "\"type\":\"string\","
4921 "\"enum\":[\"s32\"]"
4922 "},"
4923 "\"value\":{"
4924 "\"type\":\"integer\","
4925 "\"minimum\":-2147483648,"
4926 "\"maximum\":2147483647"
4927 "}"
4928 "},"
4929 "\"required\":[\"type\",\"value\"]"
4930 "},"
4931 "\"s64Value\":{"
4932 "\"properties\":{"
4933 "\"type\":{"
4934 "\"type\":\"string\","
4935 "\"enum\":[\"s64\"]"
4936 "},"
4937 "\"value\":{"
4938 "\"type\":\"integer\","
4939 "\"minimum\":-9007199254740991,"
4940 "\"maximum\":9007199254740991"
4941 "}"
4942 "},"
4943 "\"required\":[\"type\",\"value\"]"
4944 "},"
4945 "\"u32Value\":{"
4946 "\"properties\":{"
4947 "\"type\":{"
4948 "\"type\":\"string\","
4949 "\"enum\":[\"u32\"]"
4950 "},"
4951 "\"value\":{"
4952 "\"type\":\"integer\","
4953 "\"minimum\":0,"
4954 "\"maximum\":4294967295"
4955 "}"
4956 "},"
4957 "\"required\":[\"type\",\"value\"]"
4958 "},"
4959 "\"u64Value\":{"
4960 "\"properties\":{"
4961 "\"type\":{"
4962 "\"type\":\"string\","
4963 "\"enum\":[\"u64\"]"
4964 "},"
4965 "\"value\":{"
4966 "\"type\":\"integer\","
4967 "\"minimum\":0,"
4968 "\"maximum\":9007199254740991"
4969 "}"
4970 "},"
4971 "\"required\":[\"type\",\"value\"]"
4972 "},"
4973 "\"strValue\":{"
4974 "\"properties\":{"
4975 "\"type\":{"
4976 "\"type\":\"string\","
4977 "\"enum\":[\"str\"]"
4978 "},"
4979 "\"value\":{\"type\":\"string\"}"
4980 "},"
4981 "\"required\":[\"type\",\"value\"]"
4982 "},"
4983 "\"unknownValue\":{"
4984 "\"properties\":{"
4985 "\"type\":{"
4986 "\"type\":\"integer\","
4987 "\"minimum\":0"
4988 "},"
4989 "\"value\":{"
4990 "\"type\":\"string\","
4991 "\"enum\":[\"unknown\"]"
4992 "}"
4993 "},"
4994 "\"required\":[\"type\",\"value\"]"
4995 "}"
4996 "}"
4997 "}"
4998 "}"
4999 "}");
5000
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005001 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01005002 chunk_reset(out);
5003 chunk_appendf(out,
5004 "{\"errorStr\":\"output buffer too short\"}");
5005 }
Willy Tarreau5a0e7ca2022-06-10 09:21:22 +02005006 chunk_appendf(out, "\n");
Simon Horman6f6bb382017-01-04 09:37:26 +01005007}
5008
Willy Tarreau4596fe22022-05-17 19:07:51 +02005009/* This function dumps the schema onto the stream connector's read buffer.
Simon Horman6f6bb382017-01-04 09:37:26 +01005010 * It returns 0 as long as it does not complete, non-zero upon completion.
5011 * No state is used.
5012 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005013static int stats_dump_json_schema_to_buffer(struct appctx *appctx)
Simon Horman6f6bb382017-01-04 09:37:26 +01005014{
Simon Horman6f6bb382017-01-04 09:37:26 +01005015
Christopher Fauleta8b76842022-12-16 15:08:36 +01005016 chunk_reset(&trash_chunk);
Simon Horman6f6bb382017-01-04 09:37:26 +01005017
Christopher Fauleta8b76842022-12-16 15:08:36 +01005018 stats_dump_json_schema(&trash_chunk);
5019
5020 if (applet_putchk(appctx, &trash_chunk) == -1)
Simon Horman6f6bb382017-01-04 09:37:26 +01005021 return 0;
Simon Horman6f6bb382017-01-04 09:37:26 +01005022
5023 return 1;
5024}
5025
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005026static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01005027{
5028 struct proxy *px;
5029 struct server *sv;
5030 struct listener *li;
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005031 struct stats_module *mod;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005032 int clrall = 0;
5033
5034 if (strcmp(args[2], "all") == 0)
5035 clrall = 1;
5036
5037 /* check permissions */
5038 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
5039 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
5040 return 1;
5041
Olivier Houchardfbc74e82017-11-24 16:54:05 +01005042 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01005043 if (clrall) {
5044 memset(&px->be_counters, 0, sizeof(px->be_counters));
5045 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
5046 }
5047 else {
5048 px->be_counters.conn_max = 0;
5049 px->be_counters.p.http.rps_max = 0;
5050 px->be_counters.sps_max = 0;
5051 px->be_counters.cps_max = 0;
5052 px->be_counters.nbpend_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005053 px->be_counters.qtime_max = 0;
5054 px->be_counters.ctime_max = 0;
5055 px->be_counters.dtime_max = 0;
5056 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005057
5058 px->fe_counters.conn_max = 0;
5059 px->fe_counters.p.http.rps_max = 0;
5060 px->fe_counters.sps_max = 0;
5061 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005062 }
5063
5064 for (sv = px->srv; sv; sv = sv->next)
5065 if (clrall)
5066 memset(&sv->counters, 0, sizeof(sv->counters));
5067 else {
5068 sv->counters.cur_sess_max = 0;
5069 sv->counters.nbpend_max = 0;
5070 sv->counters.sps_max = 0;
Christopher Fauletefb41f02019-11-08 14:53:15 +01005071 sv->counters.qtime_max = 0;
5072 sv->counters.ctime_max = 0;
5073 sv->counters.dtime_max = 0;
5074 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01005075 }
5076
5077 list_for_each_entry(li, &px->conf.listeners, by_fe)
5078 if (li->counters) {
5079 if (clrall)
5080 memset(li->counters, 0, sizeof(*li->counters));
5081 else
5082 li->counters->conn_max = 0;
5083 }
5084 }
5085
5086 global.cps_max = 0;
5087 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02005088 global.ssl_max = 0;
5089 global.ssl_fe_keys_max = 0;
5090 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005091
Amaury Denoyelled3700a72020-10-05 11:49:43 +02005092 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5093 if (!mod->clearable && !clrall)
5094 continue;
5095
5096 for (px = proxies_list; px; px = px->next) {
5097 enum stats_domain_px_cap mod_cap = stats_px_get_cap(mod->domain_flags);
5098
5099 if (px->cap & PR_CAP_FE && mod_cap & STATS_PX_CAP_FE) {
5100 EXTRA_COUNTERS_INIT(px->extra_counters_fe,
5101 mod,
5102 mod->counters,
5103 mod->counters_size);
5104 }
5105
5106 if (px->cap & PR_CAP_BE && mod_cap & STATS_PX_CAP_BE) {
5107 EXTRA_COUNTERS_INIT(px->extra_counters_be,
5108 mod,
5109 mod->counters,
5110 mod->counters_size);
5111 }
5112
5113 if (mod_cap & STATS_PX_CAP_SRV) {
5114 for (sv = px->srv; sv; sv = sv->next) {
5115 EXTRA_COUNTERS_INIT(sv->extra_counters,
5116 mod,
5117 mod->counters,
5118 mod->counters_size);
5119 }
5120 }
5121
5122 if (mod_cap & STATS_PX_CAP_LI) {
5123 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5124 EXTRA_COUNTERS_INIT(li->extra_counters,
5125 mod,
5126 mod->counters,
5127 mod->counters_size);
5128 }
5129 }
5130 }
5131 }
5132
Emeric Brunf8642ee2021-10-29 17:59:18 +02005133 resolv_stats_clear_counters(clrall, &stats_module_list[STATS_DOMAIN_RESOLVERS]);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005134
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01005135 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01005136 return 1;
5137}
5138
5139
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005140static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005141{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005142 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005143 int arg = 2;
5144
Willy Tarreau91cefca2022-05-03 17:08:29 +02005145 ctx->scope_str = 0;
5146 ctx->scope_len = 0;
5147 ctx->flags = 0;
Aurelien DARRAGON55941842022-12-15 14:01:04 +01005148 ctx->field = 0; /* explicit default value */
Willy Tarreaud25fc792016-12-16 12:33:47 +01005149
Willy Tarreau2f397382019-10-09 11:43:59 +02005150 while (*args[arg]) {
5151 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005152 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005153 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005154 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005155 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005156 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau27456202021-05-08 07:54:24 +02005157 else if (strcmp(args[arg], "float") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005158 ctx->flags |= STAT_USE_FLOAT;
Willy Tarreau2f397382019-10-09 11:43:59 +02005159 arg++;
5160 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005161 return 0;
5162}
5163
5164
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005165static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01005166{
Willy Tarreau91cefca2022-05-03 17:08:29 +02005167 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau2f397382019-10-09 11:43:59 +02005168 int arg = 2;
5169
Willy Tarreau91cefca2022-05-03 17:08:29 +02005170 ctx->scope_str = 0;
5171 ctx->scope_len = 0;
5172 ctx->flags = STAT_SHNODE | STAT_SHDESC;
Willy Tarreau578d6e42019-10-09 11:00:22 +02005173
Willy Tarreau0698c802022-05-11 14:09:57 +02005174 if ((strm_li(appctx_strm(appctx))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005175 ctx->flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01005176
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005177 /* proxy is the default domain */
Willy Tarreau91cefca2022-05-03 17:08:29 +02005178 ctx->domain = STATS_DOMAIN_PROXY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005179 if (strcmp(args[arg], "domain") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005180 ++args;
5181
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01005182 if (strcmp(args[arg], "proxy") == 0) {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005183 ++args;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005184 } else if (strcmp(args[arg], "resolvers") == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02005185 ctx->domain = STATS_DOMAIN_RESOLVERS;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005186 ++args;
5187 } else {
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005188 return cli_err(appctx, "Invalid statistics domain.\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005189 }
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005190 }
5191
Willy Tarreau91cefca2022-05-03 17:08:29 +02005192 if (ctx->domain == STATS_DOMAIN_PROXY
Amaury Denoyelle072f97e2020-10-05 11:49:37 +02005193 && *args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005194 struct proxy *px;
5195
Willy Tarreau2f397382019-10-09 11:43:59 +02005196 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005197 if (px)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005198 ctx->iid = px->uuid;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005199 else
Willy Tarreau91cefca2022-05-03 17:08:29 +02005200 ctx->iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005201
Willy Tarreau91cefca2022-05-03 17:08:29 +02005202 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02005203 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01005204
Willy Tarreau91cefca2022-05-03 17:08:29 +02005205 ctx->flags |= STAT_BOUND;
5206 ctx->type = atoi(args[arg+1]);
5207 ctx->sid = atoi(args[arg+2]);
Willy Tarreau2f397382019-10-09 11:43:59 +02005208 arg += 3;
5209 }
5210
5211 while (*args[arg]) {
5212 if (strcmp(args[arg], "typed") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005213 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
Willy Tarreau2f397382019-10-09 11:43:59 +02005214 else if (strcmp(args[arg], "json") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005215 ctx->flags = (ctx->flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
Willy Tarreau2f397382019-10-09 11:43:59 +02005216 else if (strcmp(args[arg], "desc") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005217 ctx->flags |= STAT_SHOW_FDESC;
Willy Tarreau3e320362020-10-23 17:28:57 +02005218 else if (strcmp(args[arg], "no-maint") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005219 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau65141ff2020-10-23 17:19:48 +02005220 else if (strcmp(args[arg], "up") == 0)
Willy Tarreau91cefca2022-05-03 17:08:29 +02005221 ctx->flags |= STAT_HIDE_DOWN;
Willy Tarreau2f397382019-10-09 11:43:59 +02005222 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01005223 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01005224
Willy Tarreau2b812e22016-11-22 16:18:05 +01005225 return 0;
5226}
5227
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005228static int cli_io_handler_dump_info(struct appctx *appctx)
5229{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005230 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005231 return stats_dump_info_to_buffer(appctx_sc(appctx));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01005232}
5233
Willy Tarreau4596fe22022-05-17 19:07:51 +02005234/* This I/O handler runs as an applet embedded in a stream connector. It is
Willy Tarreau2b812e22016-11-22 16:18:05 +01005235 * used to send raw stats over a socket.
5236 */
5237static int cli_io_handler_dump_stat(struct appctx *appctx)
5238{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005239 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreauc12b3212022-05-27 11:08:15 +02005240 return stats_dump_stat_to_buffer(appctx_sc(appctx), NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01005241}
5242
Simon Horman6f6bb382017-01-04 09:37:26 +01005243static int cli_io_handler_dump_json_schema(struct appctx *appctx)
5244{
Christopher Fauleta8b76842022-12-16 15:08:36 +01005245 trash_chunk = b_make(trash.area, trash.size, 0, 0);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005246 return stats_dump_json_schema_to_buffer(appctx);
Simon Horman6f6bb382017-01-04 09:37:26 +01005247}
5248
Amaury Denoyelle216a1ce2021-03-18 15:48:14 +01005249int stats_allocate_proxy_counters_internal(struct extra_counters **counters,
5250 int type, int px_cap)
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005251{
5252 struct stats_module *mod;
5253
5254 EXTRA_COUNTERS_REGISTER(counters, type, alloc_failed);
5255
5256 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5257 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5258 continue;
5259
5260 EXTRA_COUNTERS_ADD(mod, *counters, mod->counters, mod->counters_size);
5261 }
5262
5263 EXTRA_COUNTERS_ALLOC(*counters, alloc_failed);
5264
5265 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5266 if (!(stats_px_get_cap(mod->domain_flags) & px_cap))
5267 continue;
5268
5269 EXTRA_COUNTERS_INIT(*counters, mod, mod->counters, mod->counters_size);
5270 }
5271
5272 return 1;
5273
5274 alloc_failed:
5275 return 0;
5276}
5277
5278/* Initialize and allocate all extra counters for a proxy and its attached
5279 * servers/listeners with all already registered stats module
5280 */
5281int stats_allocate_proxy_counters(struct proxy *px)
5282{
5283 struct server *sv;
5284 struct listener *li;
5285
5286 if (px->cap & PR_CAP_FE) {
5287 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_fe,
5288 COUNTERS_FE,
5289 STATS_PX_CAP_FE)) {
5290 return 0;
5291 }
5292 }
5293
5294 if (px->cap & PR_CAP_BE) {
5295 if (!stats_allocate_proxy_counters_internal(&px->extra_counters_be,
5296 COUNTERS_BE,
5297 STATS_PX_CAP_BE)) {
5298 return 0;
5299 }
5300 }
5301
5302 for (sv = px->srv; sv; sv = sv->next) {
5303 if (!stats_allocate_proxy_counters_internal(&sv->extra_counters,
5304 COUNTERS_SV,
5305 STATS_PX_CAP_SRV)) {
5306 return 0;
5307 }
5308 }
5309
5310 list_for_each_entry(li, &px->conf.listeners, by_fe) {
5311 if (!stats_allocate_proxy_counters_internal(&li->extra_counters,
5312 COUNTERS_LI,
5313 STATS_PX_CAP_LI)) {
5314 return 0;
5315 }
5316 }
5317
5318 return 1;
5319}
5320
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005321void stats_register_module(struct stats_module *m)
5322{
5323 const uint8_t domain = stats_get_domain(m->domain_flags);
5324
Willy Tarreau2b718102021-04-21 07:32:39 +02005325 LIST_APPEND(&stats_module_list[domain], &m->list);
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005326 stat_count[domain] += m->stats_count;
Amaury Denoyelle58d395e2020-10-05 11:49:40 +02005327}
5328
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005329static int allocate_stats_px_postcheck(void)
5330{
5331 struct stats_module *mod;
5332 size_t i = ST_F_TOTAL_FIELDS;
5333 int err_code = 0;
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005334 struct proxy *px;
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005335
5336 stat_count[STATS_DOMAIN_PROXY] += ST_F_TOTAL_FIELDS;
5337
5338 stat_f[STATS_DOMAIN_PROXY] = malloc(stat_count[STATS_DOMAIN_PROXY] * sizeof(struct name_desc));
5339 if (!stat_f[STATS_DOMAIN_PROXY]) {
5340 ha_alert("stats: cannot allocate all fields for proxy statistics\n");
5341 err_code |= ERR_ALERT | ERR_FATAL;
5342 return err_code;
5343 }
5344
5345 memcpy(stat_f[STATS_DOMAIN_PROXY], stat_fields,
5346 ST_F_TOTAL_FIELDS * sizeof(struct name_desc));
5347
5348 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_PROXY], list) {
5349 memcpy(stat_f[STATS_DOMAIN_PROXY] + i,
5350 mod->stats,
5351 mod->stats_count * sizeof(struct name_desc));
5352 i += mod->stats_count;
5353 }
5354
Amaury Denoyelle27373f72020-10-05 16:57:33 +02005355 for (px = proxies_list; px; px = px->next) {
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005356 if (!stats_allocate_proxy_counters(px)) {
5357 ha_alert("stats: cannot allocate all counters for proxy statistics\n");
5358 err_code |= ERR_ALERT | ERR_FATAL;
5359 return err_code;
5360 }
5361 }
5362
Christopher Fauletde79cd22021-01-06 07:41:56 +01005363 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyelleee63d4b2020-10-05 11:49:42 +02005364
5365 return err_code;
5366}
5367
5368REGISTER_CONFIG_POSTPARSER("allocate-stats-px", allocate_stats_px_postcheck);
5369
Emeric Brunf8642ee2021-10-29 17:59:18 +02005370static int allocate_stats_rslv_postcheck(void)
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005371{
5372 struct stats_module *mod;
5373 size_t i = 0;
5374 int err_code = 0;
5375
Emeric Brunf8642ee2021-10-29 17:59:18 +02005376 stat_f[STATS_DOMAIN_RESOLVERS] = malloc(stat_count[STATS_DOMAIN_RESOLVERS] * sizeof(struct name_desc));
5377 if (!stat_f[STATS_DOMAIN_RESOLVERS]) {
5378 ha_alert("stats: cannot allocate all fields for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005379 err_code |= ERR_ALERT | ERR_FATAL;
5380 return err_code;
5381 }
5382
Emeric Brunf8642ee2021-10-29 17:59:18 +02005383 list_for_each_entry(mod, &stats_module_list[STATS_DOMAIN_RESOLVERS], list) {
5384 memcpy(stat_f[STATS_DOMAIN_RESOLVERS] + i,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005385 mod->stats,
5386 mod->stats_count * sizeof(struct name_desc));
5387 i += mod->stats_count;
5388 }
5389
Emeric Brunf8642ee2021-10-29 17:59:18 +02005390 if (!resolv_allocate_counters(&stats_module_list[STATS_DOMAIN_RESOLVERS])) {
5391 ha_alert("stats: cannot allocate all counters for resolver statistics\n");
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005392 err_code |= ERR_ALERT | ERR_FATAL;
5393 return err_code;
5394 }
5395
Christopher Fauletde79cd22021-01-06 07:41:56 +01005396 /* wait per-thread alloc to perform corresponding stat_l allocation */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005397
5398 return err_code;
5399}
5400
Emeric Brunf8642ee2021-10-29 17:59:18 +02005401REGISTER_CONFIG_POSTPARSER("allocate-stats-resolver", allocate_stats_rslv_postcheck);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02005402
Christopher Fauletde79cd22021-01-06 07:41:56 +01005403static int allocate_stat_lines_per_thread(void)
5404{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005405 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005406
5407 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5408 const int domain = domains[i];
5409
5410 stat_l[domain] = malloc(stat_count[domain] * sizeof(struct field));
5411 if (!stat_l[domain])
5412 return 0;
5413 }
5414 return 1;
5415}
5416
5417REGISTER_PER_THREAD_ALLOC(allocate_stat_lines_per_thread);
5418
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005419static int allocate_trash_counters(void)
5420{
5421 struct stats_module *mod;
Emeric Brunf8642ee2021-10-29 17:59:18 +02005422 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005423 size_t max_counters_size = 0;
5424
5425 /* calculate the greatest counters used by any stats modules */
5426 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5427 list_for_each_entry(mod, &stats_module_list[domains[i]], list) {
5428 max_counters_size = mod->counters_size > max_counters_size ?
5429 mod->counters_size : max_counters_size;
5430 }
5431 }
5432
5433 /* allocate the trash with the size of the greatest counters */
5434 if (max_counters_size) {
5435 trash_counters = malloc(max_counters_size);
5436 if (!trash_counters) {
5437 ha_alert("stats: cannot allocate trash counters for statistics\n");
5438 return 0;
5439 }
5440 }
5441
5442 return 1;
5443}
5444
5445REGISTER_PER_THREAD_ALLOC(allocate_trash_counters);
5446
Christopher Fauletde79cd22021-01-06 07:41:56 +01005447static void deinit_stat_lines_per_thread(void)
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005448{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005449 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005450
5451 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5452 const int domain = domains[i];
5453
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005454 ha_free(&stat_l[domain]);
Christopher Fauletde79cd22021-01-06 07:41:56 +01005455 }
5456}
5457
5458
5459REGISTER_PER_THREAD_FREE(deinit_stat_lines_per_thread);
5460
5461static void deinit_stats(void)
5462{
Emeric Brunf8642ee2021-10-29 17:59:18 +02005463 int domains[] = { STATS_DOMAIN_PROXY, STATS_DOMAIN_RESOLVERS }, i;
Christopher Fauletde79cd22021-01-06 07:41:56 +01005464
5465 for (i = 0; i < STATS_DOMAIN_COUNT; ++i) {
5466 const int domain = domains[i];
Amaury Denoyellea2a68992020-11-10 14:24:30 +01005467
5468 if (stat_f[domain])
5469 free(stat_f[domain]);
5470 }
5471}
5472
5473REGISTER_POST_DEINIT(deinit_stats);
5474
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01005475static void free_trash_counters(void)
5476{
5477 if (trash_counters)
5478 free(trash_counters);
5479}
5480
5481REGISTER_PER_THREAD_FREE(free_trash_counters);
5482
Willy Tarreau2b812e22016-11-22 16:18:05 +01005483/* register cli keywords */
5484static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005485 { { "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 +02005486 { { "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 +02005487 { { "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 },
5488 { { "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 +01005489 {{},}
5490}};
5491
Willy Tarreau0108d902018-11-25 19:14:37 +01005492INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
5493
William Lallemand74c24fb2016-11-21 17:18:36 +01005494struct applet http_stats_applet = {
5495 .obj_type = OBJ_TYPE_APPLET,
5496 .name = "<STATS>", /* used for logging */
5497 .fct = http_stats_io_handler,
5498 .release = NULL,
5499};
5500
William Lallemand74c24fb2016-11-21 17:18:36 +01005501/*
5502 * Local variables:
5503 * c-indent-level: 8
5504 * c-basic-offset: 8
5505 * End:
5506 */